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' ;
28import type { PluginOption } from 'vite' ;
39
410import { createReadStream } from 'node:fs' ;
@@ -10,7 +16,6 @@ import {
1016 removeLeadingForwardSlash ,
1117} from '@astrojs/internal-helpers/path' ;
1218import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects' ;
13- import astroWhen from '@inox-tools/astro-when' ;
1419import { AstroError } from 'astro/errors' ;
1520import { defaultClientConditions } from 'vite' ;
1621import { type GetPlatformProxyOptions , getPlatformProxy } from 'wrangler' ;
@@ -86,6 +91,28 @@ function setProcessEnv(config: AstroConfig, env: Record<string, unknown>) {
8691 }
8792}
8893
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+
89116export default function createIntegration ( args ?: Options ) : AstroIntegration {
90117 let _config : AstroConfig ;
91118 let finalBuildOutput : HookParameters < 'astro:config:done' > [ 'buildOutput' ] ;
@@ -94,6 +121,8 @@ export default function createIntegration(args?: Options): AstroIntegration {
94121 args ?. cloudflareModules ?? true
95122 ) ;
96123
124+ let _routes : IntegrationResolvedRoute [ ] ;
125+
97126 return {
98127 name : '@astrojs/cloudflare' ,
99128 hooks : {
@@ -129,7 +158,6 @@ export default function createIntegration(args?: Options): AstroIntegration {
129158 } ,
130159 ] ,
131160 } ,
132- integrations : [ astroWhen ( ) ] ,
133161 image : setImageConfig ( args ?. imageService ?? 'compile' , config . image , command , logger ) ,
134162 } ) ;
135163 if ( args ?. platformProxy ?. configPath ) {
@@ -144,6 +172,9 @@ export default function createIntegration(args?: Options): AstroIntegration {
144172 order : 'pre' ,
145173 } ) ;
146174 } ,
175+ 'astro:routes:resolved' : ( { routes } ) => {
176+ _routes = routes ;
177+ } ,
147178 'astro:config:done' : ( { setAdapter, config, buildOutput, logger } ) => {
148179 if ( buildOutput === 'static' ) {
149180 logger . warn (
@@ -167,7 +198,11 @@ export default function createIntegration(args?: Options): AstroIntegration {
167198 hybridOutput : 'stable' ,
168199 staticOutput : 'unsupported' ,
169200 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+ } ,
171206 envGetSecret : 'stable' ,
172207 } ,
173208 } ) ;
@@ -260,7 +295,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
260295 } ;
261296 }
262297 } ,
263- 'astro:build:done' : async ( { pages, routes , dir, logger } ) => {
298+ 'astro:build:done' : async ( { pages, dir, logger, assets } ) => {
264299 await cloudflareModulePlugin . afterBuildCompleted ( _config ) ;
265300 const PLATFORM_FILES = [ '_headers' , '_redirects' , '_routes.json' ] ;
266301 if ( _config . base !== '/' ) {
@@ -285,7 +320,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
285320 redirectsExists = false ;
286321 }
287322
288- const redirects : IntegrationRouteData [ 'segments' ] [ ] = [ ] ;
323+ const redirects : IntegrationResolvedRoute [ 'segments' ] [ ] = [ ] ;
289324 if ( redirectsExists ) {
290325 const rl = createInterface ( {
291326 input : createReadStream ( new URL ( './_redirects' , _config . outDir ) ) ,
@@ -324,7 +359,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
324359 await createRoutesFile (
325360 _config ,
326361 logger ,
327- routes ,
362+ _routes ,
328363 pages ,
329364 redirects ,
330365 args ?. routes ?. extend ?. include ,
@@ -333,8 +368,10 @@ export default function createIntegration(args?: Options): AstroIntegration {
333368 }
334369
335370 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 ) , '' ] ) ;
338375 }
339376
340377 const trueRedirects = createRedirectsFromAstroRoutes ( {
0 commit comments