1
- import type { AstroConfig , AstroIntegration , HookParameters , IntegrationRouteData } from 'astro' ;
1
+ import type {
2
+ AstroConfig ,
3
+ AstroIntegration ,
4
+ HookParameters ,
5
+ IntegrationResolvedRoute ,
6
+ IntegrationRouteData ,
7
+ } from 'astro' ;
2
8
import type { PluginOption } from 'vite' ;
3
9
4
10
import { createReadStream } from 'node:fs' ;
@@ -10,7 +16,6 @@ import {
10
16
removeLeadingForwardSlash ,
11
17
} from '@astrojs/internal-helpers/path' ;
12
18
import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects' ;
13
- import astroWhen from '@inox-tools/astro-when' ;
14
19
import { AstroError } from 'astro/errors' ;
15
20
import { defaultClientConditions } from 'vite' ;
16
21
import { type GetPlatformProxyOptions , getPlatformProxy } from 'wrangler' ;
@@ -86,6 +91,28 @@ function setProcessEnv(config: AstroConfig, env: Record<string, unknown>) {
86
91
}
87
92
}
88
93
94
+ function resolvedRouteToRouteData (
95
+ assets : HookParameters < 'astro:build:done' > [ 'assets' ] ,
96
+ route : IntegrationResolvedRoute
97
+ ) : IntegrationRouteData {
98
+ return {
99
+ pattern : route . patternRegex ,
100
+ component : route . entrypoint ,
101
+ prerender : route . isPrerendered ,
102
+ route : route . pattern ,
103
+ generate : route . generate ,
104
+ params : route . params ,
105
+ segments : route . segments ,
106
+ type : route . type ,
107
+ pathname : route . pathname ,
108
+ redirect : route . redirect ,
109
+ distURL : assets . get ( route . pattern ) ,
110
+ redirectRoute : route . redirectRoute
111
+ ? resolvedRouteToRouteData ( assets , route . redirectRoute )
112
+ : undefined ,
113
+ } ;
114
+ }
115
+
89
116
export default function createIntegration ( args ?: Options ) : AstroIntegration {
90
117
let _config : AstroConfig ;
91
118
let finalBuildOutput : HookParameters < 'astro:config:done' > [ 'buildOutput' ] ;
@@ -94,6 +121,8 @@ export default function createIntegration(args?: Options): AstroIntegration {
94
121
args ?. cloudflareModules ?? true
95
122
) ;
96
123
124
+ let _routes : IntegrationResolvedRoute [ ] ;
125
+
97
126
return {
98
127
name : '@astrojs/cloudflare' ,
99
128
hooks : {
@@ -129,7 +158,6 @@ export default function createIntegration(args?: Options): AstroIntegration {
129
158
} ,
130
159
] ,
131
160
} ,
132
- integrations : [ astroWhen ( ) ] ,
133
161
image : setImageConfig ( args ?. imageService ?? 'compile' , config . image , command , logger ) ,
134
162
} ) ;
135
163
if ( args ?. platformProxy ?. configPath ) {
@@ -144,6 +172,9 @@ export default function createIntegration(args?: Options): AstroIntegration {
144
172
order : 'pre' ,
145
173
} ) ;
146
174
} ,
175
+ 'astro:routes:resolved' : ( { routes } ) => {
176
+ _routes = routes ;
177
+ } ,
147
178
'astro:config:done' : ( { setAdapter, config, buildOutput, logger } ) => {
148
179
if ( buildOutput === 'static' ) {
149
180
logger . warn (
@@ -167,7 +198,11 @@ export default function createIntegration(args?: Options): AstroIntegration {
167
198
hybridOutput : 'stable' ,
168
199
staticOutput : 'unsupported' ,
169
200
i18nDomains : 'experimental' ,
170
- sharpImageService : 'limited' ,
201
+ sharpImageService : {
202
+ support : 'limited' ,
203
+ message :
204
+ '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.' ,
205
+ } ,
171
206
envGetSecret : 'stable' ,
172
207
} ,
173
208
} ) ;
@@ -260,7 +295,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
260
295
} ;
261
296
}
262
297
} ,
263
- 'astro:build:done' : async ( { pages, routes , dir, logger } ) => {
298
+ 'astro:build:done' : async ( { pages, dir, logger, assets } ) => {
264
299
await cloudflareModulePlugin . afterBuildCompleted ( _config ) ;
265
300
const PLATFORM_FILES = [ '_headers' , '_redirects' , '_routes.json' ] ;
266
301
if ( _config . base !== '/' ) {
@@ -285,7 +320,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
285
320
redirectsExists = false ;
286
321
}
287
322
288
- const redirects : IntegrationRouteData [ 'segments' ] [ ] = [ ] ;
323
+ const redirects : IntegrationResolvedRoute [ 'segments' ] [ ] = [ ] ;
289
324
if ( redirectsExists ) {
290
325
const rl = createInterface ( {
291
326
input : createReadStream ( new URL ( './_redirects' , _config . outDir ) ) ,
@@ -324,7 +359,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
324
359
await createRoutesFile (
325
360
_config ,
326
361
logger ,
327
- routes ,
362
+ _routes ,
328
363
pages ,
329
364
redirects ,
330
365
args ?. routes ?. extend ?. include ,
@@ -333,8 +368,10 @@ export default function createIntegration(args?: Options): AstroIntegration {
333
368
}
334
369
335
370
const redirectRoutes : [ IntegrationRouteData , string ] [ ] = [ ] ;
336
- for ( const route of routes ) {
337
- if ( route . type === 'redirect' ) redirectRoutes . push ( [ route , '' ] ) ;
371
+ for ( const route of _routes ) {
372
+ // TODO: Replace workaround after upstream @astrojs /underscore-redirects is changed, to support new IntegrationResolvedRoute type
373
+ if ( route . type === 'redirect' )
374
+ redirectRoutes . push ( [ resolvedRouteToRouteData ( assets , route ) , '' ] ) ;
338
375
}
339
376
340
377
const trueRedirects = createRedirectsFromAstroRoutes ( {
0 commit comments