From 725c44a762dbc2f45a1d47ffa31b7e6e0b22ff95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Seery?= Date: Fri, 22 Apr 2022 11:03:53 -0300 Subject: [PATCH] feat(vercel): Support `trailingSlash` (#3176) --- .changeset/thirty-boxes-shave.md | 5 +++ packages/integrations/vercel/src/index.ts | 48 ++++++++++++++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 .changeset/thirty-boxes-shave.md diff --git a/.changeset/thirty-boxes-shave.md b/.changeset/thirty-boxes-shave.md new file mode 100644 index 000000000000..bb270722c205 --- /dev/null +++ b/.changeset/thirty-boxes-shave.md @@ -0,0 +1,5 @@ +--- +'@astrojs/vercel': patch +--- + +Support trailingSlash diff --git a/packages/integrations/vercel/src/index.ts b/packages/integrations/vercel/src/index.ts index 439eeeb04ce2..35d20d42f661 100644 --- a/packages/integrations/vercel/src/index.ts +++ b/packages/integrations/vercel/src/index.ts @@ -87,11 +87,49 @@ export default function vercel(): AstroIntegration { version: 3, basePath: '/', pages404: false, - rewrites: staticRoutes.map((route) => ({ - source: route.pathname, - regex: route.pattern.toString(), - destination: `/${ENTRYFILE}`, - })), + redirects: + // Extracted from Next.js v12.1.5 + _config.trailingSlash === 'always' + ? [ + { + source: '/:file((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+)/', + destination: '/:file', + internal: true, + statusCode: 308, + regex: '^(?:/((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+))/$', + }, + { + source: '/:notfile((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+)', + destination: '/:notfile/', + internal: true, + statusCode: 308, + regex: '^(?:/((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+))$', + }, + ] + : _config.trailingSlash === 'never' + ? [ + { + source: '/:path+/', + destination: '/:path+', + internal: true, + statusCode: 308, + regex: '^(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))/$', + }, + ] + : undefined, + rewrites: staticRoutes.map((route) => { + let source = route.pathname as string; + + if (_config.trailingSlash === 'always' && !source.endsWith('/')) { + source += '/'; + } + + return { + source, + regex: route.pattern.toString(), + destination: `/${ENTRYFILE}`, + }; + }), dynamicRoutes: dynamicRoutes.map((route) => ({ page: `/${ENTRYFILE}`, regex: route.pattern.toString(),