Skip to content

Commit

Permalink
minor misc (#2181)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout authored Feb 14, 2025
1 parent bc234f8 commit 4c843cd
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
6 changes: 3 additions & 3 deletions docs/pages/pageContext/+Page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ Built-in properties:
```
```js
{
// Without Base URL, decodes escaped characters
// Without Base URL, and decodes escaped characters.
pathname: '/hello/sébastien',
// With Base URL, doesn't decode escaped characters
// With Base URL, and doesn't decode escaped characters.
pathnameOriginal: '/some-base-url/hello/s%C3%A9bastien',
search: { fruit: 'orânge' },
searchAll: { fruit: ['âpple', 'orânge'] },
searchOriginal: '?fruit=%C3%A2pple&fruit=orânge',
hash: 'âge',
hashOriginal: '#%C3%A2ge'
// Without Base URL, doesn't decode escaped characters
// Without Base URL, and doesn't decode escaped characters.
href: 'https://example.com/hello/s%C3%A9bastien?fruit=%C3%A2pple&fruit=orânge#%C3%A2ge',
origin: 'https://example.com',
protocol: 'https://',
Expand Down
1 change: 1 addition & 0 deletions vike/TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- `TODO/now`
- `getGlobalContext()`
- make scripts/dev.js work
- Add CLI option `--host`?
- Prettify build error
Expand Down
4 changes: 2 additions & 2 deletions vike/node/plugin/shared/getHttpRequestAsyncStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
export { getHttpRequestAsyncStore }
export { installHttpRequestAsyncStore }

import { renderPage_addWrapper } from '../../runtime/renderPage.js'
import { renderPage_addAsyncHookwrapper } from '../../runtime/renderPage.js'
import { assert, assertIsNotProductionRuntime, isObject, unique } from '../utils.js'
import type { AsyncLocalStorage as AsyncLocalStorageType } from 'node:async_hooks'
import { getConfigBuildErrorFormatted } from '../plugins/importUserCode/v1-design/getVikeConfig/transpileAndExecuteFile.js'
Expand All @@ -41,7 +41,7 @@ async function installHttpRequestAsyncStore(): Promise<void> {
return
}
asyncLocalStorage = new mod.AsyncLocalStorage()
renderPage_addWrapper(async (httpRequestId, renderPage) => {
renderPage_addAsyncHookwrapper(async (httpRequestId, renderPage) => {
assert(asyncLocalStorage)

const loggedErrors = new Set<unknown>()
Expand Down
4 changes: 3 additions & 1 deletion vike/node/runtime/globalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ function getViteConfig(): ResolvedConfig | null {

async function initGlobalContext_renderPage(): Promise<void> {
debug('initGlobalContext_renderPage()')
// globalObject.isProduction isn't set upon production server without vike-server (there isn't any signal we can use)

// `globalObject.isProduction === undefined` when using production server without `vike-server`. (There isn't any reliable signal we can use to determine early whether the environement is production or development.)
if (globalObject.isProduction === undefined) setIsProduction(true)

await initGlobalContext()
}

Expand Down
27 changes: 17 additions & 10 deletions vike/node/runtime/renderPage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { renderPage }
export { renderPage_addWrapper }
export { renderPage_addAsyncHookwrapper }

import {
getPageContextInitEnhanced,
Expand Down Expand Up @@ -66,12 +66,6 @@ import type { PageConfigRuntime } from '../../shared/page-configs/PageConfig.js'
const globalObject = getGlobalObject('runtime/renderPage.ts', {
httpRequestsCount: 0
})
let renderPage_wrapper = async <PageContext>(_httpRequestId: number, ret: () => Promise<PageContext>) => ({
pageContextReturn: await ret()
})
const renderPage_addWrapper = (wrapper: typeof renderPage_wrapper) => {
renderPage_wrapper = wrapper
}

type PageContextAfterRender = { httpResponse: HttpResponse } & Partial<PageContextBuiltInServerInternal>

Expand Down Expand Up @@ -103,7 +97,7 @@ async function renderPage<
const urlOriginalPretty = getUrlPretty(pageContextInit.urlOriginal)
logHttpRequest(urlOriginalPretty, httpRequestId)

const { pageContextReturn } = await renderPage_wrapper(httpRequestId, () =>
const { pageContextReturn } = await asyncHookWrapper(httpRequestId, () =>
renderPageAndPrepare(pageContextInit, httpRequestId)
)

Expand All @@ -113,6 +107,16 @@ async function renderPage<
assert(pageContextReturn.httpResponse)
return pageContextReturn as any
}

// Fallback wrapper if node:async_hooks isn't available
let asyncHookWrapper = async <PageContext>(_httpRequestId: number, ret: () => Promise<PageContext>) => ({
pageContextReturn: await ret()
})
// Add node:async_hooks wrapper
function renderPage_addAsyncHookwrapper(wrapper: typeof asyncHookWrapper) {
asyncHookWrapper = wrapper
}

async function renderPageAndPrepare(
pageContextInit: { urlOriginal: string } & Record<string, unknown>,
httpRequestId: number
Expand All @@ -139,8 +143,11 @@ async function renderPageAndPrepare(
try {
await initGlobalContext_renderPage()
} catch (err) {
// Errors are expected since assertUsage() is used in both initGlobalContext_renderPage() and getRenderContext().
// initGlobalContext_renderPage() and getRenderContext() don't call any user hooks => err isn't thrown from user code.
// Errors are expected since assertUsage() is used in initGlobalContext_renderPage() such as:
// ```bash
// Re-build your app (you're using 1.2.3 but your app was built with 1.2.2)
// ```
// initGlobalContext_renderPage() doens't call any user hook => err isn't thrown from user code.
assert(!isAbortError(err))
logRuntimeError(err, httpRequestId)
const pageContextWithError = getPageContextHttpResponseError(err, pageContextInit, null)
Expand Down
5 changes: 2 additions & 3 deletions vike/utils/parseUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export { isBaseServer }
export { assertUrlComponents }
export { createUrlFromComponents }
export type { UrlPublic }
export type { UrlPrivate }

import { slice } from './slice.js'
import { assert, assertUsage } from './assert.js'
Expand Down Expand Up @@ -54,9 +53,9 @@ type UrlPublic = {
/** @deprecated */
searchString: null | string
}
type UrlPrivate = Omit<UrlPublic, 'hashString' | 'searchString'> & { hasBaseServer: boolean }

function parseUrl(url: string, baseServer: string): UrlPrivate {
type UrlInternal = Omit<UrlPublic, 'hashString' | 'searchString'> & { hasBaseServer: boolean }
function parseUrl(url: string, baseServer: string): UrlInternal {
assert(isUrl(url), url)
assert(baseServer.startsWith('/'))

Expand Down

0 comments on commit 4c843cd

Please sign in to comment.