Skip to content

Commit

Permalink
Add back pageContext function (#28)
Browse files Browse the repository at this point in the history
feat: add back `pageContext` function
  • Loading branch information
magne4000 authored Dec 9, 2024
1 parent 98f3d38 commit cbb8e51
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 139 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
### BREAKING CHANGES

* cache related options have been removed
* the `pageContext` setting has been removed in favor of universal-middleware context
* ~~the `pageContext` setting has been removed in favor of universal-middleware context~~
* this breaking change has been reverted in versions >= 0.2.4
* `vike-node/connect` replaced by `vike-node/express`
* The `vike` middleware is now only exported as the default export:
* ```diff
Expand Down
25 changes: 20 additions & 5 deletions packages/vike-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

# `vike-node`

> [!WARNING]
> **BETA**
> This package will have relatively frequent breaking changes
Server integration for Vike.

With this extension, your server code is transpiled with Vite.
Expand Down Expand Up @@ -243,17 +247,28 @@ function startServer() {

<br/>

## Custom `pageContext`:

## Custom `pageContext`
You can define custom [pageContext](https://vike.dev/pageContext) properties:

`vike-node` uses [universal-middleware](https://universal-middleware.dev/) and automatically adds the universal context to [`pageContext`](https://vike.dev/pageContext).
> [!NOTE]
> `vike-node` uses [universal-middleware](https://universal-middleware.dev/)
> and automatically adds the universal context to [`pageContext`](https://vike.dev/pageContext).
If you need custom properties to be available in `pageContext`,
you can [create a universal context middleware](https://universal-middleware.dev/recipes/context-middleware#updating-the-context) and attach it to your server.
```ts
app.use(
vike({
pageContext(req: IncomingMessage) {
return {
user: req.user
}
}
})
)
```

<br/>


## Standalone build

You can enable standalone builds by setting `standalone` to `true`.
Expand Down
8 changes: 4 additions & 4 deletions packages/vike-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
"dependencies": {
"@brillout/picocolors": "^1.0.14",
"@nitedani/shrink-ray-current": "^4.3.0",
"@universal-middleware/core": "^0.2.13",
"@universal-middleware/compress": "^0.2.1",
"@universal-middleware/core": "^0.3.2",
"@universal-middleware/compress": "^0.2.6",
"@vercel/nft": "^0.26.5",
"esbuild": "^0.19.0 || ^0.20.0 || ^0.21.0 || ^0.22.0 || ^0.23.0 || ^0.24.0",
"resolve-from": "^5.0.0",
Expand All @@ -100,7 +100,7 @@
"hono": "^4.6.3",
"tsup": "^8.3.0",
"typescript": "^5.5.4",
"universal-middleware": "^0.4.0",
"universal-middleware": "^0.5.3",
"vike": "^0.4.198",
"vite": "^6.0.2"
},
Expand All @@ -109,4 +109,4 @@
],
"repository": "github:vikejs/vike-node",
"license": "MIT"
}
}
4 changes: 2 additions & 2 deletions packages/vike-node/src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { IncomingMessage, ServerResponse } from 'http'
export type HeadersProvided = Record<string, string | string[] | undefined> | Headers
export type VikeHttpResponse = Awaited<ReturnType<typeof import('vike/server').renderPage>>['httpResponse']
export type NextFunction = (err?: unknown) => void
export type VikeOptions = {
pageContext?: Record<string, any>
export type VikeOptions<PlatformRequest = unknown> = {
pageContext?: ((req: PlatformRequest) => Record<string, any> | Promise<Record<string, any>>) | Record<string, any>
compress?: boolean | 'static'
static?: boolean | string | { root?: string; cache?: boolean }
onError?: (err: unknown) => void
Expand Down
18 changes: 13 additions & 5 deletions packages/vike-node/src/runtime/vike-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ import { globalStore } from './globalStore.js'
import type { ConnectMiddleware, VikeHttpResponse, VikeOptions } from './types.js'
import { parseHeaders } from './utils/header-utils.js'

export { renderPage }

async function renderPage({
url,
headers,
runtimeRequest,
options
}: {
url: string
headers: [string, string][]
runtimeRequest: unknown
options: VikeOptions
}): Promise<VikeHttpResponse> {
let pageContextInit: Record<string, any> = {}
if (typeof options?.pageContext === 'function') {
pageContextInit = await options.pageContext(runtimeRequest)
} else if (options?.pageContext) {
pageContextInit = options.pageContext
}

const pageContext = await _renderPage({
...options?.pageContext,
...pageContextInit,
urlOriginal: url,
headersOriginal: headers
})
Expand Down Expand Up @@ -49,8 +56,8 @@ export const compressMiddleware = ((options?) => async (request, _context, runti
}
}) satisfies Get<[options: VikeOptions], UniversalMiddleware>

export const renderPageHandler = ((options?) => async (request, context, runtime: any) => {
const nodeReq: IncomingMessage | undefined = runtime.req
export const renderPageHandler = ((options?) => async (request, context, runtime) => {
const nodeReq: IncomingMessage | undefined = 'req' in runtime ? runtime.req : undefined
let staticConfig: false | { root: string; cache: boolean } = false
let staticMiddleware: ConnectMiddleware | undefined

Expand Down Expand Up @@ -87,6 +94,7 @@ export const renderPageHandler = ((options?) => async (request, context, runtime
const response = await renderPage({
url: request.url,
headers: parseHeaders(request.headers),
runtimeRequest: runtime.adapter in runtime ? (runtime as any)[runtime.adapter] : request,
options: {
...options,
pageContext: {
Expand Down
Loading

0 comments on commit cbb8e51

Please sign in to comment.