@astrojs/cloudflare 7.0.0-beta.0 → 7.0.0-beta.1

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.
package/README.md CHANGED
@@ -106,7 +106,10 @@ Cloudflare has support for adding custom [headers](https://developers.cloudflare
106
106
 
107
107
  ### Custom `_routes.json`
108
108
 
109
- By default, `@astrojs/cloudflare` will generate a `_routes.json` file that lists all files from your `dist/` folder and redirects from the `_redirects` file in the `exclude` array. This will enable Cloudflare to serve files and process static redirects without a function invocation. Creating a custom `_routes.json` will override this automatic optimization and, if not configured manually, cause function invocations that will count against the request limits of your Cloudflare plan.
109
+ By default, `@astrojs/cloudflare` will generate a `_routes.json` file with `include` and `exclude` rules based on your applications's dynamic and static routes.
110
+ This will enable Cloudflare to serve files and process static redirects without a function invocation. Creating a custom `_routes.json` will override this automatic optimization and, if not configured manually, cause function invocations that will count against the request limits of your Cloudflare plan.
111
+
112
+ See [Cloudflare's documentation](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) for more details.
110
113
 
111
114
  ## Troubleshooting
112
115
 
package/dist/index.js CHANGED
@@ -41,6 +41,7 @@ const SHIM = `globalThis.process = {
41
41
  env: {},
42
42
  };`;
43
43
  const SERVER_BUILD_FOLDER = "/$server_build/";
44
+ const potentialFunctionRouteTypes = ["endpoint", "page"];
44
45
  function createIntegration(args) {
45
46
  let _config;
46
47
  let _buildConfig;
@@ -192,10 +193,20 @@ function createIntegration(args) {
192
193
  }
193
194
  const routesExists = await fs.promises.stat(new URL("./_routes.json", _config.outDir)).then((stat) => stat.isFile()).catch(() => false);
194
195
  if (!routesExists) {
196
+ const functionEndpoints = routes.filter((route) => potentialFunctionRouteTypes.includes(route.type) && !route.prerender).map((route) => {
197
+ const includePattern = "/" + route.segments.flat().map((segment) => segment.dynamic ? "*" : segment.content).join("/");
198
+ const regexp = new RegExp(
199
+ "^\\/" + route.segments.flat().map((segment) => segment.dynamic ? "(.*)" : segment.content).join("\\/") + "$"
200
+ );
201
+ return {
202
+ includePattern,
203
+ regexp
204
+ };
205
+ });
195
206
  const staticPathList = (await glob(`${fileURLToPath(_buildConfig.client)}/**/*`, {
196
207
  cwd: fileURLToPath(_config.outDir),
197
208
  filesOnly: true
198
- })).filter((file) => cloudflareSpecialFiles.indexOf(file) < 0).map((file) => `/${file}`);
209
+ })).filter((file) => cloudflareSpecialFiles.indexOf(file) < 0).map((file) => `/${file.replace(/\\/g, "/")}`);
199
210
  for (let page of pages) {
200
211
  let pagePath = prependForwardSlash(page.pathname);
201
212
  if (_config.base !== "/") {
@@ -234,13 +245,30 @@ function createIntegration(args) {
234
245
  trueRedirects.print()
235
246
  );
236
247
  }
248
+ staticPathList.push(...routes.filter((r) => r.type === "redirect").map((r) => r.route));
249
+ let include = deduplicatePatterns(
250
+ functionEndpoints.map((endpoint) => endpoint.includePattern)
251
+ );
252
+ let exclude = deduplicatePatterns(
253
+ staticPathList.filter(
254
+ (file) => functionEndpoints.some((endpoint) => endpoint.regexp.test(file))
255
+ )
256
+ );
257
+ if (include.length === 0) {
258
+ include = ["/"];
259
+ exclude = ["/"];
260
+ }
261
+ if (include.length + exclude.length > staticPathList.length) {
262
+ include = ["/*"];
263
+ exclude = deduplicatePatterns(staticPathList);
264
+ }
237
265
  await fs.promises.writeFile(
238
266
  new URL("./_routes.json", _config.outDir),
239
267
  JSON.stringify(
240
268
  {
241
269
  version: 1,
242
- include: ["/*"],
243
- exclude: staticPathList
270
+ include,
271
+ exclude
244
272
  },
245
273
  null,
246
274
  2
@@ -254,6 +282,18 @@ function createIntegration(args) {
254
282
  function prependForwardSlash(path) {
255
283
  return path[0] === "/" ? path : "/" + path;
256
284
  }
285
+ function deduplicatePatterns(patterns) {
286
+ const openPatterns = [];
287
+ return [...new Set(patterns)].sort((a, b) => a.length - b.length).filter((pattern) => {
288
+ if (openPatterns.some((p) => p.test(pattern))) {
289
+ return false;
290
+ }
291
+ if (pattern.endsWith("*")) {
292
+ openPatterns.push(new RegExp(`^${pattern.replace(/(\*\/)*\*$/g, ".*")}`));
293
+ }
294
+ return true;
295
+ });
296
+ }
257
297
  export {
258
298
  createIntegration as default,
259
299
  getAdapter
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": "7.0.0-beta.0",
4
+ "version": "7.0.0-beta.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -38,14 +38,14 @@
38
38
  "@astrojs/underscore-redirects": "0.3.0-beta.0"
39
39
  },
40
40
  "peerDependencies": {
41
- "astro": "^3.0.0-beta.0"
41
+ "astro": "^3.0.0-beta.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "chai": "^4.3.7",
45
45
  "cheerio": "1.0.0-rc.12",
46
46
  "mocha": "^9.2.2",
47
47
  "wrangler": "^2.0.23",
48
- "astro": "3.0.0-beta.0",
48
+ "astro": "3.0.0-beta.1",
49
49
  "astro-scripts": "0.0.14"
50
50
  },
51
51
  "scripts": {