Skip to content

Commit 8f20ece

Browse files
morinokamiclaude
andcommitted
fix(deploy): leave out run_worker_first rules that a wider rule already covers
An `output: "static"` Pages build lists only the paths with server code in `run_worker_first`, each widened to its static prefix before the first parameter. When one server path sits under another one's `/prefix/*` rule, both rules were written: ``` pages/blog/[slug].tsx -> /blog/* pages/blog/tags/[tag].tsx -> /blog/tags/* ``` Cloudflare rejects that list ("'/blog/tags/*': rule '/blog/*' makes it redundant"). `vp build` succeeded, but `vp preview` exited and `wrangler deploy` refused the output. A dynamic API route next to the starter's `routes/api/hello.ts` (`/api/*` and `/api/hello`) failed the same way. This affected such apps since 1.6.0 (#660). The planner now leaves out every rule that a `/prefix/*` rule in the list already matches, including server paths under the assets folder rule that a build adds when it cannot list its hashed files. The wider rule sends the same requests to the Worker, so routing does not change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7848d23 commit 8f20ece

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

‎packages/void/src/asset-worker-bypass.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ function resolveStaticPageBypass(
253253
} else if (files.length > 0) {
254254
headers = renderGeneratedAssetHeaders(files);
255255
}
256-
const runWorkerFirst = [...VOID_INTERNAL_WORKER_FIRST, ...[...rules].sort()];
256+
const runWorkerFirst = withoutRedundantRules([
257+
...VOID_INTERNAL_WORKER_FIRST,
258+
...[...rules].sort(),
259+
]);
257260
return fitsRunWorkerFirstLimits(runWorkerFirst) ? { runWorkerFirst, headers } : null;
258261
}
259262

@@ -285,6 +288,18 @@ function workerPathRules(pattern: string): Array<string> | null {
285288
: [rule];
286289
}
287290

291+
/**
292+
* Leaves out each rule that a `/prefix/*` rule in the list already matches.
293+
* Cloudflare rejects a list with such a rule ("rule '/blog/*' makes it
294+
* redundant"), and the wider rule already sends those paths to the Worker.
295+
*/
296+
function withoutRedundantRules(rules: Array<string>): Array<string> {
297+
const prefixes = rules.filter((rule) => rule.endsWith('/*')).map((rule) => rule.slice(0, -1));
298+
return rules.filter(
299+
(rule) => !prefixes.some((prefix) => rule !== prefix + '*' && rule.startsWith(prefix)),
300+
);
301+
}
302+
288303
function fitsRunWorkerFirstLimits(rules: Array<string>): boolean {
289304
return (
290305
rules.length <= MAX_RUN_WORKER_FIRST_RULES &&

‎packages/void/test/integration/native-asset-cache-policy.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,22 @@ describe('native output: "static" pages in real Workers', () => {
643643
// The `/proxy/*` route also answers `/proxy`, so this file must not.
644644
writeFileSync(join(assets, 'proxy.html'), '<h1>proxy file</h1>');
645645
writeFileSync(join(assets, 'assets/app-abcdefgh.js'), 'export const value = 1;');
646+
// `/items/tags/:tag` and `/api/hello` sit under the rules of `/items/:id` and
647+
// `/api/:name`. Miniflare rejects a list that names them too, as Cloudflare does.
648+
const serverPatterns = [
649+
'/products',
650+
'/contact',
651+
'/items/:id',
652+
'/items/tags/:tag',
653+
'/api/hello',
654+
'/api/:name',
655+
'/proxy/*',
656+
];
646657
const plan = resolveAssetWorkerBypass(
647658
bypassFacts({
648-
patterns: ['/', '/about', '/products', '/contact', '/items/:id', '/api/hello', '/proxy/*'],
659+
patterns: ['/', '/about', ...serverPatterns],
649660
immutableFiles: ['assets/app-abcdefgh.js'],
650-
serverPatterns: ['/products', '/contact', '/items/:id', '/api/hello', '/proxy/*'],
661+
serverPatterns,
651662
}),
652663
)!;
653664
writeFileSync(join(assets, '_headers'), plan.headers!);
@@ -713,7 +724,9 @@ describe('native output: "static" pages in real Workers', () => {
713724
expect(await action.text()).toBe('action ran');
714725
for (const path of [
715726
'/items/42',
727+
'/items/tags/new',
716728
'/api/hello',
729+
'/api/someone',
717730
'/proxy',
718731
'/proxy/a/b',
719732
'/missing',

‎packages/void/test/unit/asset-worker-bypass.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,36 @@ describe('resolveAssetWorkerBypass for output: "static" Pages builds', () => {
170170
).toEqual(['/__void', '/__void/*', '/docs/*', '/proxy', '/proxy/*']);
171171
});
172172

173+
it('leaves out rules that a wider prefix rule already matches', () => {
174+
expect(
175+
resolveAssetWorkerBypass(
176+
facts({
177+
serverPatterns: [
178+
'/api/hello',
179+
'/api/:name',
180+
'/api-docs',
181+
'/blog/:slug',
182+
'/blog/tags/:tag',
183+
'/blog/tags/featured',
184+
'/docs/archive',
185+
'/docs/:path{.+}',
186+
'/proxy/*',
187+
'/proxy/health',
188+
],
189+
}),
190+
)?.runWorkerFirst,
191+
).toEqual([
192+
'/__void',
193+
'/__void/*',
194+
'/api-docs',
195+
'/api/*',
196+
'/blog/*',
197+
'/docs/*',
198+
'/proxy',
199+
'/proxy/*',
200+
]);
201+
});
202+
173203
it.each<[string, Partial<AssetWorkerBypassFacts>, number | null]>([
174204
['no hashed files', { immutableFiles: [] }, null],
175205
['an empty assetsDir', { assetsDir: '' }, null],
@@ -195,6 +225,17 @@ describe('resolveAssetWorkerBypass for output: "static" Pages builds', () => {
195225
});
196226
});
197227

228+
it('leaves out a server path that the assets folder rule already matches', () => {
229+
expect(
230+
resolveAssetWorkerBypass(
231+
facts({ serverPatterns: ['/products', '/assets/guide'], immutableFiles: files(101) }),
232+
),
233+
).toEqual({
234+
runWorkerFirst: ['/__void', '/__void/*', '/assets/*', '/products'],
235+
headers: null,
236+
});
237+
});
238+
198239
it('keeps every path on the Worker when no rule can cover the hashed files', () => {
199240
expect(
200241
resolveAssetWorkerBypass(

0 commit comments

Comments
 (0)