Files
cloudflare__vinext/tests/env-test-helpers.ts
T
Nathan Nguyen 4aa7663084 fix(app-router): hard navigate stale RSC build payloads (#1178)
* fix(app-router): hard navigate stale RSC build payloads

App Router soft navigation currently accepts any valid RSC payload after deploy. That lets stale tabs decode RSC responses produced by a different build, which can break client navigation when the server and browser artifacts no longer match.

The missing invariant was that every RSC response consumed by the browser must identify the build that produced it, and the browser must reject payloads from a different build before handing them to React.

Emit a vinext-owned build ID header on App Router RSC responses, preserve it in prefetched and visited response snapshots, and hard navigate when a response is missing or mismatches the current client build ID.

* chore: fix e2e fixture hook lint suppression

* chore: avoid hook-like fixture callback name

* fix(app-router): preserve build IDs for cached RSC payloads

* fix(app-router): use public RSC compatibility IDs
2026-05-15 12:01:33 +01:00

39 lines
834 B
TypeScript

export function withEnvVar<T>(
name: string,
value: string | undefined,
run: () => Promise<T>,
): Promise<T>;
export function withEnvVar<T>(name: string, value: string | undefined, run: () => T): T;
export function withEnvVar<T>(
name: string,
value: string | undefined,
run: () => T | Promise<T>,
): T | Promise<T> {
const previous = process.env[name];
if (value === undefined) {
delete process.env[name];
} else {
process.env[name] = value;
}
const restore = () => {
if (previous === undefined) {
delete process.env[name];
} else {
process.env[name] = previous;
}
};
try {
const result = run();
if (result instanceof Promise) {
return result.finally(restore);
}
restore();
return result;
} catch (error) {
restore();
throw error;
}
}