@@ -42,6 +42,14 @@ const answered = (path: string, method: string) =>
4242 ! route . page && ( route [ `$${ method } ` ] || ( method === "HEAD" && route . $GET ) ) && matches ( route . path , path ) ,
4343 )
4444
45+ /** The methods an address takes: an API route's handlers (HEAD with GET), else a page's GET and HEAD. */
46+ const allowed = ( path : string ) => {
47+ const methods = pages
48+ . filter ( ( route ) => ! route . page && matches ( route . path , path ) )
49+ . flatMap ( ( route ) => Object . keys ( route ) . flatMap ( ( key ) => ( / ^ \$ [ A - Z ] + $ / . test ( key ) ? [ key . slice ( 1 ) ] : [ ] ) ) )
50+ return [ ...new Set ( methods . includes ( "GET" ) || ! methods . length ? [ ...methods , "GET" , "HEAD" ] : methods ) ] . join ( ", " )
51+ }
52+
4553/**
4654 * Production only: the static routes (routes.ts) as plain HTML with no hydration, and the not-found
4755 * page (`[...404].tsx`, with its 404 status) for every address no route answers. The page renders
@@ -56,12 +64,12 @@ export const staticPages: Middleware = async (_event, next) => {
5664 if ( import . meta. env . DEV || ! event ) return next ( )
5765 const url = new URL ( event . request . url )
5866 const path = strip ( url . pathname )
59- // Pages only answer GET and HEAD. Another method on an address no API route or server function serves gets a
60- // 405, not the app's document, whose scripts production no longer ships.
67+ // Pages only answer GET and HEAD. A method nothing handles gets a 405 listing the methods the address takes,
68+ // not the app's document, whose scripts production no longer ships.
6169 if ( event . request . method !== "GET" && event . request . method !== "HEAD" )
62- return path . startsWith ( "/_server" ) || pages . some ( ( route ) => ! route . page && matches ( route . path , path ) )
70+ return answered ( path , event . request . method )
6371 ? next ( )
64- : new Response ( null , { status : 405 , headers : { Allow : "GET, HEAD" } } )
72+ : new Response ( null , { status : 405 , headers : { Allow : allowed ( path ) } } )
6573 const pattern = staticPath ( path ) ?? ( answered ( path , event . request . method ) ? undefined : "/*404" )
6674 const load =
6775 pattern && pages . find ( ( route ) => route . page && route . path . replace ( / ( .) \/ $ / , "$1" ) === pattern ) ?. $component ?. import
0 commit comments