@@ -101,25 +101,61 @@ const applyTemplateVariables = (template: string, variables: Record<string, stri
101101}
102102
103103/** Convert Next.js route syntax to URLPattern syntax */
104- const transformRoutePattern = ( route : string ) : string => {
104+ const transformRoutePatterns = ( route : string ) : string => {
105105 return route
106106 . replace ( / \[ \[ \. \. \. ( \w + ) ] ] / g, ':$1*' ) // [[...slug]] -> :slug*
107107 . replace ( / \[ \. { 3 } ( \w + ) ] / g, ':$1+' ) // [...slug] -> :slug+
108108 . replace ( / \[ ( \w + ) ] / g, ':$1' ) // [id] -> :id
109109}
110110
111- /** Get's the content of the handler file that will be written to the lambda */
112- const getHandlerFile = async ( ctx : PluginContext ) : Promise < string > => {
113- const templatesDir = join ( ctx . pluginDir , 'dist/build/templates' )
111+ const getRoutes = async ( ctx : PluginContext ) => {
112+ const internalRoutes = [
113+ '/_next/static/*' ,
114+ '/_next/data/*' ,
115+ '/_next/image/*' ,
116+ '/_next/postponed/*' ,
117+ ]
114118
115119 const routesManifest = await ctx . getRoutesManifest ( )
116- const routes = [ ...routesManifest . staticRoutes , ...routesManifest . dynamicRoutes ]
117- . map ( ( route ) => transformRoutePattern ( route . page ) )
118- . join ( "','" )
120+ const staticRoutes = routesManifest . staticRoutes . map ( ( route ) => route . page )
121+ const dynamicRoutes = routesManifest . dynamicRoutes . map ( ( route ) => route . page )
122+
123+ // route.source conforms to the URLPattern syntax, which will work with our redirect engine
124+ // however this will be a superset of possible routes as it does not parse the
125+ // header/cookie/query matching that Next.js offers
126+ const redirects = routesManifest . redirects . map ( ( route ) => route . source )
127+ const rewrites = Array . isArray ( routesManifest . rewrites )
128+ ? routesManifest . rewrites . map ( ( route ) => route . source )
129+ : [ ]
130+
131+ // this contains the static Route Handler routes
132+ const appPathRoutesManifest = await ctx . getAppPathRoutesManifest ( )
133+ const appRoutes = Object . values ( appPathRoutesManifest )
134+
135+ // this contains the API handler routes
136+ const pagesManifest = await ctx . getPagesManifest ( )
137+ const pagesRoutes = Object . keys ( pagesManifest )
138+
139+ return [
140+ ...internalRoutes ,
141+ ...staticRoutes ,
142+ ...dynamicRoutes ,
143+ ...redirects ,
144+ ...rewrites ,
145+ ...appRoutes ,
146+ ...pagesRoutes ,
147+ '/*' , // retain the catch-all route for our initial testing
148+ ] . map ( transformRoutePatterns )
149+ }
119150
151+ /** Get's the content of the handler file that will be written to the lambda */
152+ const getHandlerFile = async ( ctx : PluginContext ) : Promise < string > => {
153+ const routes = await getRoutes ( ctx )
154+
155+ const templatesDir = join ( ctx . pluginDir , 'dist/build/templates' )
120156 const templateVariables : Record < string , string > = {
121157 '{{useRegionalBlobs}}' : ctx . useRegionalBlobs . toString ( ) ,
122- '{{paths}}' : routes ,
158+ '{{paths}}' : routes . join ( "','" ) ,
123159 }
124160 // In this case it is a monorepo and we need to use a own template for it
125161 // as we have to change the process working directory
0 commit comments