From 47be7cb21be7e70080ccfc0bb1e8b45cdaa74965 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 15 Nov 2024 14:10:52 +0000 Subject: [PATCH] feat: configure ssr handler with known routes --- src/build/functions/server.ts | 14 ++++++++++++++ src/build/templates/handler.tmpl.js | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/build/functions/server.ts b/src/build/functions/server.ts index bd38a82162..b3cb217989 100644 --- a/src/build/functions/server.ts +++ b/src/build/functions/server.ts @@ -100,12 +100,26 @@ const applyTemplateVariables = (template: string, variables: Record { + 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 => { 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 = { '{{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 diff --git a/src/build/templates/handler.tmpl.js b/src/build/templates/handler.tmpl.js index 0b10bcd902..1a089cb09f 100644 --- a/src/build/templates/handler.tmpl.js +++ b/src/build/templates/handler.tmpl.js @@ -44,6 +44,6 @@ export default async function handler(req, context) { } export const config = { - path: '/*', + path: ['{{paths}}'], preferStatic: true, }