Skip to content

Commit

Permalink
Vercel routing defaults (#3820)
Browse files Browse the repository at this point in the history
* expose trailingSlash to adapters

* update routes config according to trailingSlash option
  • Loading branch information
Rich-Harris authored Feb 10, 2022
1 parent c9e9809 commit 68fd684
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-donuts-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Pass trailingSlash config to adapters
5 changes: 5 additions & 0 deletions .changeset/modern-grapes-tell.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
1 change: 1 addition & 0 deletions packages/kit/src/core/adapt/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function create_builder({ cwd, config, build_data, log }) {
copy,

appDir: config.kit.appDir,
trailingSlash: config.kit.trailingSlash,

createEntries(fn) {
generated_manifest = true;
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface Builder {
mkdirp(dir: string): void;

appDir: string;
trailingSlash: 'always' | 'never' | 'ignore';

/**
* Create entry points that map to individual functions
Expand Down

0 comments on commit 68fd684

Please sign in to comment.