Skip to content

Commit

Permalink
feat: Add runtime to pageContext (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 authored Dec 11, 2024
1 parent 1720ee4 commit e55ac44
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 5 additions & 4 deletions packages/vike-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
<br/>

Expand Down
2 changes: 1 addition & 1 deletion packages/vike-node/src/runtime/vike-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
13 changes: 11 additions & 2 deletions packages/vike-node/src/vike.handler.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit e55ac44

Please sign in to comment.