@@ -24,7 +24,26 @@ export type PageRouteOptions = {
2424 headConfig : PageGetArguments [ 6 ] | null ;
2525 renderOptions : PageGetArguments [ 7 ] ;
2626 namedActions ?: boolean ;
27+ trailingSlash ?: boolean ;
28+ catchAllParam ?: string ;
2729} ;
30+ // Hono's trie matcher cannot match a named catch-all followed by a literal slash.
31+ // Register that route without the slash, then enforce the public URL and exclude
32+ // the final separator from the parameter before invoking application code.
33+ function preparePageRequest ( c : Context , options : PageRouteOptions ) : boolean {
34+ if ( ! options . trailingSlash ) {
35+ return true ;
36+ }
37+ if ( ! c . req . path . endsWith ( '/' ) ) {
38+ return false ;
39+ }
40+ if ( options . catchAllParam ) {
41+ const params = c . req . param ( ) ;
42+ params [ options . catchAllParam ] = params [ options . catchAllParam ] . slice ( 0 , - 1 ) ;
43+ c . req . param = ( ( key ?: string ) => ( key ? params [ key ] : { ...params } ) ) as typeof c . req . param ;
44+ }
45+ return true ;
46+ }
2847function actionNameOf ( url : string ) : string | undefined {
2948 return new URL ( url ) . searchParams . keys ( ) . next ( ) . value ;
3049}
@@ -58,6 +77,9 @@ export function createNamedActionMarker(
5877}
5978export function createPageGetHandler ( options : PageRouteOptions ) : ( c : Context ) => Promise < Response > {
6079 return markRouteHandler ( async ( context : Context ) => {
80+ if ( ! preparePageRequest ( context , options ) ) {
81+ return context . notFound ( ) ;
82+ }
6183 const mod = options . load ? await options . load ( ) : { head : options . head } ;
6284 return handlePageGet (
6385 mod . loader ,
@@ -75,6 +97,9 @@ export function createPageMutationHandler(
7597 options : PageRouteOptions ,
7698) : ( c : Context ) => Promise < Response > {
7799 const mutationHandler = async ( context : Context ) => {
100+ if ( ! preparePageRequest ( context , options ) ) {
101+ return context . notFound ( ) ;
102+ }
78103 const mod = options . load ? await options . load ( ) : { } ;
79104 const actionName = options . namedActions ? actionNameOf ( context . req . url ) : undefined ;
80105 // The name comes from the query string, so only own function properties
@@ -131,22 +156,37 @@ export function createPagesFallback(options: {
131156 ) {
132157 return c . text ( 'Not Found' , 404 ) ;
133158 }
159+ const assets = c . env ?. ASSETS ;
160+ const assetResponse : Response =
161+ assets && typeof assets . fetch === 'function'
162+ ? await assets . fetch ( c . req . raw )
163+ : options . routing . markNoMatch ( c . req . raw , new Response ( 'Not Found' , { status : 404 } ) ) ;
164+ const filePath = path . includes ( '/.' ) || path . substring ( path . lastIndexOf ( '/' ) + 1 ) . includes ( '.' ) ;
134165 // Directory indexes may be served without a redirect by the asset binding.
135- // Canonicalize known Pages URLs first, without registering overlapping catch-all routes .
136- if (
166+ // Dynamic page patterns also match real files, which must keep their URLs .
167+ const pageNeedsSlash =
137168 options . trailingSlash &&
138169 ! path . endsWith ( '/' ) &&
139- options . pagePatterns ?. some ( ( pattern ) => pattern . test ( decodePagePath ( `${ path } /` ) ) )
170+ options . pagePatterns ?. some ( ( pattern ) => pattern . test ( decodePagePath ( `${ path } /` ) ) ) ;
171+ let directoryIndex = false ;
172+ if (
173+ pageNeedsSlash &&
174+ assetResponse . status === 200 &&
175+ assetResponse . headers . get ( 'Content-Type' ) ?. split ( ';' ) [ 0 ] === 'text/html' &&
176+ assets
140177 ) {
178+ const url = new URL ( c . req . url ) ;
179+ url . pathname += '/' ;
180+ const index = await assets . fetch ( new Request ( url , c . req . raw ) ) ;
181+ directoryIndex = index . status === 200 ;
182+ await index . body ?. cancel ( ) ;
183+ }
184+ if ( pageNeedsSlash && ( assetResponse . status === 404 || directoryIndex ) ) {
185+ await assetResponse . body ?. cancel ( ) ;
141186 const url = new URL ( c . req . url ) ;
142187 url . pathname += '/' ;
143188 return Response . redirect ( url . href , 301 ) ;
144189 }
145- const assets = c . env ?. ASSETS ;
146- const assetResponse : Response =
147- assets && typeof assets . fetch === 'function'
148- ? await assets . fetch ( c . req . raw )
149- : options . routing . markNoMatch ( c . req . raw , new Response ( 'Not Found' , { status : 404 } ) ) ;
150190 if ( assetResponse . status !== 404 ) {
151191 return markStaticAsset ( c , await cachePolicy ( c . req . raw , c . env , assetResponse ) ) ;
152192 }
@@ -156,7 +196,7 @@ export function createPagesFallback(options: {
156196 if ( fallback ) {
157197 return fallback ;
158198 }
159- if ( path . includes ( '/.' ) || path . substring ( path . lastIndexOf ( '/' ) + 1 ) . includes ( '.' ) ) {
199+ if ( filePath ) {
160200 return options . errors . serveCustomPage ( c , 404 , 'Not Found' ) ;
161201 }
162202 if ( path !== '/' && ( options . trailingSlash ? ! path . endsWith ( '/' ) : path . endsWith ( '/' ) ) ) {
0 commit comments