Skip to content

Commit

Permalink
Reduce amount of rules in routes json file (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
veitbjarsch authored Sep 23, 2024
1 parent 6cc9d05 commit 44dfa99
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-deers-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': minor
---

Added functionality to compare include and exclude rules to reduce the amount of cloudflare rules
35 changes: 33 additions & 2 deletions packages/cloudflare/src/utils/generate-routes-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,31 @@ class PathTrie {
}
}

/**
* The reduce function is used to remove unnecessary paths from the trie.
* It receives a trie node to compare with the current node.
*/
private reduce(compNode: TrieNode, node: TrieNode): void {
if (node.hasWildcardChild || compNode.hasWildcardChild) return;

for (const [segment, childNode] of node.children) {
if (childNode.children.size === 0) continue;

const compChildNode = compNode.children.get(segment);
if (compChildNode === undefined) {
childNode.hasWildcardChild = true;
continue;
}

this.reduce(compChildNode, childNode);
}
}

reduceAllPaths(compTrie: PathTrie): this {
this.reduce(compTrie.root, this.root);
return this;
}

getAllPaths(): [string[][], boolean] {
const allPaths: string[][] = [];
this.dfs(this.root, [], allPaths);
Expand Down Expand Up @@ -244,7 +269,6 @@ export async function createRoutesFile(
for (const includePath of includePaths) {
includeTrie.insert(includePath);
}
const [deduplicatedIncludePaths, includedPathsHaveWildcard] = includeTrie.getAllPaths();

const excludeTrie = new PathTrie();
for (const excludePath of excludePaths) {
Expand All @@ -256,7 +280,14 @@ export async function createRoutesFile(
if (excludePath[0] === '*') continue;
excludeTrie.insert(excludePath);
}
const [deduplicatedExcludePaths, _excludedPathsHaveWildcard] = excludeTrie.getAllPaths();

const [deduplicatedIncludePaths, includedPathsHaveWildcard] = includeTrie
.reduceAllPaths(excludeTrie)
.getAllPaths();

const [deduplicatedExcludePaths, _excludedPathsHaveWildcard] = excludeTrie
.reduceAllPaths(includeTrie)
.getAllPaths();

/**
* Cloudflare allows no more than 100 include/exclude rules combined
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference path="../../.astro/types.d.ts" />
/// <reference types="astro/client" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
export const prerender = true;
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=false;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=false;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=false;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=false;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=false;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=true;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=false;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=true;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=true;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=true;
---

ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender=true;
---

ok
41 changes: 40 additions & 1 deletion packages/cloudflare/test/routes-json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('_routes.json generation', () => {
assert.deepEqual(routes, {
version: 1,
include: ['/*'],
exclude: ['/_astro/*', '/redirectme', '/public.txt', '/a/redirect'],
exclude: ['/_astro/*', '/redirectme', '/public.txt', '/a/*'],
});
});
});
Expand Down Expand Up @@ -146,4 +146,43 @@ describe('_routes.json generation', () => {
});
});
});

describe('with nested on demand and prerendered routes', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/routes-json/', import.meta.url).toString(),
srcDir: './src/reduceComplexity',
adapter: cloudflare({}),
});
await fixture.build();
});

it('reduces the amount of include and exclude entries by applying wildcards wherever possible', async () => {
const _routesJson = await fixture.readFile('/_routes.json');
const routes = JSON.parse(_routesJson);

assert.deepEqual(routes, {
version: 1,
include: [
'/',
'/_image',
'/dynamicPages/*',
'/mixedPages/dynamic',
'/mixedPages/subfolder/dynamic',
],
exclude: [
'/_astro/*',
'/redirectme',
'/public.txt',
'/a/*',
'/404',
'/mixedPages/static',
'/mixedPages/subfolder/static',
'/staticPages/*',
],
});
});
});
});

0 comments on commit 44dfa99

Please sign in to comment.