Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(deploy): pin manifest asset URLs with ?dpl= for Vercel skew protection (#1325)

Sidecf5f8fcc713
diff --git a/package.json b/package.json
index baa9325383..a26c46e4d6 100755
--- a/package.json
+++ b/package.json
@@ -111,7 +111,8 @@
],
"patchedDependencies": {
"@ai-sdk-tools/[email protected]": "patches/@[email protected]",
- "@react-router/[email protected]": "patches/@[email protected]"
+ "@react-router/[email protected]": "patches/@[email protected]",
+ "@vercel/[email protected]": "patches/@[email protected]"
}
}
}
diff --git a/patches/@[email protected] b/patches/@[email protected]
new file mode 100644
index 0000000000..2f8f984629
--- /dev/null
+++ b/patches/@[email protected]
@@ -0,0 +1,185 @@
+diff --git a/edge/entry.server.js b/edge/entry.server.js
+index 7d18c72..de8c399 100644
+--- a/edge/entry.server.js
++++ b/edge/entry.server.js
+@@ -4,9 +4,66 @@ import { renderToReadableStream, } from 'react-dom/server';
+ import { ServerRouter } from 'react-router';
+ const vercelDeploymentId = process.env.VERCEL_DEPLOYMENT_ID;
+ const vercelSkewProtectionEnabled = process.env.VERCEL_SKEW_PROTECTION_ENABLED === '1';
++/**
++ * Appends `?dpl=<deploymentId>` to an asset URL so Vercel's edge can route
++ * the request to the deployment that produced the asset, preventing 404s
++ * when a cached HTML page references assets from a previous deployment.
++ */
++function appendDpl(url, deploymentId) {
++ if (!url || url.includes('?dpl=') || url.includes('&dpl=')) {
++ return url;
++ }
++ return url.includes('?')
++ ? `${url}&dpl=${deploymentId}`
++ : `${url}?dpl=${deploymentId}`;
++}
++/**
++ * Returns a new `AssetsManifest` where every script module URL, import URL,
++ * CSS URL, and the manifest's own URL have `?dpl=<deploymentId>` appended.
++ *
++ * This ensures that `<Scripts>` and `<Links>` emitted by `<ServerRouter>` all
++ * carry the deployment ID, allowing Vercel's Skew Protection to serve them
++ * from the correct deployment. Unlike the browser-global `__vdpl` cookie
++ * (which a newer tab overwrites), the pinned URLs are baked into each page,
++ * so a stale page keeps resolving its own deployment's assets.
++ */
++function pinManifestAssets(manifest, deploymentId) {
++ const pin = (url) => appendDpl(url, deploymentId);
++ return {
++ ...manifest,
++ entry: {
++ ...manifest.entry,
++ module: pin(manifest.entry.module),
++ imports: manifest.entry.imports.map(pin),
++ },
++ routes: Object.fromEntries(Object.entries(manifest.routes).map(([id, route]) => [
++ id,
++ route
++ ? {
++ ...route,
++ module: pin(route.module),
++ imports: route.imports?.map(pin),
++ css: route.css?.map(pin),
++ }
++ : route,
++ ])),
++ // Pin the manifest's own resource URL for skew protection
++ url: pin(manifest.url),
++ };
++}
+ export async function handleRequest(request, responseStatusCode, responseHeaders, routerContext, _loadContext, options) {
++ // When Vercel Skew Protection is enabled, pin all asset URLs in the
++ // manifest to the current deployment via `?dpl=<deploymentId>`, so
++ // stale HTML pages can resolve their assets against the deployment
++ // they were generated from.
++ const effectiveContext = vercelSkewProtectionEnabled && vercelDeploymentId
++ ? {
++ ...routerContext,
++ manifest: pinManifestAssets(routerContext.manifest, vercelDeploymentId),
++ }
++ : routerContext;
+ const body = await renderToReadableStream(createElement(ServerRouter, {
+- context: routerContext,
++ context: effectiveContext,
+ url: request.url,
+ nonce: options?.nonce,
+ }), {
+@@ -21,8 +78,14 @@ export async function handleRequest(request, responseStatusCode, responseHeaders
+ await body.allReady;
+ }
+ responseHeaders.set('Content-Type', 'text/html');
++ // Carbon divergence from vercel/vercel#16649: keep the __vdpl cookie
++ // alongside query pinning. Query pinning only covers manifest-derived
++ // asset URLs; the cookie is what pins single-fetch `.data` loader/action
++ // requests to the matching deployment. Upstream dropped it to keep SSR
++ // responses CDN-cacheable, which doesn't apply to Carbon's authenticated,
++ // per-user SSR responses.
+ if (vercelSkewProtectionEnabled && vercelDeploymentId) {
+- responseHeaders.append('Set-Cookie', `__vdpl=${vercelDeploymentId}; HttpOnly`);
++ responseHeaders.append('Set-Cookie', `__vdpl=${vercelDeploymentId}; Path=/; HttpOnly`);
+ }
+ return new Response(body, {
+ headers: responseHeaders,
+diff --git a/entry.server.js b/entry.server.js
+index 5b6ab35..5c666c5 100644
+--- a/entry.server.js
++++ b/entry.server.js
+@@ -7,17 +7,74 @@ import { ServerRouter } from 'react-router';
+ export const streamTimeout = 5_000;
+ const vercelDeploymentId = process.env.VERCEL_DEPLOYMENT_ID;
+ const vercelSkewProtectionEnabled = process.env.VERCEL_SKEW_PROTECTION_ENABLED === '1';
++/**
++ * Appends `?dpl=<deploymentId>` to an asset URL so Vercel's edge can route
++ * the request to the deployment that produced the asset, preventing 404s
++ * when a cached HTML page references assets from a previous deployment.
++ */
++function appendDpl(url, deploymentId) {
++ if (!url || url.includes('?dpl=') || url.includes('&dpl=')) {
++ return url;
++ }
++ return url.includes('?')
++ ? `${url}&dpl=${deploymentId}`
++ : `${url}?dpl=${deploymentId}`;
++}
++/**
++ * Returns a new `AssetsManifest` where every script module URL, import URL,
++ * CSS URL, and the manifest's own URL have `?dpl=<deploymentId>` appended.
++ *
++ * This ensures that `<Scripts>` and `<Links>` emitted by `<ServerRouter>` all
++ * carry the deployment ID, allowing Vercel's Skew Protection to serve them
++ * from the correct deployment. Unlike the browser-global `__vdpl` cookie
++ * (which a newer tab overwrites), the pinned URLs are baked into each page,
++ * so a stale page keeps resolving its own deployment's assets.
++ */
++function pinManifestAssets(manifest, deploymentId) {
++ const pin = (url) => appendDpl(url, deploymentId);
++ return {
++ ...manifest,
++ entry: {
++ ...manifest.entry,
++ module: pin(manifest.entry.module),
++ imports: manifest.entry.imports.map(pin),
++ },
++ routes: Object.fromEntries(Object.entries(manifest.routes).map(([id, route]) => [
++ id,
++ route
++ ? {
++ ...route,
++ module: pin(route.module),
++ imports: route.imports?.map(pin),
++ css: route.css?.map(pin),
++ }
++ : route,
++ ])),
++ // Pin the manifest's own resource URL for skew protection
++ url: pin(manifest.url),
++ };
++}
+ export function handleRequest(request, responseStatusCode, responseHeaders, routerContext, _loadContext, options) {
+ return new Promise((resolve, reject) => {
+ let shellRendered = false;
+ const userAgent = request.headers.get('user-agent');
++ // When Vercel Skew Protection is enabled, pin all asset URLs in the
++ // manifest to the current deployment via `?dpl=<deploymentId>`, so
++ // stale HTML pages can resolve their assets against the deployment
++ // they were generated from.
++ const effectiveContext = vercelSkewProtectionEnabled && vercelDeploymentId
++ ? {
++ ...routerContext,
++ manifest: pinManifestAssets(routerContext.manifest, vercelDeploymentId),
++ }
++ : routerContext;
+ // Ensure requests from bots and SPA Mode renders wait for all content to load before responding
+ // https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation
+ const readyOption = (userAgent && isbot(userAgent)) || routerContext.isSpaMode
+ ? 'onAllReady'
+ : 'onShellReady';
+ const { pipe, abort } = renderToPipeableStream(createElement(ServerRouter, {
+- context: routerContext,
++ context: effectiveContext,
+ url: request.url,
+ nonce: options?.nonce,
+ }), {
+@@ -27,8 +84,15 @@ export function handleRequest(request, responseStatusCode, responseHeaders, rout
+ const body = new PassThrough();
+ const stream = createReadableStreamFromReadable(body);
+ responseHeaders.set('Content-Type', 'text/html');
++ // Carbon divergence from vercel/vercel#16649: keep the __vdpl
++ // cookie alongside query pinning. Query pinning only covers
++ // manifest-derived asset URLs; the cookie is what pins
++ // single-fetch `.data` loader/action requests to the matching
++ // deployment. Upstream dropped it to keep SSR responses
++ // CDN-cacheable, which doesn't apply to Carbon's authenticated,
++ // per-user SSR responses.
+ if (vercelSkewProtectionEnabled && vercelDeploymentId) {
+- responseHeaders.append('Set-Cookie', `__vdpl=${vercelDeploymentId}; HttpOnly`);
++ responseHeaders.append('Set-Cookie', `__vdpl=${vercelDeploymentId}; Path=/; HttpOnly`);
+ }
+ resolve(new Response(stream, {
+ headers: responseHeaders,
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index a256831537..d403230c05 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -294,6 +294,9 @@ patchedDependencies:
'@react-router/[email protected]':
hash: 77f56df34c4642159bb889b71903a3953827448e994174779d32db487727da95
path: patches/@[email protected]
+ '@vercel/[email protected]':
+ hash: fdc988df95f5639f888c552079014d0e615cb4c762f8c25a16d43e4498e21a6a
+ path: patches/@[email protected]
importers:
@@ -453,7 +456,7 @@ importers:
'@vercel/react-router':
specifier: 'catalog:'
cookie:
specifier: 'catalog:'
version: 1.1.1
@@ -694,7 +697,7 @@ importers:
'@react-router/remix-routes-option-adapter':
specifier: 'catalog:'
'@react-router/serve':
specifier: 'catalog:'
@@ -739,7 +742,7 @@ importers:
'@vercel/react-router':
specifier: 'catalog:'
'@visx/responsive':
specifier: 2.17.0
version: 2.17.0([email protected])
@@ -784,7 +787,7 @@ importers:
inngest:
specifier: ^3.52.7
intl-parse-accept-language:
specifier: 'catalog:'
version: 1.0.0
@@ -974,16 +977,16 @@ importers:
'@lingui/vite-plugin':
specifier: 'catalog:'
'@react-router/dev':
specifier: 'catalog:'
'@tailwindcss/typography':
specifier: 'catalog:'
version: 0.5.19([email protected])
'@tailwindcss/vite':
specifier: 'catalog:'
'@types/d3':
specifier: 7.4.3
version: 7.4.3
@@ -1043,13 +1046,13 @@ importers:
version: 5.8.3
vite:
specifier: 'catalog:'
vite-plugin-babel-macros:
specifier: 'catalog:'
vitest:
specifier: 'catalog:'
apps/mes:
dependencies:
@@ -1133,7 +1136,7 @@ importers:
'@lingui/vite-plugin':
specifier: 'catalog:'
'@nanostores/react':
specifier: 'catalog:'
@@ -1145,10 +1148,10 @@ importers:
'@react-router/dev':
specifier: 'catalog:'
'@react-router/remix-routes-option-adapter':
specifier: 'catalog:'
'@react-router/serve':
specifier: 'catalog:'
@@ -1169,7 +1172,7 @@ importers:
version: 0.5.19([email protected])
'@tailwindcss/vite':
specifier: 'catalog:'
'@tanstack/react-query':
specifier: 5.97.0
version: 5.97.0([email protected])
@@ -1214,7 +1217,7 @@ importers:
'@vercel/react-router':
specifier: 'catalog:'
'@xyflow/react':
specifier: 12.10.2
@@ -1344,13 +1347,13 @@ importers:
version: 5.8.3
vite:
specifier: 'catalog:'
vite-plugin-babel-macros:
specifier: 'catalog:'
vitest:
specifier: 'catalog:'
wait-on:
specifier: 'catalog:'
version: 7.2.0
@@ -1410,7 +1413,7 @@ importers:
'@vercel/react-router':
specifier: 'catalog:'
cookie:
specifier: 'catalog:'
version: 1.1.1
@@ -1553,7 +1556,7 @@ importers:
version: 2.80.0
'@vercel/react-router':
specifier: 'catalog:'
non.geist:
specifier: 'catalog:'
version: 1.0.4
@@ -1632,7 +1635,7 @@ importers:
version: 2.80.0
'@vercel/react-router':
specifier: 'catalog:'
nanoid:
specifier: 'catalog:'
version: 5.1.7
@@ -1820,7 +1823,7 @@ importers:
'@vercel/react-router':
specifier: 'catalog:'
cookie:
specifier: 'catalog:'
version: 1.1.1
@@ -24388,7 +24391,7 @@ snapshots:
'@vercel/[email protected]': {}
dependencies:
@@ -24398,7 +24401,7 @@ snapshots:
react-dom: 18.3.1([email protected])
ts-morph: 12.0.0
dependencies:
@@ -27127,44 +27130,6 @@ snapshots:
- dependencies:
- '@bufbuild/protobuf': 2.11.0
- '@inngest/ai': 0.1.7
- '@jpwilliams/waitgroup': 2.1.1
- '@opentelemetry/api': 1.9.0
- '@opentelemetry/auto-instrumentations-node': 0.72.0(@opentelemetry/[email protected])(@opentelemetry/[email protected](@opentelemetry/[email protected]))
- '@opentelemetry/context-async-hooks': 2.6.1(@opentelemetry/[email protected])
- '@opentelemetry/exporter-trace-otlp-http': 0.214.0(@opentelemetry/[email protected])
- '@opentelemetry/instrumentation': 0.214.0(@opentelemetry/[email protected])
- '@opentelemetry/resources': 2.6.1(@opentelemetry/[email protected])
- '@opentelemetry/sdk-trace-base': 2.6.1(@opentelemetry/[email protected])
- '@standard-schema/spec': 1.1.0
- '@traceloop/instrumentation-anthropic': 0.20.0
- '@types/debug': 4.1.13
- '@types/ms': 2.1.0
- canonicalize: 1.0.8
- chalk: 4.1.2
- cross-fetch: 4.1.0([email protected])
- debug: 4.4.3([email protected])
- hash.js: 1.1.7
- json-stringify-safe: 5.0.1
- ms: 2.1.3
- serialize-error-cjs: 0.1.4
- strip-ansi: 5.2.0
- temporal-polyfill: 0.2.5
- ulid: 2.4.0
- zod: 3.25.76
- optionalDependencies:
- express: 4.22.1
- hono: 4.12.14
- typescript: 5.8.3
- transitivePeerDependencies:
- - '@opentelemetry/core'
- - encoding
- - supports-color
-
dependencies:
'@bufbuild/protobuf': 2.11.0