Skip to content

Commit

Permalink
pageContext.urlParsed.{hostname,port} instead of `pageContext.urlPa…
Browse files Browse the repository at this point in the history
…rsed.host`
  • Loading branch information
brillout committed Jul 19, 2024
1 parent d4c1437 commit 7c38567
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
6 changes: 4 additions & 2 deletions docs/pages/pageContext/+Page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ Built-in properties:
href: string
origin: null | string
protocol: null | string
host: null | string
hostname: null | string
port: null | string
}
```

Expand All @@ -90,7 +91,8 @@ Built-in properties:
href: 'https://example.com/hello/s%C3%A9bastien?fruit=%C3%A2pple&fruit=orânge#%C3%A2ge',
origin: 'https://example.com',
protocol: 'https://',
host: 'example.com',
hostname: 'example.com', // 'localhost' for http://localhost:3000
port: null // 3000 for http://localhost:3000
}
```
- **`pageContext.headers`**: The headers of the HTTP Request. As a string object (`Record<string, string>`) normalized by Vike, see <Link href="/headers"/>.
Expand Down
39 changes: 30 additions & 9 deletions vike/utils/parseUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ type UrlPublic = {
href: string
/** The URL protocol, e.g. `https://` in `https://example.com` */
protocol: null | string
/** The URL host, e.g. `example.com` in `https://example.com/product` */
host: null | string
/** The URL hostname, e.g. `example.com` in `https://example.com/product` and `localhost` in `http://localhost:3000/product` */
hostname: null | string
/** The URL host port, e.g. `3000` in `http://localhost:3000/product` */
port: null | number
/** The URL origin, e.g. `https://example.com` in `https://example.com/product/42` */
origin: null | string
/** The URL pathname, e.g. `/product/42` in `https://example.com/product/42?details=yes#reviews` */
Expand All @@ -55,11 +57,7 @@ type UrlPublic = {
type UrlPrivate = Omit<UrlPublic, 'hashString' | 'searchString'> & { hasBaseServer: boolean }

function parseUrl(url: string, baseServer: string): UrlPrivate {
assert(
isUrl(url),
// Eventually remove debug log once URL handling is stable
{ url }
)
assert(isUrl(url), url)
assert(baseServer.startsWith('/'))

// Hash
Expand Down Expand Up @@ -89,9 +87,12 @@ function parseUrl(url: string, baseServer: string): UrlPrivate {
// Base URL
let { pathname, hasBaseServer } = removeBaseServer(pathnameAbsoluteWithBase, baseServer)

// More props
// pageContext.urlParsed.href
const href = createUrlFromComponents(origin, pathname, searchOriginal, hashOriginal)

// pageContext.urlParsed.{hostname, port}
const host = !origin ? null : origin.slice(protocol!.length)
const { hostname, port } = parseHost(host, url)

// decode after setting href
pathname = decodePathname(pathname)
Expand All @@ -100,7 +101,8 @@ function parseUrl(url: string, baseServer: string): UrlPrivate {
return {
href,
protocol,
host,
hostname,
port,
origin,
pathname,
pathnameOriginal: pathnameOriginal,
Expand Down Expand Up @@ -179,6 +181,25 @@ function parseOrigin(url: string): { pathname: string; origin: null | string; pr
return { pathname, origin, protocol }
}
}
function parseHost(host: string | null, url: string) {
const ret: { hostname: string | null; port: number | null } = { hostname: null, port: null }
if (!host) return ret

// hostname
const [hostname, ...rest] = host.split(':')
ret.hostname = hostname!

// port
if (rest.length > 0) {
assert(rest.length === 1, url)
const portStr = rest[0]!
const port = parseInt(portStr, 10)
assert(port || port === 0, url)
ret.port = port
}

return ret
}
function parseProtocol(uri: string) {
const SEP = ':'
const [before, ...after] = uri.split(SEP)
Expand Down

0 comments on commit 7c38567

Please sign in to comment.