Skip to content
Merged
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
17 changes: 16 additions & 1 deletion packages/void/src/asset-worker-bypass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -285,6 +288,18 @@ function workerPathRules(pattern: string): Array<string> | 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<string>): Array<string> {
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<string>): boolean {
return (
rules.length <= MAX_RUN_WORKER_FIRST_RULES &&
Expand Down
17 changes: 15 additions & 2 deletions packages/void/test/integration/native-asset-cache-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'), '<h1>proxy file</h1>');
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',
'/api/hello',
'/api/:name',
'/proxy/*',
];
const plan = resolveAssetWorkerBypass(
bypassFacts({
patterns: ['/', '/about', '/products', '/contact', '/items/:id', '/api/hello', '/proxy/*'],
patterns: ['/', '/about', ...serverPatterns],
immutableFiles: ['assets/app-abcdefgh.js'],
serverPatterns: ['/products', '/contact', '/items/:id', '/api/hello', '/proxy/*'],
serverPatterns,
}),
)!;
writeFileSync(join(assets, '_headers'), plan.headers!);
Expand Down Expand Up @@ -713,7 +724,9 @@ describe('native output: "static" pages in real Workers', () => {
expect(await action.text()).toBe('action ran');
for (const path of [
'/items/42',
'/items/tags/new',
'/api/hello',
'/api/someone',
'/proxy',
'/proxy/a/b',
'/missing',
Expand Down
41 changes: 41 additions & 0 deletions packages/void/test/unit/asset-worker-bypass.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
'/api/hello',
'/api/:name',
'/api-docs',
'/blog/:slug',
'/blog/tags/:tag',
'/blog/tags/featured',
'/docs/archive',
'/docs/:path{.+}',
'/proxy/*',
'/proxy/health',
],
}),
)?.runWorkerFirst,
).toEqual([
'/__void',
'/__void/*',
'/api-docs',
'/api/*',
'/blog/*',
'/docs/*',
'/proxy',
'/proxy/*',
]);
});

it.each<[string, Partial<AssetWorkerBypassFacts>, number | null]>([
['no hashed files', { immutableFiles: [] }, null],
['an empty assetsDir', { assetsDir: '' }, null],
Expand All @@ -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', '/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(
Expand Down