Skip to content
Open
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
14 changes: 14 additions & 0 deletions docs/guide/pages-routing/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ Production deploys exclude resources used only by `.dev` pages and their compani

The first request receives rendered HTML. Later navigations run the loader and update the page without reloading the document. Form submissions run an action, then refresh page props or redirect.

### After a deploy

An open tab keeps the code it loaded. Deploy while it stays open, and its next navigation asks for chunks that no longer exist.

Void recovers that navigation. The tab sends the build it is running with each page request, and an answer from a different build makes it load the requested page as a document instead of rendering it. The destination keeps its query string and fragment, and the current page stays visible until the load starts.

These never reload the tab:

- A prefetch drops the response and lets the next navigation decide.
- A form submission renders its response, so the action never runs twice. The next navigation reloads if it has to.
- A failed request is not treated as a deploy.

Recovery is on by default. Set `deployRecovery: false` in your framework plugin's options to keep your own [error boundaries](./errors.md) in charge instead.

To opt a specific route out of server-rendered component HTML, export `ssr = false` from its companion `.server.ts` file:

```ts
Expand Down
4 changes: 4 additions & 0 deletions docs/reference/api/pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ interface VoidVueOptions {
hoverDelay?: number; // default: 75 (ms)
cacheFor?: number | string | [string, string]; // default: '30s'
};
deployRecovery?: boolean; // reload a tab whose build is gone. default: true
}
```

Expand Down Expand Up @@ -147,6 +148,7 @@ interface VoidReactOptions {
hoverDelay?: number; // default: 75 (ms)
cacheFor?: number | string | [string, string]; // default: '30s'
};
deployRecovery?: boolean; // reload a tab whose build is gone. default: true
}
```

Expand Down Expand Up @@ -232,6 +234,7 @@ interface VoidSvelteOptions {
hoverDelay?: number; // default: 75 (ms)
cacheFor?: number | string | [string, string]; // default: '30s'
};
deployRecovery?: boolean; // reload a tab whose build is gone. default: true
}
```

Expand Down Expand Up @@ -286,6 +289,7 @@ interface VoidSolidOptions {
hoverDelay?: number; // default: 75 (ms)
cacheFor?: number | string | [string, string]; // default: '30s'
};
deployRecovery?: boolean; // reload a tab whose build is gone. default: true
}
```

Expand Down
8 changes: 7 additions & 1 deletion packages/edge/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export type { ProjectZeroTrustRouting } from './access.ts';
export { PAGES_PROTOCOL_VERSION } from './pages-protocol.ts';
export {
buildHeaders,
isStaleBuild,
pagesPayloadFitsBuild,
PAGES_BUILD_HEADER,
PAGES_PROTOCOL_VERSION,
} from './pages-protocol.ts';
export { redirectToHttps } from './https.ts';
export {
applyHeaderRules,
Expand Down
34 changes: 34 additions & 0 deletions packages/edge/src/pages-protocol.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
/** Current Pages wire version; a missing header identifies legacy clients. */
export const PAGES_PROTOCOL_VERSION = '2';

/**
* The build a request or response belongs to. Unlike the protocol version it
* changes with every deploy, so a tab that outlives a deployment can tell that
* the chunks it still holds are gone.
*/
export const PAGES_BUILD_HEADER = 'X-VoidPages-Build';

/**
* Whether a build id seen on the wire belongs to another deployment. A peer
* that sends no id (an older Void, or a parse of a partial response) is not
* evidence of a new build, so it never counts as a mismatch.
*/
export function isStaleBuild(buildId: string, seen: string | null | undefined): boolean {
return buildId !== '' && seen != null && seen !== buildId;
}

/** Request and response headers that carry a build, empty when none is known. */
export function buildHeaders(buildId: string): Record<string, string> {
return buildId === '' ? {} : { [PAGES_BUILD_HEADER]: buildId };
}

/**
* Whether a cached Pages payload may answer a request. A client that names no
* build cannot tell builds apart, so it keeps the cached payload. A client that
* names one accepts only a payload from that same build, and an entry without an
* id predates ids, so it is not trusted either.
*/
export function pagesPayloadFitsBuild(
cachedBuild: string | null,
requestBuild: string | null,
): boolean {
return requestBuild === null || cachedBuild === requestBuild;
}
35 changes: 32 additions & 3 deletions packages/isr/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { matchSegmentPattern, PAGES_PROTOCOL_VERSION } from '@void/edge';
import {
matchSegmentPattern,
pagesPayloadFitsBuild,
PAGES_BUILD_HEADER,
PAGES_PROTOCOL_VERSION,
} from '@void/edge';
import {
buildVariantOriginalUrl,
decodeIsrCacheEntry,
Expand Down Expand Up @@ -326,14 +331,22 @@ function sharedHtmlCacheHeaders(response: Response, ttl: number, state: 'HIT' |
return headers;
}

function jsonCacheResponse(pageData: string, ttl: number, state: 'HIT' | 'MISS'): Response {
function jsonCacheResponse(
pageData: string,
ttl: number,
state: 'HIT' | 'MISS',
build: string | null,
): Response {
const response = new Response(pageData, {
headers: {
'content-type': 'application/json',
// Match the live Pages JSON response: the client parses JSON only when
// `X-VoidPages` is present, and otherwise reloads the whole page.
'X-VoidPages': 'true',
'X-VoidPages-Version': PAGES_PROTOCOL_VERSION,
// Kept with the payload: a hit for a client from another build must not
// pass off this render's props as its own.
...(build ? { [PAGES_BUILD_HEADER]: build } : {}),
Vary: 'X-VoidPages, X-VoidPages-Version',
'Cache-Control': `public, s-maxage=${ttl}, max-age=0, must-revalidate`,
'x-isr-cache': state,
Expand All @@ -359,14 +372,15 @@ export async function populateJsonEdgeCache(
return;
}
try {
const build = response.headers.get(PAGES_BUILD_HEADER);
const fromHeader = response.headers.get('x-void-page-data');
const pageData = fromHeader
? normalizePageDataForJsonCache(fromHeader)
: extractPageDataFromHtml(await response.clone().text());
if (!pageData) {
return;
}
await cache.put(cacheKey, jsonCacheResponse(pageData, ttl, state));
await cache.put(cacheKey, jsonCacheResponse(pageData, ttl, state, build));
} catch {
// Fail open.
}
Expand Down Expand Up @@ -582,12 +596,27 @@ async function handleIsrInternal(
return null; // fail open → caller renders live
}
if (jsonCached && hasCurrentSharedCachePolicy(jsonCached)) {
// A cached payload from another build would hand this client page props it
// has no components for. Let the app answer, which reloads a stale tab.
if (
!pagesPayloadFitsBuild(
jsonCached.headers.get(PAGES_BUILD_HEADER),
request.headers.get(PAGES_BUILD_HEADER),
)
) {
return render(request);
}
return clientRevalidateResponse(jsonCached);
}
if (jsonCached) {
await safeCacheDelete(cache, jsonKey);
}
const response = await render(request);
// The app answered a stale client with its reload response. That says nothing
// about this path's cached HTML, which other tabs can still use.
if (!response.headers.get('X-VoidPages')) {
return response;
}
const ttl = resolveRevalidateTtl(response, isrTtl);
if (isUncacheableRender(response, ttl, false)) {
ctx.waitUntil(
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu
return createPagesPlugin(pagesDir, {
name: 'void-react:pages',
root,
deployRecovery: options?.deployRecovery,
generateRenderer(scan, isDev) {
return generateRendererModule(scan, pagesDir, isDev);
},
Expand Down
8 changes: 3 additions & 5 deletions packages/react/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ export type VoidReactOptions = Readonly<{
react?: Parameters<typeof react>[0];
viewTransitions?: boolean;
prefetch?: PrefetchConfig;
deployRecovery?: boolean;
}>;

export function voidReact(options?: VoidReactOptions): Array<Plugin> {
const root = process.cwd();
const pagesDir = resolvePagesDir(root);
return [
...react(options?.react),
pagesPlugin(pagesDir, {
root,
viewTransitions: options?.viewTransitions,
prefetch: options?.prefetch,
}),
// Spread so adapter options the pages plugin owns reach it without per-option glue.
pagesPlugin(pagesDir, { ...options, root }),
...islandsPlugin(pagesDir, { root }),
];
}
1 change: 1 addition & 0 deletions packages/solid/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu
return createPagesPlugin(pagesDir, {
name: 'void-solid:pages',
root: options?.root,
deployRecovery: options?.deployRecovery,
generateRenderer(scan) {
return generateRendererModule(scan, pagesDir);
},
Expand Down
8 changes: 3 additions & 5 deletions packages/solid/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type VoidSolidOptions = Readonly<{
solid?: Parameters<typeof solid>[0];
viewTransitions?: boolean;
prefetch?: PrefetchConfig;
deployRecovery?: boolean;
}>;

// The runtime entry exports raw `.tsx` source. Without these hints, Vite's SSR
Expand Down Expand Up @@ -46,11 +47,8 @@ export function voidSolid(options?: VoidSolidOptions): Array<Plugin> {
return [
configPlugin(),
solid({ ssr: true, ...options?.solid }),
pagesPlugin(pagesDir, {
root,
viewTransitions: options?.viewTransitions,
prefetch: options?.prefetch,
}),
// Spread so adapter options the pages plugin owns reach it without per-option glue.
pagesPlugin(pagesDir, { ...options, root }),
...islandsPlugin(pagesDir, { root }),
];
}
1 change: 1 addition & 0 deletions packages/svelte/src/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu
return createPagesPlugin(pagesDir, {
name: 'void-svelte:pages',
root: options?.root,
deployRecovery: options?.deployRecovery,
generateRenderer(scan) {
return generateRendererModule(scan, pagesDir);
},
Expand Down
8 changes: 3 additions & 5 deletions packages/svelte/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ export type VoidSvelteOptions = Readonly<{
svelte?: Parameters<typeof svelte>[0];
viewTransitions?: boolean;
prefetch?: PrefetchConfig;
deployRecovery?: boolean;
}>;

export function voidSvelte(options?: VoidSvelteOptions): Array<Plugin> {
const root = process.cwd();
const pagesDir = resolvePagesDir(root);
return [
...(svelte(options?.svelte) as ReadonlyArray<Plugin>),
pagesPlugin(pagesDir, {
root,
viewTransitions: options?.viewTransitions,
prefetch: options?.prefetch,
}),
// Spread so adapter options the pages plugin owns reach it without per-option glue.
pagesPlugin(pagesDir, { ...options, root }),
...islandsPlugin(pagesDir, { root }),
];
}
7 changes: 7 additions & 0 deletions packages/void/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Constants the build folds into both the client and server bundles, such as
* the Pages build id. Vite replaces each key at build time.
*/
interface ImportMeta {
readonly env: Record<string, unknown>;
}
Loading