Skip to content

Commit

Permalink
fix(vercel): trailingSlash fix for non-html pages (#3185)
Browse files Browse the repository at this point in the history
* fix(vercel): `trailingSlash` fix for non-html pages

* Changeset
  • Loading branch information
JuanM04 authored Apr 26, 2022
1 parent 22cb4b7 commit eaad176
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-houses-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Fixed `trailingSlash` for non-HTML pages
66 changes: 37 additions & 29 deletions packages/integrations/vercel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,47 @@ export default function vercel(): AstroIntegration {
basePath: '/',
pages404: false,
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: '^(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))/$',
},
]
_config.trailingSlash !== 'ignore'
? routes
.filter((route) => route.type === 'page' && !route.pathname?.endsWith('/'))
.map((route) => {
const path =
'/' +
route.segments
.map((segments) =>
segments
.map((part) =>
part.spread
? `:${part.content}*`
: part.dynamic
? `:${part.content}`
: part.content
)
.join('')
)
.join('/');

let source, destination;

if (_config.trailingSlash === 'always') {
source = path;
destination = path + '/';
} else {
source = path + '/';
destination = path;
}

return { source, destination, statusCode: 308 };
})
: undefined,
rewrites: staticRoutes.map((route) => {
let source = route.pathname as string;

if (_config.trailingSlash === 'always' && !source.endsWith('/')) {
if (
route.type === 'page' &&
_config.trailingSlash === 'always' &&
!source.endsWith('/')
) {
source += '/';
}

Expand Down

0 comments on commit eaad176

Please sign in to comment.