Skip to content

Commit 47be7cb

Browse files
committed
feat: configure ssr handler with known routes
1 parent 4e13ef7 commit 47be7cb

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/build/functions/server.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,26 @@ const applyTemplateVariables = (template: string, variables: Record<string, stri
100100
}, template)
101101
}
102102

103+
/** Convert Next.js route syntax to URLPattern syntax */
104+
const transformRoutePattern = (route: string): string => {
105+
return route
106+
.replace(/\[\[\.\.\.(\w+)]]/g, ':$1*') // [[...slug]] -> :slug*
107+
.replace(/\[\.{3}(\w+)]/g, ':$1+') // [...slug] -> :slug+
108+
.replace(/\[(\w+)]/g, ':$1') // [id] -> :id
109+
}
110+
103111
/** Get's the content of the handler file that will be written to the lambda */
104112
const getHandlerFile = async (ctx: PluginContext): Promise<string> => {
105113
const templatesDir = join(ctx.pluginDir, 'dist/build/templates')
106114

115+
const routesManifest = await ctx.getRoutesManifest()
116+
const routes = [...routesManifest.staticRoutes, ...routesManifest.dynamicRoutes]
117+
.map((route) => transformRoutePattern(route.page))
118+
.join("','")
119+
107120
const templateVariables: Record<string, string> = {
108121
'{{useRegionalBlobs}}': ctx.useRegionalBlobs.toString(),
122+
'{{paths}}': routes,
109123
}
110124
// In this case it is a monorepo and we need to use a own template for it
111125
// as we have to change the process working directory

src/build/templates/handler.tmpl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ export default async function handler(req, context) {
4444
}
4545

4646
export const config = {
47-
path: '/*',
47+
path: ['{{paths}}'],
4848
preferStatic: true,
4949
}

0 commit comments

Comments
 (0)