Skip to content

Commit

Permalink
update routes config according to trailingSlash option
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 10, 2022
1 parent a0da6fb commit 35b86c0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-donuts-grab.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Pass trailingSlash config to adapters
5 changes: 5 additions & 0 deletions .changeset/modern-grapes-tell.md
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

Clean URLs and handle trailingSlash configuration
78 changes: 78 additions & 0 deletions packages/adapter-vercel/index.js
Expand Up @@ -5,6 +5,83 @@ import esbuild from 'esbuild';

const dir = '.vercel_build_output';

// rules for clean URLs and trailing slash handling,
// generated with @vercel/routing-utils
const redirects = {
always: [
{
src: '^/(?:(.+)/)?index(?:\\.html)?/?$',
headers: {
Location: '/$1/'
},
status: 308
},
{
src: '^/(.*)\\.html/?$',
headers: {
Location: '/$1/'
},
status: 308
},
{
src: '^/\\.well-known(?:/.*)?$'
},
{
src: '^/((?:[^/]+/)*[^/\\.]+)$',
headers: {
Location: '/$1/'
},
status: 308
},
{
src: '^/((?:[^/]+/)*[^/]+\\.\\w+)/$',
headers: {
Location: '/$1'
},
status: 308
}
],
never: [
{
src: '^/(?:(.+)/)?index(?:\\.html)?/?$',
headers: {
Location: '/$1'
},
status: 308
},
{
src: '^/(.*)\\.html/?$',
headers: {
Location: '/$1'
},
status: 308
},
{
src: '^/(.*)/$',
headers: {
Location: '/$1'
},
status: 308
}
],
ignore: [
{
src: '^/(?:(.+)/)?index(?:\\.html)?/?$',
headers: {
Location: '/$1'
},
status: 308
},
{
src: '^/(.*)\\.html/?$',
headers: {
Location: '/$1'
},
status: 308
}
]
};

/** @type {import('.')} **/
export default function ({ external = [] } = {}) {
return {
Expand Down Expand Up @@ -69,6 +146,7 @@ export default function ({ external = [] } = {}) {
writeFileSync(
`${dir}/config/routes.json`,
JSON.stringify([
...redirects[builder.trailingSlash],
{
src: `/${builder.appDir}/.+`,
headers: {
Expand Down

0 comments on commit 35b86c0

Please sign in to comment.