Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back pageContext function #28

Merged
merged 9 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading