diff --git a/packages/void/src/asset-worker-bypass.ts b/packages/void/src/asset-worker-bypass.ts index 042a55d89..efe88bef9 100644 --- a/packages/void/src/asset-worker-bypass.ts +++ b/packages/void/src/asset-worker-bypass.ts @@ -253,7 +253,10 @@ function resolveStaticPageBypass( } else if (files.length > 0) { headers = renderGeneratedAssetHeaders(files); } - const runWorkerFirst = [...VOID_INTERNAL_WORKER_FIRST, ...[...rules].sort()]; + const runWorkerFirst = withoutRedundantRules([ + ...VOID_INTERNAL_WORKER_FIRST, + ...[...rules].sort(), + ]); return fitsRunWorkerFirstLimits(runWorkerFirst) ? { runWorkerFirst, headers } : null; } @@ -285,6 +288,18 @@ function workerPathRules(pattern: string): Array | null { : [rule]; } +/** + * Leaves out each rule that a `/prefix/*` rule in the list already matches. + * Cloudflare rejects a list with such a rule ("rule '/blog/*' makes it + * redundant"), and the wider rule already sends those paths to the Worker. + */ +function withoutRedundantRules(rules: Array): Array { + const prefixes = rules.filter((rule) => rule.endsWith('/*')).map((rule) => rule.slice(0, -1)); + return rules.filter( + (rule) => !prefixes.some((prefix) => rule !== prefix + '*' && rule.startsWith(prefix)), + ); +} + function fitsRunWorkerFirstLimits(rules: Array): boolean { return ( rules.length <= MAX_RUN_WORKER_FIRST_RULES && diff --git a/packages/void/test/integration/native-asset-cache-policy.test.ts b/packages/void/test/integration/native-asset-cache-policy.test.ts index d62836ad5..0f79c3d53 100644 --- a/packages/void/test/integration/native-asset-cache-policy.test.ts +++ b/packages/void/test/integration/native-asset-cache-policy.test.ts @@ -643,11 +643,22 @@ describe('native output: "static" pages in real Workers', () => { // The `/proxy/*` route also answers `/proxy`, so this file must not. writeFileSync(join(assets, 'proxy.html'), '

proxy file

'); writeFileSync(join(assets, 'assets/app-abcdefgh.js'), 'export const value = 1;'); + // `/items/tags/:tag` and `/api/hello` sit under the rules of `/items/:id` and + // `/api/:name`. Miniflare rejects a list that names them too, as Cloudflare does. + const serverPatterns = [ + '/products', + '/contact', + '/items/:id', + '/items/tags/:tag', + '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/api/hello', + '/api/:name', + '/proxy/*', + ]; const plan = resolveAssetWorkerBypass( bypassFacts({ - patterns: ['/', '/about', '/products', '/contact', '/items/:id', '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/api/hello', '/proxy/*'], + patterns: ['/', '/about', ...serverPatterns], immutableFiles: ['assets/app-abcdefgh.js'], - serverPatterns: ['/products', '/contact', '/items/:id', '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/api/hello', '/proxy/*'], + serverPatterns, }), )!; writeFileSync(join(assets, '_headers'), plan.headers!); @@ -713,7 +724,9 @@ describe('native output: "static" pages in real Workers', () => { expect(await action.text()).toBe('action ran'); for (const path of [ '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/items/42', + '/items/tags/new', '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/api/hello', + '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/api/someone', '/proxy', '/proxy/a/b', '/missing', diff --git a/packages/void/test/unit/asset-worker-bypass.test.ts b/packages/void/test/unit/asset-worker-bypass.test.ts index 4ef178c6f..c9e565c15 100644 --- a/packages/void/test/unit/asset-worker-bypass.test.ts +++ b/packages/void/test/unit/asset-worker-bypass.test.ts @@ -170,6 +170,36 @@ describe('resolveAssetWorkerBypass for output: "static" Pages builds', () => { ).toEqual(['/__void', '/__void/*', '/docs/*', '/proxy', '/proxy/*']); }); + it('leaves out rules that a wider prefix rule already matches', () => { + expect( + resolveAssetWorkerBypass( + facts({ + serverPatterns: [ + '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/api/hello', + '/api/:name', + '/api-docs', + '/blog/:slug', + '/blog/tags/:tag', + '/blog/tags/featured', + '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/docs/archive', + '/docs/:path{.+}', + '/proxy/*', + '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/proxy/health', + ], + }), + )?.runWorkerFirst, + ).toEqual([ + '/__void', + '/__void/*', + '/api-docs', + '/api/*', + '/blog/*', + '/docs/*', + '/proxy', + '/proxy/*', + ]); + }); + it.each<[string, Partial, number | null]>([ ['no hashed files', { immutableFiles: [] }, null], ['an empty assetsDir', { assetsDir: '' }, null], @@ -195,6 +225,17 @@ describe('resolveAssetWorkerBypass for output: "static" Pages builds', () => { }); }); + it('leaves out a server path that the assets folder rule already matches', () => { + expect( + resolveAssetWorkerBypass( + facts({ serverPatterns: ['/products', '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/assets/guide'], immutableFiles: files(101) }), + ), + ).toEqual({ + runWorkerFirst: ['/__void', '/__void/*', '/assets/*', '/products'], + headers: null, + }); + }); + it('keeps every path on the Worker when no rule can cover the hashed files', () => { expect( resolveAssetWorkerBypass(