@astrojs/cloudflare 6.6.2 → 6.7.0

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
@@ -21,6 +21,7 @@ const SHIM = `globalThis.process = {
21
21
  env: {},
22
22
  };`;
23
23
  const SERVER_BUILD_FOLDER = "/$server_build/";
24
+ const potentialFunctionRouteTypes = ["endpoint", "page"];
24
25
  function createIntegration(args) {
25
26
  let _config;
26
27
  let _buildConfig;
@@ -173,10 +174,20 @@ function createIntegration(args) {
173
174
  }
174
175
  const routesExists = await fs.promises.stat(new URL("./_routes.json", _config.outDir)).then((stat) => stat.isFile()).catch(() => false);
175
176
  if (!routesExists) {
177
+ const functionEndpoints = routes.filter((route) => potentialFunctionRouteTypes.includes(route.type) && !route.prerender).map((route) => {
178
+ const includePattern = "/" + route.segments.flat().map((segment) => segment.dynamic ? "*" : segment.content).join("/");
179
+ const regexp = new RegExp(
180
+ "^\\/" + route.segments.flat().map((segment) => segment.dynamic ? "(.*)" : segment.content).join("\\/") + "$"
181
+ );
182
+ return {
183
+ includePattern,
184
+ regexp
185
+ };
186
+ });
176
187
  const staticPathList = (await glob(`${fileURLToPath(_buildConfig.client)}/**/*`, {
177
188
  cwd: fileURLToPath(_config.outDir),
178
189
  filesOnly: true
179
- })).filter((file) => cloudflareSpecialFiles.indexOf(file) < 0).map((file) => `/${file}`);
190
+ })).filter((file) => cloudflareSpecialFiles.indexOf(file) < 0).map((file) => `/${file.replace(/\\/g, "/")}`);
180
191
  for (let page of pages) {
181
192
  let pagePath = prependForwardSlash(page.pathname);
182
193
  if (_config.base !== "/") {
@@ -215,13 +226,30 @@ function createIntegration(args) {
215
226
  trueRedirects.print()
216
227
  );
217
228
  }
229
+ staticPathList.push(...routes.filter((r) => r.type === "redirect").map((r) => r.route));
230
+ let include = deduplicatePatterns(
231
+ functionEndpoints.map((endpoint) => endpoint.includePattern)
232
+ );
233
+ let exclude = deduplicatePatterns(
234
+ staticPathList.filter(
235
+ (file) => functionEndpoints.some((endpoint) => endpoint.regexp.test(file))
236
+ )
237
+ );
238
+ if (include.length === 0) {
239
+ include = ["/"];
240
+ exclude = ["/"];
241
+ }
242
+ if (include.length + exclude.length > staticPathList.length) {
243
+ include = ["/*"];
244
+ exclude = deduplicatePatterns(staticPathList);
245
+ }
218
246
  await fs.promises.writeFile(
219
247
  new URL("./_routes.json", _config.outDir),
220
248
  JSON.stringify(
221
249
  {
222
250
  version: 1,
223
- include: ["/*"],
224
- exclude: staticPathList
251
+ include,
252
+ exclude
225
253
  },
226
254
  null,
227
255
  2
@@ -235,6 +263,18 @@ function createIntegration(args) {
235
263
  function prependForwardSlash(path) {
236
264
  return path[0] === "/" ? path : "/" + path;
237
265
  }
266
+ function deduplicatePatterns(patterns) {
267
+ const openPatterns = [];
268
+ return [...new Set(patterns)].sort((a, b) => a.length - b.length).filter((pattern) => {
269
+ if (openPatterns.some((p) => p.test(pattern))) {
270
+ return false;
271
+ }
272
+ if (pattern.endsWith("*")) {
273
+ openPatterns.push(new RegExp(`^${pattern.replace(/(\*\/)*\*$/g, ".*")}`));
274
+ }
275
+ return true;
276
+ });
277
+ }
238
278
  export {
239
279
  createIntegration as default,
240
280
  getAdapter
@@ -1,4 +1,4 @@
1
- import type { ExecutionContext, Request as CFRequest } from '@cloudflare/workers-types';
1
+ import type { Request as CFRequest, ExecutionContext } from '@cloudflare/workers-types';
2
2
  import type { SSRManifest } from 'astro';
3
3
  type Env = {
4
4
  ASSETS: {
@@ -1,4 +1,4 @@
1
- import type { EventContext, Request as CFRequest } from '@cloudflare/workers-types';
1
+ import type { Request as CFRequest, EventContext } from '@cloudflare/workers-types';
2
2
  import type { SSRManifest } from 'astro';
3
3
  export declare function createExports(manifest: SSRManifest): {
4
4
  onRequest: ({ request, next, ...runtimeEnv }: {
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": "6.6.2",
4
+ "version": "6.7.0",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -38,14 +38,14 @@
38
38
  "tiny-glob": "^0.2.9"
39
39
  },
40
40
  "peerDependencies": {
41
- "astro": "^2.8.4"
41
+ "astro": "^2.10.5"
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": "2.8.4",
48
+ "astro": "2.10.5",
49
49
  "astro-scripts": "0.0.14"
50
50
  },
51
51
  "scripts": {