@astrojs/cloudflare 12.0.1 → 12.2.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.
@@ -1,20 +1,9 @@
1
- import { When, whenAmI } from '@it-astro:when';
2
- const middlewares = {
3
- [When.Client]: () => {
4
- throw new Error('Client should not run a middleware!');
5
- },
6
- [When.DevServer]: (_, next) => next(),
7
- [When.Server]: (_, next) => next(),
8
- [When.Prerender]: (ctx, next) => {
1
+ export const onRequest = (context, next) => {
2
+ if (context.isPrerendered) {
9
3
  // @ts-expect-error
10
- if (ctx.locals.runtime === undefined) {
11
- // @ts-expect-error
12
- ctx.locals.runtime = {
13
- env: process.env,
14
- };
15
- }
16
- return next();
17
- },
18
- [When.StaticBuild]: (_, next) => next(),
4
+ context.locals.runtime ??= {
5
+ env: process.env,
6
+ };
7
+ }
8
+ return next();
19
9
  };
20
- export const onRequest = middlewares[whenAmI];
@@ -34,6 +34,7 @@ export function createExports(manifest) {
34
34
  passThroughOnException: () => {
35
35
  throw new Error('`passThroughOnException` is currently not available in Cloudflare Pages. See https://developers.cloudflare.com/pages/platform/known-issues/#pages-functions.');
36
36
  },
37
+ props: {},
37
38
  },
38
39
  },
39
40
  };
package/dist/index.js CHANGED
@@ -3,9 +3,8 @@ import { appendFile, rename, stat } from 'node:fs/promises';
3
3
  import { createInterface } from 'node:readline/promises';
4
4
  import { appendForwardSlash, prependForwardSlash, removeLeadingForwardSlash, } from '@astrojs/internal-helpers/path';
5
5
  import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
6
- import astroWhen from '@inox-tools/astro-when';
7
6
  import { AstroError } from 'astro/errors';
8
- import { defaultServerConditions } from 'vite';
7
+ import { defaultClientConditions } from 'vite';
9
8
  import { getPlatformProxy } from 'wrangler';
10
9
  import { cloudflareModuleLoader, } from './utils/cloudflare-module-loader.js';
11
10
  import { createGetEnv } from './utils/env.js';
@@ -27,7 +26,9 @@ function setProcessEnv(config, env) {
27
26
  }
28
27
  export default function createIntegration(args) {
29
28
  let _config;
29
+ let finalBuildOutput;
30
30
  const cloudflareModulePlugin = cloudflareModuleLoader(args?.cloudflareModules ?? true);
31
+ let _routes;
31
32
  return {
32
33
  name: '@astrojs/cloudflare',
33
34
  hooks: {
@@ -56,21 +57,30 @@ export default function createIntegration(args) {
56
57
  },
57
58
  ],
58
59
  },
59
- integrations: [astroWhen()],
60
60
  image: setImageConfig(args?.imageService ?? 'compile', config.image, command, logger),
61
61
  });
62
- addWatchFile(new URL('./wrangler.toml', config.root));
63
- addWatchFile(new URL('./wrangler.json', config.root));
62
+ if (args?.platformProxy?.configPath) {
63
+ addWatchFile(new URL(args.platformProxy.configPath, config.root));
64
+ }
65
+ else {
66
+ addWatchFile(new URL('./wrangler.toml', config.root));
67
+ addWatchFile(new URL('./wrangler.json', config.root));
68
+ addWatchFile(new URL('./wrangler.jsonc', config.root));
69
+ }
64
70
  addMiddleware({
65
71
  entrypoint: '@astrojs/cloudflare/entrypoints/middleware.js',
66
72
  order: 'pre',
67
73
  });
68
74
  },
75
+ 'astro:routes:resolved': ({ routes }) => {
76
+ _routes = routes;
77
+ },
69
78
  'astro:config:done': ({ setAdapter, config, buildOutput, logger }) => {
70
79
  if (buildOutput === 'static') {
71
80
  logger.warn('[@astrojs/cloudflare] This adapter is intended to be used with server rendered pages, which this project does not contain any of. As such, this adapter is unnecessary.');
72
81
  }
73
82
  _config = config;
83
+ finalBuildOutput = buildOutput;
74
84
  setAdapter({
75
85
  name: '@astrojs/cloudflare',
76
86
  serverEntrypoint: '@astrojs/cloudflare/entrypoints/server.js',
@@ -84,8 +94,11 @@ export default function createIntegration(args) {
84
94
  hybridOutput: 'stable',
85
95
  staticOutput: 'unsupported',
86
96
  i18nDomains: 'experimental',
87
- sharpImageService: 'limited',
88
- envGetSecret: 'experimental',
97
+ sharpImageService: {
98
+ support: 'limited',
99
+ message: 'Cloudflare does not support sharp. You can use the `compile` image service to compile images at build time. It will not work for any on-demand rendered images.',
100
+ },
101
+ envGetSecret: 'stable',
89
102
  },
90
103
  });
91
104
  },
@@ -135,7 +148,7 @@ export default function createIntegration(args) {
135
148
  // (previously supported in esbuild instead: https://github.com/withastro/astro/pull/7092)
136
149
  vite.ssr ||= {};
137
150
  vite.ssr.resolve ||= {};
138
- vite.ssr.resolve.conditions ||= [...defaultServerConditions];
151
+ vite.ssr.resolve.conditions ||= [...defaultClientConditions];
139
152
  vite.ssr.resolve.conditions.push('workerd', 'worker');
140
153
  vite.ssr.target = 'webworker';
141
154
  vite.ssr.noExternal = true;
@@ -164,7 +177,7 @@ export default function createIntegration(args) {
164
177
  };
165
178
  }
166
179
  },
167
- 'astro:build:done': async ({ pages, routes, dir, logger }) => {
180
+ 'astro:build:done': async ({ pages, dir, logger, assets }) => {
168
181
  await cloudflareModulePlugin.afterBuildCompleted(_config);
169
182
  const PLATFORM_FILES = ['_headers', '_redirects', '_routes.json'];
170
183
  if (_config.base !== '/') {
@@ -221,17 +234,16 @@ export default function createIntegration(args) {
221
234
  routesExists = false;
222
235
  }
223
236
  if (!routesExists) {
224
- await createRoutesFile(_config, logger, routes, pages, redirects, args?.routes?.extend?.include, args?.routes?.extend?.exclude);
225
- }
226
- const redirectRoutes = [];
227
- for (const route of routes) {
228
- if (route.type === 'redirect')
229
- redirectRoutes.push([route, '']);
237
+ await createRoutesFile(_config, logger, _routes, pages, redirects, args?.routes?.extend?.include, args?.routes?.extend?.exclude);
230
238
  }
231
239
  const trueRedirects = createRedirectsFromAstroRoutes({
232
240
  config: _config,
233
- routeToDynamicTargetMap: new Map(Array.from(redirectRoutes)),
241
+ routeToDynamicTargetMap: new Map(Array.from(_routes
242
+ .filter((route) => route.type === 'redirect')
243
+ .map((route) => [route, '']))),
234
244
  dir,
245
+ buildOutput: finalBuildOutput,
246
+ assets,
235
247
  });
236
248
  if (!trueRedirects.empty()) {
237
249
  try {
@@ -1,8 +1,8 @@
1
- import type { AstroConfig, AstroIntegrationLogger, IntegrationRouteData, RoutePart } from 'astro';
1
+ import type { AstroConfig, AstroIntegrationLogger, IntegrationResolvedRoute, RoutePart } from 'astro';
2
2
  export declare function getParts(part: string): RoutePart[];
3
- export declare function createRoutesFile(_config: AstroConfig, logger: AstroIntegrationLogger, routes: IntegrationRouteData[], pages: {
3
+ export declare function createRoutesFile(_config: AstroConfig, logger: AstroIntegrationLogger, routes: IntegrationResolvedRoute[], pages: {
4
4
  pathname: string;
5
- }[], redirects: IntegrationRouteData['segments'][], includeExtends: {
5
+ }[], redirects: IntegrationResolvedRoute['segments'][], includeExtends: {
6
6
  pattern: string;
7
7
  }[] | undefined, excludeExtends: {
8
8
  pattern: string;
@@ -162,16 +162,16 @@ export async function createRoutesFile(_config, logger, routes, pages, redirects
162
162
  let hasPrerendered404 = false;
163
163
  for (const route of routes) {
164
164
  const convertedPath = segmentsToCfSyntax(route.segments, _config);
165
- if (route.pathname === '/404' && route.prerender === true)
165
+ if (route.pathname === '/404' && route.isPrerendered === true)
166
166
  hasPrerendered404 = true;
167
167
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
168
168
  switch (route.type) {
169
169
  case 'page':
170
- if (route.prerender === false)
170
+ if (route.isPrerendered === false)
171
171
  includePaths.push(convertedPath);
172
172
  break;
173
173
  case 'endpoint':
174
- if (route.prerender === false)
174
+ if (route.isPrerendered === false)
175
175
  includePaths.push(convertedPath);
176
176
  else
177
177
  excludePaths.push(convertedPath);
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": "12.0.1",
4
+ "version": "12.2.0",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -29,29 +29,28 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@astrojs/internal-helpers": "0.4.1",
33
- "@astrojs/underscore-redirects": "^0.4.0-alpha.0",
34
- "@cloudflare/workers-types": "^4.20241112.0",
35
- "@inox-tools/astro-when": "^0.2.4",
32
+ "@astrojs/internal-helpers": "0.4.2",
33
+ "@astrojs/underscore-redirects": "^0.6.0",
34
+ "@cloudflare/workers-types": "^4.20241230.0",
36
35
  "esbuild": "^0.24.0",
37
36
  "estree-walker": "^3.0.3",
38
- "magic-string": "^0.30.14",
39
- "miniflare": "^3.20241106.1",
37
+ "magic-string": "^0.30.17",
38
+ "miniflare": "^3.20241218.0",
40
39
  "tiny-glob": "^0.2.9",
41
- "vite": "^6.0.2",
42
- "wrangler": "^3.91.0"
40
+ "vite": "^6.0.7",
41
+ "wrangler": "^3.99.0"
43
42
  },
44
43
  "peerDependencies": {
45
44
  "astro": "^5.0.0"
46
45
  },
47
46
  "devDependencies": {
48
- "astro": "^5.0.0",
47
+ "astro": "^5.1.3",
49
48
  "cheerio": "1.0.0",
50
49
  "execa": "^8.0.1",
51
50
  "fast-glob": "^3.3.2",
52
- "rollup": "^4.27.4",
51
+ "rollup": "^4.29.1",
53
52
  "strip-ansi": "^7.1.0",
54
- "@astrojs/test-utils": "0.0.1",
53
+ "@astrojs/test-utils": "0.0.2",
55
54
  "astro-scripts": "0.0.14"
56
55
  },
57
56
  "publishConfig": {