@astrojs/cloudflare 10.0.1 → 10.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -58,6 +58,7 @@ class TrieNode {
58
58
  }
59
59
  class PathTrie {
60
60
  root;
61
+ returnHasWildcard = false;
61
62
  constructor() {
62
63
  this.root = new TrieNode();
63
64
  }
@@ -83,6 +84,7 @@ class PathTrie {
83
84
  */
84
85
  dfs(node, path, allPaths) {
85
86
  if (node.hasWildcardChild) {
87
+ this.returnHasWildcard = true;
86
88
  allPaths.push([...path, '*']);
87
89
  return;
88
90
  }
@@ -96,12 +98,44 @@ class PathTrie {
96
98
  getAllPaths() {
97
99
  const allPaths = [];
98
100
  this.dfs(this.root, [], allPaths);
99
- return allPaths;
101
+ return [allPaths, this.returnHasWildcard];
100
102
  }
101
103
  }
102
104
  export async function createRoutesFile(_config, logger, routes, pages, redirects, includeExtends, excludeExtends) {
103
105
  const includePaths = [];
104
106
  const excludePaths = [];
107
+ /**
108
+ * All files in the `_config.build.assets` path, e.g. `_astro`
109
+ * are considered static assets and should not be handled by the function
110
+ * therefore we exclude a wildcard for that, e.g. `/_astro/*`
111
+ */
112
+ const assetsPath = segmentsToCfSyntax([
113
+ [{ content: _config.build.assets, dynamic: false, spread: false }],
114
+ [{ content: '', dynamic: true, spread: false }],
115
+ ], _config);
116
+ excludePaths.push(assetsPath);
117
+ if (existsSync(fileURLToPath(_config.publicDir))) {
118
+ const staticFiles = await glob(`${fileURLToPath(_config.publicDir)}/**/*`, {
119
+ cwd: fileURLToPath(_config.publicDir),
120
+ filesOnly: true,
121
+ dot: true,
122
+ });
123
+ for (const staticFile of staticFiles) {
124
+ if (['_headers', '_redirects', '_routes.json'].includes(staticFile))
125
+ continue;
126
+ const staticPath = staticFile;
127
+ const segments = removeLeadingForwardSlash(staticPath)
128
+ .split(posix.sep)
129
+ .filter(Boolean)
130
+ .map((s) => {
131
+ return getParts(s);
132
+ });
133
+ excludePaths.push(segmentsToCfSyntax(segments, _config));
134
+ }
135
+ }
136
+ for (const redirect of redirects) {
137
+ excludePaths.push(segmentsToCfSyntax(redirect, _config));
138
+ }
105
139
  let hasPrerendered404 = false;
106
140
  for (const route of routes) {
107
141
  const convertedPath = segmentsToCfSyntax(route.segments, _config);
@@ -132,6 +166,8 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
132
166
  }
133
167
  }
134
168
  for (const page of pages) {
169
+ if (page.pathname === '404')
170
+ hasPrerendered404 = true;
135
171
  const pageSegments = removeLeadingForwardSlash(page.pathname)
136
172
  .split(posix.sep)
137
173
  .filter(Boolean)
@@ -140,66 +176,58 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
140
176
  });
141
177
  excludePaths.push(segmentsToCfSyntax(pageSegments, _config));
142
178
  }
143
- if (existsSync(fileURLToPath(_config.publicDir))) {
144
- const staticFiles = await glob(`${fileURLToPath(_config.publicDir)}/**/*`, {
145
- cwd: fileURLToPath(_config.publicDir),
146
- filesOnly: true,
147
- dot: true,
148
- });
149
- for (const staticFile of staticFiles) {
150
- if (['_headers', '_redirects', '_routes.json'].includes(staticFile))
151
- continue;
152
- const staticPath = staticFile;
153
- const segments = removeLeadingForwardSlash(staticPath)
154
- .split(posix.sep)
155
- .filter(Boolean)
156
- .map((s) => {
157
- return getParts(s);
158
- });
159
- excludePaths.push(segmentsToCfSyntax(segments, _config));
160
- }
161
- }
162
- /**
163
- * All files in the `_config.build.assets` path, e.g. `_astro`
164
- * are considered static assets and should not be handled by the function
165
- * therefore we exclude a wildcard for that, e.g. `/_astro/*`
166
- */
167
- const assetsPath = segmentsToCfSyntax([
168
- [{ content: _config.build.assets, dynamic: false, spread: false }],
169
- [{ content: '', dynamic: true, spread: false }],
170
- ], _config);
171
- excludePaths.push(assetsPath);
172
- for (const redirect of redirects) {
173
- excludePaths.push(segmentsToCfSyntax(redirect, _config));
174
- }
175
179
  const includeTrie = new PathTrie();
176
180
  for (const includePath of includePaths) {
177
181
  includeTrie.insert(includePath);
178
182
  }
179
- const deduplicatedIncludePaths = includeTrie.getAllPaths();
183
+ const [deduplicatedIncludePaths, includedPathsHaveWildcard] = includeTrie.getAllPaths();
180
184
  const excludeTrie = new PathTrie();
181
185
  for (const excludePath of excludePaths) {
186
+ /**
187
+ * A excludePath with starts with a wildcard (*) is a catch-all
188
+ * that would mean all routes are static, that would be equal to a full SSG project
189
+ * the adapter is not needed in this case, so we do not consider such paths
190
+ */
191
+ if (excludePath[0] === '*')
192
+ continue;
182
193
  excludeTrie.insert(excludePath);
183
194
  }
184
- const deduplicatedExcludePaths = excludeTrie.getAllPaths();
195
+ const [deduplicatedExcludePaths, _excludedPathsHaveWildcard] = excludeTrie.getAllPaths();
185
196
  /**
186
197
  * Cloudflare allows no more than 100 include/exclude rules combined
187
198
  * https://developers.cloudflare.com/pages/functions/routing/#limits
188
199
  */
189
200
  const CLOUDFLARE_COMBINED_LIMIT = 100;
201
+ /**
202
+ * Caluclate the number of automated and extended include rules
203
+ */
204
+ const AUTOMATIC_INCLUDE_RULES_COUNT = deduplicatedIncludePaths.length;
205
+ const EXTENDED_INCLUDE_RULES_COUNT = includeExtends?.length ?? 0;
206
+ const INCLUDE_RULES_COUNT = AUTOMATIC_INCLUDE_RULES_COUNT + EXTENDED_INCLUDE_RULES_COUNT;
207
+ /**
208
+ * Caluclate the number of automated and extended exclude rules
209
+ */
210
+ const AUTOMATIC_EXCLUDE_RULES_COUNT = deduplicatedExcludePaths.length;
211
+ const EXTENDED_EXCLUDE_RULES_COUNT = excludeExtends?.length ?? 0;
212
+ const EXCLUDE_RULES_COUNT = AUTOMATIC_EXCLUDE_RULES_COUNT + EXTENDED_EXCLUDE_RULES_COUNT;
190
213
  if (!hasPrerendered404 ||
191
- deduplicatedIncludePaths.length + (includeExtends?.length ?? 0) > CLOUDFLARE_COMBINED_LIMIT ||
192
- deduplicatedExcludePaths.length + (excludeExtends?.length ?? 0) > CLOUDFLARE_COMBINED_LIMIT) {
214
+ INCLUDE_RULES_COUNT > CLOUDFLARE_COMBINED_LIMIT ||
215
+ EXCLUDE_RULES_COUNT > CLOUDFLARE_COMBINED_LIMIT) {
193
216
  await writeRoutesFileToOutDir(_config, logger, ['/*'].concat(includeExtends?.map((entry) => entry.pattern) ?? []), deduplicatedExcludePaths
194
217
  .map((path) => `${prependForwardSlash(path.join('/'))}`)
195
- .concat(excludeExtends?.map((entry) => entry.pattern) ?? [])
196
- .slice(0, 99));
218
+ .slice(0, CLOUDFLARE_COMBINED_LIMIT -
219
+ EXTENDED_INCLUDE_RULES_COUNT -
220
+ EXTENDED_EXCLUDE_RULES_COUNT -
221
+ 1)
222
+ .concat(excludeExtends?.map((entry) => entry.pattern) ?? []));
197
223
  }
198
224
  else {
199
225
  await writeRoutesFileToOutDir(_config, logger, deduplicatedIncludePaths
200
226
  .map((path) => `${prependForwardSlash(path.join('/'))}`)
201
- .concat(includeExtends?.map((entry) => entry.pattern) ?? []), deduplicatedExcludePaths
202
- .map((path) => `${prependForwardSlash(path.join('/'))}`)
203
- .concat(excludeExtends?.map((entry) => entry.pattern) ?? []));
227
+ .concat(includeExtends?.map((entry) => entry.pattern) ?? []), includedPathsHaveWildcard
228
+ ? deduplicatedExcludePaths
229
+ .map((path) => `${prependForwardSlash(path.join('/'))}`)
230
+ .concat(excludeExtends?.map((entry) => entry.pattern) ?? [])
231
+ : []);
204
232
  }
205
233
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/cloudflare",
3
3
  "description": "Deploy your site to Cloudflare Workers/Pages",
4
- "version": "10.0.1",
4
+ "version": "10.0.2",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -46,8 +46,8 @@
46
46
  "strip-ansi": "^7.1.0",
47
47
  "astro": "^4.5.8",
48
48
  "cheerio": "1.0.0-rc.12",
49
- "astro-scripts": "0.0.14",
50
- "@astrojs/test-utils": "0.0.1"
49
+ "@astrojs/test-utils": "0.0.1",
50
+ "astro-scripts": "0.0.14"
51
51
  },
52
52
  "publishConfig": {
53
53
  "provenance": true