Skip to content

Commit

Permalink
feat: configure ssr handler with known routes
Browse files Browse the repository at this point in the history
  • Loading branch information
orinokai committed Nov 15, 2024
1 parent 4e13ef7 commit 47be7cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/build/functions/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,26 @@ const applyTemplateVariables = (template: string, variables: Record<string, stri
}, template)
}

/** Convert Next.js route syntax to URLPattern syntax */
const transformRoutePattern = (route: string): string => {
return route
.replace(/\[\[\.\.\.(\w+)]]/g, ':$1*') // [[...slug]] -> :slug*
.replace(/\[\.{3}(\w+)]/g, ':$1+') // [...slug] -> :slug+
.replace(/\[(\w+)]/g, ':$1') // [id] -> :id
}

/** Get's the content of the handler file that will be written to the lambda */
const getHandlerFile = async (ctx: PluginContext): Promise<string> => {
const templatesDir = join(ctx.pluginDir, 'dist/build/templates')

const routesManifest = await ctx.getRoutesManifest()
const routes = [...routesManifest.staticRoutes, ...routesManifest.dynamicRoutes]
.map((route) => transformRoutePattern(route.page))
.join("','")

const templateVariables: Record<string, string> = {
'{{useRegionalBlobs}}': ctx.useRegionalBlobs.toString(),
'{{paths}}': routes,
}
// In this case it is a monorepo and we need to use a own template for it
// as we have to change the process working directory
Expand Down
2 changes: 1 addition & 1 deletion src/build/templates/handler.tmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ export default async function handler(req, context) {
}

export const config = {
path: '/*',
path: ['{{paths}}'],
preferStatic: true,
}

0 comments on commit 47be7cb

Please sign in to comment.