Skip to content

Commit

Permalink
[fix] write redirects for prerendered pages with trailing slashes (#7747
Browse files Browse the repository at this point in the history
)

fixes #7725
  • Loading branch information
Rich-Harris authored Nov 21, 2022
1 parent 3603cec commit e354459
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-weeks-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

[fix] Write redirects for prerendered pages with trailing slashes
50 changes: 33 additions & 17 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default function ({ external = [], edge, split } = {}) {
const node_version = get_node_version();

const dir = '.vercel/output';

const tmp = builder.getBuildDirectory('vercel-tmp');

builder.rimraf(dir);
builder.rimraf(tmp);

const files = fileURLToPath(new URL('./files', import.meta.url).href);
Expand All @@ -25,16 +25,34 @@ export default function ({ external = [], edge, split } = {}) {
functions: `${dir}/functions`
};

const prerendered_redirects = Array.from(
builder.prerendered.redirects,
([src, redirect]) => ({
/** @type {any[]} */
const prerendered_redirects = [];

/** @type {Record<string, { path: string }>} */
const overrides = {};

for (const [src, redirect] of builder.prerendered.redirects) {
prerendered_redirects.push({
src,
headers: {
Location: redirect.location
},
status: redirect.status
})
);
});
}

for (const [path, page] of builder.prerendered.pages) {
if (path.endsWith('/') && path !== '/') {
prerendered_redirects.push(
{ src: path, dest: path.slice(0, -1) },
{ src: path.slice(0, -1), status: 308, headers: { Location: path } }
);

overrides[page.file] = { path: path.slice(1, -1) };
} else {
overrides[page.file] = { path: path.slice(1) };
}
}

/** @type {any[]} */
const routes = [
Expand Down Expand Up @@ -164,19 +182,17 @@ export default function ({ external = [], edge, split } = {}) {

builder.log.minor('Writing routes...');

/** @type {Record<string, { path: string }>} */
const overrides = {};
builder.prerendered.pages.forEach((page, src) => {
overrides[page.file] = { path: src.slice(1) };
});

write(
`${dir}/config.json`,
JSON.stringify({
version: 3,
routes,
overrides
})
JSON.stringify(
{
version: 3,
routes,
overrides
},
null,
' '
)
);
}
};
Expand Down

0 comments on commit e354459

Please sign in to comment.