Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retain URL query string for trailing slash redirects to prerendered pages #11142

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/gold-meals-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-cloudflare': patch
'@sveltejs/kit': patch
---

fix: retain URL query string for trailing slash redirects to prerendered pages
5 changes: 3 additions & 2 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default {
});
}

let { pathname } = url;
let { pathname, search } = url;
try {
pathname = decodeURIComponent(pathname);
} catch {
Expand All @@ -57,7 +57,7 @@ export default {
manifest.assets.has(filename) || manifest.assets.has(filename + '/index.html');
}

const location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';
let location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';

if (is_static_asset || prerendered.has(pathname)) {
return get_asset_from_kv(req, env, context, (request, options) => {
Expand All @@ -69,6 +69,7 @@ export default {
return mapRequestToAsset(request, options);
});
} else if (location && prerendered.has(location)) {
if (search) location += search;
return new Response('', {
status: 308,
headers: {
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const worker = {
let res = !pragma.includes('no-cache') && (await Cache.lookup(req));
if (res) return res;

let { pathname } = new URL(req.url);
let { pathname, search } = new URL(req.url);
try {
pathname = decodeURIComponent(pathname);
} catch {
Expand All @@ -31,11 +31,12 @@ const worker = {
manifest.assets.has(filename) || manifest.assets.has(filename + '/index.html');
}

const location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';
let location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';

if (is_static_asset || prerendered.has(pathname)) {
res = await env.ASSETS.fetch(req);
} else if (location && prerendered.has(location)) {
if (search) location += search;
res = new Response('', {
status: 308,
headers: {
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export async function preview(vite, vite_config, svelte_config) {
return;
}

const { pathname } = new URL(/** @type {string} */ (req.url), 'http://dummy');
const { pathname, search } = new URL(/** @type {string} */ (req.url), 'http://dummy');

let filename = normalizePath(
join(svelte_config.kit.outDir, 'output/prerendered/pages' + pathname)
Expand All @@ -113,6 +113,7 @@ export async function preview(vite, vite_config, svelte_config) {
const has_trailing_slash = pathname.endsWith('/');
const html_filename = `${filename}${has_trailing_slash ? 'index.html' : '.html'}`;

/** @type {string | undefined} */
let redirect;

if (is_file(html_filename)) {
Expand All @@ -127,6 +128,7 @@ export async function preview(vite, vite_config, svelte_config) {
}

if (redirect) {
if (search) redirect += search;
res.writeHead(307, {
location: redirect
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const prerender = true;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const trailingSlash = 'always';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const trailingSlash = 'ignore';
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
const config = {
kit: {
prerender: {
entries: [
'*',
'/routing/prerendered/trailing-slash/always/',
'/routing/prerendered/trailing-slash/never',
'/routing/prerendered/trailing-slash/ignore'
],
handleHttpError: 'warn'
},

Expand Down
15 changes: 15 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,21 @@ test.describe('Routing', () => {
const data = await response.json();
expect(data).toEqual({ surprise: 'lol' });
});

test('Vite trailing slash redirect for prerendered pages retains URL query string', async ({
request
}) => {
if (process.env.DEV) return;

let response = await request.get('/routing/prerendered/trailing-slash/always?a=1');
expect(new URL(response.url()).search).toBe('?a=1');

response = await request.get('/routing/prerendered/trailing-slash/never/?a=1');
expect(new URL(response.url()).search).toBe('?a=1');

response = await request.get('/routing/prerendered/trailing-slash/ignore/?a=1');
expect(new URL(response.url()).search).toBe('?a=1');
});
});

test.describe('Shadowed pages', () => {
Expand Down
Loading