rsnext/packages/next/shared/lib/get-hostname.ts
Javi Velasco 523704b83f
Execute middleware on Next.js internal requests (#37121)
* Do not exclude internal _next request in middleware

* Allow for `NextURL` to parse prefetch requests

* Add test for middleware data prefetch

* Refactor `hasBasePath` and `replaceBasePath`

* Refactor `removeTrailingSlash`

* Refactor parsed next url to use `getNextPathnameInfo`

* Allow to configure `NextURL`

* Ensure middleware rewrites with always with a locale

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-05-27 13:29:04 -05:00

14 lines
528 B
TypeScript

/**
* Takes an object with a hostname property (like a parsed URL) and some
* headers that may contain Host and returns the preferred hostname.
* @param parsed An object containing a hostname property.
* @param headers A dictionary with headers containing a `host`.
*/
export function getHostname(
parsed: { hostname?: string | null },
headers?: { [key: string]: string | string[] | undefined }
) {
return ((!Array.isArray(headers?.host) && headers?.host) || parsed.hostname)
?.split(':')[0]
.toLowerCase()
}