Skip to content

Commit

Permalink
refactor URL type
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Jul 19, 2024
1 parent 814f209 commit 144f2b8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 53 deletions.
9 changes: 5 additions & 4 deletions vike/node/runtime/renderPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
removeUrlOrigin,
addUrlOrigin,
createUrlFromComponents,
isUri
isUri,
type UrlPublic
} from './utils.js'
import {
assertNoInfiniteAbortLoop,
Expand All @@ -51,7 +52,7 @@ import type { PageContextDebugRouteMatches } from './renderPage/debugPageFiles.j
import { log404 } from './renderPage/log404/index.js'
import { isConfigInvalid } from './renderPage/isConfigInvalid.js'
import pc from '@brillout/picocolors'
import type { PageContextBuiltInServer, Url } from '../../types/index.js'
import type { PageContextServer } from '../../types/index.js'
import { serializePageContextAbort, serializePageContextClientSide } from './html/serializePageContextClientSide.js'
import { getErrorPageId } from '../../shared/error-page.js'
import { handleErrorWithoutErrorPage } from './renderPage/handleErrorWithoutErrorPage.js'
Expand Down Expand Up @@ -85,7 +86,7 @@ async function renderPage<
pageContextInit: PageContextInit
): Promise<
// Partial because rendering may fail at any user hook. Also Partial when httpResponse !== null because .pageContext.json requests may fail while still returning the HTTP response `JSON.stringify({ serverSideError: true })`.
PageContextInit & { httpResponse: HttpResponse | null } & Partial<PageContextBuiltInServer & PageContextUserAdded>
PageContextInit & { httpResponse: HttpResponse | null } & Partial<PageContextServer & PageContextUserAdded>
> {
assertArguments(...arguments)
assert(hasProp(pageContextInit, 'urlOriginal', 'string'))
Expand Down Expand Up @@ -551,7 +552,7 @@ async function handleAbortError(
// handleAbortError() creates a new pageContext object and we don't merge pageContextNominalPageInit to it: we only use some pageContextNominalPageInit information.
pageContextNominalPageInit: {
urlOriginal: string
urlParsed: Url
urlParsed: UrlPublic
_urlRewrite: null | string
isClientSideNavigation: boolean
},
Expand Down
45 changes: 5 additions & 40 deletions vike/shared/getPageContextUrlComputed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ export type { PageContextUrlInternal }
export type { PageContextUrlClient }
export type { PageContextUrlServer }
export type { PageContextUrlSource }
// Public type (https://github.com/vikejs/vike/issues/1184)
export type { Url }

// =====================
// File determining the URL logic.
Expand All @@ -20,47 +18,14 @@ import {
isPlainObject,
isPropertyGetter,
isBrowser,
changeEnumerable
changeEnumerable,
UrlPublic
} from './utils.js'

// JSDocs copied from https://vike.dev/pageContext
type Url = {
/** The full URL. (Without Base URL.) */
href: null | string
/** The full URL. (With Base URL.) */
hrefOriginal: null | 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 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` */
pathname: string
/** URL pathname including the Base URL, e.g. `/some-base-url/product/42` in `https://example.com/some-base-url/product/42` (whereas `pageContext.urlParsed.pathname` is `/product/42`) */
pathnameOriginal: string
/** The URL search parameters, e.g. `{ details: 'yes' }` for `https://example.com/product/42?details=yes#reviews` */
search: Record<string, string>
/** The URL search parameters array, e.g. `{ fruit: ['apple', 'orange'] }` for `https://example.com?fruit=apple&fruit=orange` **/
searchAll: Record<string, string[]>
/** The URL search parameterer string, e.g. `?details=yes` in `https://example.com/product/42?details=yes#reviews` */
searchOriginal: null | string
/** The URL hash, e.g. `reviews` in `https://example.com/product/42?details=yes#reviews` */
hash: string
/** The URL hash string, e.g. `#reviews` in `https://example.com/product/42?details=yes#reviews` */
hashOriginal: null | string

// TODO/v1-release: remove
/** @deprecated */
hashString: null | string
/** @deprecated */
searchString: null | string
}

// TODO/v1-release: move pageContext.urlParsed to pageContext.url
type PageContextUrlComputed = {
/** Parsed information about the current URL */
urlParsed: Url
urlParsed: UrlPublic
/** The URL pathname, e.g. `/product/42` of `https://example.com/product/42?details=yes#reviews` */
urlPathname: string
/** @deprecated */
Expand Down Expand Up @@ -182,7 +147,7 @@ function urlGetter(this: PageContextUrlSource) {
return urlPathnameGetter.call(this)
}
function urlParsedGetter(this: PageContextUrlSource) {
const urlParsed = getUrlParsed(this)
const { hasBaseServer: _, ...urlParsed } = getUrlParsed(this)

const hashIsAvailable = isBrowser()
const warnHashNotAvailable = (prop: HashProps) => {
Expand All @@ -193,7 +158,7 @@ function urlParsedGetter(this: PageContextUrlSource) {
)
}

const urlParsedEnhanced: Url = {
const urlParsedEnhanced: UrlPublic = {
...urlParsed,
get hash() {
warnHashNotAvailable('hash')
Expand Down
2 changes: 1 addition & 1 deletion vike/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type {
} from '../node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.js'
export type { ConfigEntries } from '../shared/getPageFiles/getExports.js'

export type { Url } from '../shared/getPageContextUrlComputed.js'
export type { UrlPublic as Url } from '../utils/parseUrl.js'

export type { InjectFilterEntry } from '../node/runtime/html/injectAssets/getHtmlTags.js'

Expand Down
36 changes: 28 additions & 8 deletions vike/utils/parseUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,49 @@ export { isUrlExternal }
export { isBaseServer }
export { assertUrlComponents }
export { createUrlFromComponents }
export type { UrlPublic }
export type { UrlPrivate }

import { slice } from './slice.js'
import { assert, assertUsage } from './assert.js'
import pc from '@brillout/picocolors'

function parseUrl(
url: string,
baseServer: string
): {
href: string
hrefOriginal: string
// JSDocs copied from https://vike.dev/pageContext
type UrlPublic = {
/** The full URL. (Without Base URL.) */
href: null | string
/** The full URL. (With Base URL.) */
hrefOriginal: null | 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 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` */
pathname: string
/** URL pathname including the Base URL, e.g. `/some-base-url/product/42` in `https://example.com/some-base-url/product/42` (whereas `pageContext.urlParsed.pathname` is `/product/42`) */
pathnameOriginal: string
hasBaseServer: boolean
/** The URL search parameters, e.g. `{ details: 'yes' }` for `https://example.com/product/42?details=yes#reviews` */
search: Record<string, string>
/** The URL search parameters array, e.g. `{ fruit: ['apple', 'orange'] }` for `https://example.com?fruit=apple&fruit=orange` **/
searchAll: Record<string, string[]>
/** The URL search parameterer string, e.g. `?details=yes` in `https://example.com/product/42?details=yes#reviews` */
searchOriginal: null | string
/** The URL hash, e.g. `reviews` in `https://example.com/product/42?details=yes#reviews` */
hash: string
/** The URL hash string, e.g. `#reviews` in `https://example.com/product/42?details=yes#reviews` */
hashOriginal: null | string
} {

// TODO/v1-release: remove
/** @deprecated */
hashString: null | string
/** @deprecated */
searchString: null | string
}
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
Expand Down

0 comments on commit 144f2b8

Please sign in to comment.