1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
import type { AstroConfig, AstroIntegrationLogger, RouteData, RoutePart } from 'astro';
import { existsSync } from 'node:fs';
import { writeFile } from 'node:fs/promises';
import { posix } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
prependForwardSlash,
removeLeadingForwardSlash,
removeTrailingForwardSlash,
} from '@astrojs/internal-helpers/path';
import glob from 'tiny-glob';
// Copied from https://github.com/withastro/astro/blob/3776ecf0aa9e08a992d3ae76e90682fd04093721/packages/astro/src/core/routing/manifest/create.ts#L45-L70
// We're not sure how to improve this regex yet
const ROUTE_DYNAMIC_SPLIT = /\[(.+?\(.+?\)|.+?)\]/;
const ROUTE_SPREAD = /^\.{3}.+$/;
export function getParts(part: string) {
const result: RoutePart[] = [];
part.split(ROUTE_DYNAMIC_SPLIT).map((str, i) => {
if (!str) return;
const dynamic = i % 2 === 1;
const [, content] = dynamic ? /([^(]+)$/.exec(str) || [null, null] : [null, str];
if (!content || (dynamic && !/^(?:\.\.\.)?[\w$]+$/.test(content))) {
throw new Error('Parameter name must match /^[a-zA-Z0-9_$]+$/');
}
result.push({
content,
dynamic,
spread: dynamic && ROUTE_SPREAD.test(content),
});
});
return result;
}
async function writeRoutesFileToOutDir(
_config: AstroConfig,
logger: AstroIntegrationLogger,
include: string[],
exclude: string[]
) {
try {
await writeFile(
new URL('./_routes.json', _config.outDir),
JSON.stringify(
{
version: 1,
include: include,
exclude: exclude,
},
null,
2
),
'utf-8'
);
} catch (error) {
logger.error("There was an error writing the '_routes.json' file to the output directory.");
}
}
function segmentsToCfSyntax(segments: RouteData['segments'], _config: AstroConfig) {
const pathSegments = [];
if (removeLeadingForwardSlash(removeTrailingForwardSlash(_config.base)).length > 0) {
pathSegments.push(removeLeadingForwardSlash(removeTrailingForwardSlash(_config.base)));
}
for (const segment of segments.flat()) {
if (segment.dynamic) pathSegments.push('*');
else pathSegments.push(segment.content);
}
return pathSegments;
}
class TrieNode {
children: Map<string, TrieNode> = new Map();
isEndOfPath = false;
hasWildcardChild = false;
}
class PathTrie {
root: TrieNode;
constructor() {
this.root = new TrieNode();
}
insert(path: string[]) {
let node = this.root;
for (const segment of path) {
if (segment === '*') {
node.hasWildcardChild = true;
break;
}
if (!node.children.has(segment)) {
node.children.set(segment, new TrieNode());
}
// biome-ignore lint/style/noNonNullAssertion: The `if` condition above ensures that the segment exists inside the map
node = node.children.get(segment)!;
}
node.isEndOfPath = true;
}
/**
* Depth-first search (dfs), traverses the "graph" segment by segment until the end or wildcard (*).
* It makes sure that all necessary paths are returned, but not paths with an existing wildcard prefix.
* e.g. if we have a path like /foo/* and /foo/bar, we only want to return /foo/*
*/
private dfs(node: TrieNode, path: string[], allPaths: string[][]): void {
if (node.hasWildcardChild) {
allPaths.push([...path, '*']);
return;
}
if (node.isEndOfPath) {
allPaths.push([...path]);
}
for (const [segment, childNode] of node.children) {
this.dfs(childNode, [...path, segment], allPaths);
}
}
getAllPaths(): string[][] {
const allPaths: string[][] = [];
this.dfs(this.root, [], allPaths);
return allPaths;
}
}
export async function createRoutesFile(
_config: AstroConfig,
logger: AstroIntegrationLogger,
routes: RouteData[],
pages: {
pathname: string;
}[],
redirects: RouteData['segments'][],
includeExtends:
| {
pattern: string;
}[]
| undefined,
excludeExtends:
| {
pattern: string;
}[]
| undefined
) {
const includePaths: string[][] = [];
const excludePaths: string[][] = [];
let hasPrerendered404 = false;
for (const route of routes) {
const convertedPath = segmentsToCfSyntax(route.segments, _config);
if (route.pathname === '/404' && route.prerender === true) hasPrerendered404 = true;
switch (route.type) {
case 'page':
if (route.prerender === false) includePaths.push(convertedPath);
break;
case 'endpoint':
if (route.prerender === false) includePaths.push(convertedPath);
else excludePaths.push(convertedPath);
break;
case 'redirect':
excludePaths.push(convertedPath);
break;
default:
/**
* We don't know the type, so we are conservative!
* Invoking the function on these is a safe-bet because
* the function will fallback to static asset fetching
*/
includePaths.push(convertedPath);
break;
}
}
for (const page of pages) {
const pageSegments = removeLeadingForwardSlash(page.pathname)
.split(posix.sep)
.filter(Boolean)
.map((s) => {
return getParts(s);
});
excludePaths.push(segmentsToCfSyntax(pageSegments, _config));
}
if (existsSync(fileURLToPath(_config.publicDir))) {
const staticFiles = await glob(`${fileURLToPath(_config.publicDir)}/**/*`, {
cwd: fileURLToPath(_config.publicDir),
filesOnly: true,
dot: true,
});
for (const staticFile of staticFiles) {
if (['_headers', '_redirects', '_routes.json'].includes(staticFile)) continue;
const staticPath = staticFile;
const segments = removeLeadingForwardSlash(staticPath)
.split(posix.sep)
.filter(Boolean)
.map((s: string) => {
return getParts(s);
});
excludePaths.push(segmentsToCfSyntax(segments, _config));
}
}
/**
* All files in the `_config.build.assets` path, e.g. `_astro`
* are considered static assets and should not be handled by the function
* therefore we exclude a wildcard for that, e.g. `/_astro/*`
*/
const assetsPath = segmentsToCfSyntax(
[
[{ content: _config.build.assets, dynamic: false, spread: false }],
[{ content: '', dynamic: true, spread: false }],
],
_config
);
excludePaths.push(assetsPath);
for (const redirect of redirects) {
excludePaths.push(segmentsToCfSyntax(redirect, _config));
}
const includeTrie = new PathTrie();
for (const includePath of includePaths) {
includeTrie.insert(includePath);
}
const deduplicatedIncludePaths = includeTrie.getAllPaths();
const excludeTrie = new PathTrie();
for (const excludePath of excludePaths) {
excludeTrie.insert(excludePath);
}
const deduplicatedExcludePaths = excludeTrie.getAllPaths();
/**
* Cloudflare allows no more than 100 include/exclude rules combined
* https://developers.cloudflare.com/pages/functions/routing/#limits
*/
const CLOUDFLARE_COMBINED_LIMIT = 100;
if (
!hasPrerendered404 ||
deduplicatedIncludePaths.length + (includeExtends?.length ?? 0) > CLOUDFLARE_COMBINED_LIMIT ||
deduplicatedExcludePaths.length + (excludeExtends?.length ?? 0) > CLOUDFLARE_COMBINED_LIMIT
) {
await writeRoutesFileToOutDir(
_config,
logger,
['/*'].concat(includeExtends?.map((entry) => entry.pattern) ?? []),
deduplicatedExcludePaths
.map((path) => `${prependForwardSlash(path.join('/'))}`)
.concat(excludeExtends?.map((entry) => entry.pattern) ?? [])
.slice(0, 99)
);
} else {
await writeRoutesFileToOutDir(
_config,
logger,
deduplicatedIncludePaths
.map((path) => `${prependForwardSlash(path.join('/'))}`)
.concat(includeExtends?.map((entry) => entry.pattern) ?? []),
deduplicatedExcludePaths
.map((path) => `${prependForwardSlash(path.join('/'))}`)
.concat(excludeExtends?.map((entry) => entry.pattern) ?? [])
);
}
}
|