Skip to content

Commit

Permalink
feat(vercel): Support trailingSlash
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanM04 committed Apr 22, 2022
1 parent 9c0fc68 commit e5bca4e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-boxes-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Support trailingSlash
48 changes: 43 additions & 5 deletions packages/integrations/vercel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit e5bca4e

Please sign in to comment.