diff --git a/packages/vike-node/README.md b/packages/vike-node/README.md index 395de5b..2626e5d 100644 --- a/packages/vike-node/README.md +++ b/packages/vike-node/README.md @@ -265,10 +265,11 @@ app.use( ``` > [!NOTE] -> `vike-node` uses [universal-middleware](https://universal-middleware.dev/) -> and automatically adds the universal context to [`pageContext`](https://vike.dev/pageContext). -> -> You can find details on `RuntimeAdapter` in the [documentation](https://universal-middleware.dev/reference/runtime-adapter). +> See [`RuntimeAdapter`](https://universal-middleware.dev/reference/runtime-adapter) (`vike-node` uses [universal-middleware](https://universal-middleware.dev/) under the hood). + +> [!NOTE] +> The `runtime` object is also available at `pageContext.runtime` so that, even without the custom `pageContext` function above, +> you can retrieve `pageContext.runtime.req.user` in Vike hooks and UI components (with [`usePageContext()`](https://vike.dev/usePageContext)).
diff --git a/packages/vike-node/src/runtime/vike-handler.ts b/packages/vike-node/src/runtime/vike-handler.ts index 054717e..24d083f 100644 --- a/packages/vike-node/src/runtime/vike-handler.ts +++ b/packages/vike-node/src/runtime/vike-handler.ts @@ -101,7 +101,7 @@ export const renderPageHandler = ((options?) => async (request, context, runtime }) } - const pageContextInit = { ...context, ...runtime, urlOriginal: request.url, headersOriginal: request.headers } + const pageContextInit = { ...context, runtime, urlOriginal: request.url, headersOriginal: request.headers } const response = await renderPage({ url: request.url, headers: parseHeaders(request.headers), diff --git a/packages/vike-node/src/vike.handler.ts b/packages/vike-node/src/vike.handler.ts index 1d619ab..44b5f7d 100644 --- a/packages/vike-node/src/vike.handler.ts +++ b/packages/vike-node/src/vike.handler.ts @@ -1,7 +1,16 @@ -import { Get, UniversalHandler, pipe } from '@universal-middleware/core' -import { VikeOptions } from './runtime/types.js' +import { Get, type RuntimeAdapter, type UniversalHandler, pipe } from '@universal-middleware/core' +import type { VikeOptions } from './runtime/types.js' import { compressMiddleware, renderPageHandler } from './runtime/vike-handler.js' +// https://vike.dev/pageContext#typescript +declare global { + namespace Vike { + interface PageContext { + runtime: RuntimeAdapter + } + } +} + const renderPageUniversal = ((options?) => pipe(compressMiddleware(options), renderPageHandler(options))) satisfies Get< [options: VikeOptions], UniversalHandler