From abe5988c66c476809cf37da937381f127e0bc665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Charles?= Date: Wed, 11 Dec 2024 16:34:57 +0100 Subject: [PATCH 1/3] feat: Add `runtime` to pageContext --- packages/vike-node/src/runtime/vike-handler.ts | 2 +- packages/vike-node/src/vike.handler.ts | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) 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 From 942be1d694c84cd8d4d95b47c40348c24f806240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Charles?= Date: Wed, 11 Dec 2024 16:39:33 +0100 Subject: [PATCH 2/3] doc: `pageContext.runtime` --- packages/vike-node/README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/vike-node/README.md b/packages/vike-node/README.md index 395de5b..194273c 100644 --- a/packages/vike-node/README.md +++ b/packages/vike-node/README.md @@ -265,11 +265,16 @@ app.use( ``` > [!NOTE] -> `vike-node` uses [universal-middleware](https://universal-middleware.dev/) -> and automatically adds the universal context to [`pageContext`](https://vike.dev/pageContext). +> `vike-node` uses [universal-middleware](https://universal-middleware.dev/) under the hood. > > You can find details on `RuntimeAdapter` in the [documentation](https://universal-middleware.dev/reference/runtime-adapter). +> [!NOTE] +> `runtime` is already available at `pageContext.runtime`. +> +> So even without the custom `pageContext` function above, +> one could also retrieve the user from `pageContext.runtime.req.user` in any Vike hook. +
## Standalone build From 792144a9fc57f72baf2217272d7a3d19e197bf7d Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Wed, 11 Dec 2024 17:01:31 +0100 Subject: [PATCH 3/3] docs: polish --- packages/vike-node/README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/vike-node/README.md b/packages/vike-node/README.md index 194273c..2626e5d 100644 --- a/packages/vike-node/README.md +++ b/packages/vike-node/README.md @@ -265,15 +265,11 @@ app.use( ``` > [!NOTE] -> `vike-node` uses [universal-middleware](https://universal-middleware.dev/) under the hood. -> -> 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] -> `runtime` is already available at `pageContext.runtime`. -> -> So even without the custom `pageContext` function above, -> one could also retrieve the user from `pageContext.runtime.req.user` in any Vike hook. +> 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)).