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: prerendering encodes url multiple times #3339

Closed
wants to merge 6 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/green-rings-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] don't encode redirects multiple times
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
"options": {
"requirePragma": true
}
},
{
"files": [
"packages/kit/src/core/adapt/test/fixtures/**/*.html"
],
"options": {
"requirePragma": true
}
}
]
}
31 changes: 27 additions & 4 deletions packages/kit/src/core/adapt/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

/**
* @param {string} path
* @returns string
*/
function normalize(path) {
if (config.kit.trailingSlash === 'always') {
Expand All @@ -109,19 +110,39 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
return path;
}

/**
* @param {string} path
* @returns string
*/
function remove_query_string(path) {
return path.split('?')[0];
}

/** @type {(location: string) => string} */
function encode_location(location) {
const is_relative_url = location.startsWith('.') || location.startsWith('/');
if (is_relative_url) {
const protocol = 'http://';
return new URL(protocol + location).toString().substring(protocol.length);
}

return new URL(location).toString();
}

const q = queue(config.kit.prerender.concurrency);

/**
* @param {string} decoded_path
* @param {string?} referrer
*/
function enqueue(decoded_path, referrer) {
const path = encodeURI(normalize(decoded_path));
const decoded_path_without_query_string = remove_query_string(decoded_path);
const path = encodeURI(normalize(decoded_path_without_query_string));

if (seen.has(path)) return;
seen.add(path);

return q.add(() => visit(path, decoded_path, referrer));
return q.add(() => visit(path, decoded_path_without_query_string, referrer));
}

/**
Expand Down Expand Up @@ -167,8 +188,10 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
if (location) {
mkdirp(dirname(file));

const encoded_location = encode_location(location);

log.warn(`${rendered.status} ${decoded_path} -> ${location}`);
writeFileSync(file, `<meta http-equiv="refresh" content="0;url=${encodeURI(location)}">`);
writeFileSync(file, `<meta http-equiv="refresh" content="0;url=${encoded_location}">`);

const resolved = resolve(path, location);
if (is_root_relative(resolved)) {
Expand Down Expand Up @@ -206,7 +229,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

if (result.body) {
writeFileSync(file, result.body);
paths.push(dependency_path);
paths.push(normalize(dependency_path));
}

if (response_type === OK) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
function get_redirect_response(url) {
const [, , , , url_type, encoding_style] = url.split('/');

const base_url = (() => {
switch (url_type) {
case 'absolute-url':
return 'https://my.server.com/';
case 'path-url':
return './../../redirected';
case 'relative-url':
return '/redirected';
}
})();

const search_params =
encoding_style === 'encoding'
? new URLSearchParams({ redirect: 'https://kit.svelte.dev/docs' }).toString()
: 'redirect=https://kit.svelte.dev/docs';

return {
status: 302,
headers: {
location: `${base_url}?${search_params}`
}
};
}

export class App {
render() {
render({ url }) {
if (url.startsWith('http://prerender/redirects/')) return get_redirect_response(url);

return {
status: 200,
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta http-equiv="refresh" content="0;url=https://my.server.com/?redirect=https%3A%2F%2Fkit.svelte.dev%2Fdocs">
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta http-equiv="refresh" content="0;url=https://my.server.com/?redirect=https://kit.svelte.dev/docs">
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta http-equiv="refresh" content="0;url=./redirected?redirect=https%3A%2F%2Fkit.svelte.dev%2Fdocs">
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta http-equiv="refresh" content="0;url=./redirected?redirect=https://kit.svelte.dev/docs">
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta http-equiv="refresh" content="0;url=redirected/?redirect=https%3A%2F%2Fkit.svelte.dev%2Fdocs">
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta http-equiv="refresh" content="0;url=redirected/?redirect=https://kit.svelte.dev/docs">
30 changes: 27 additions & 3 deletions packages/kit/src/core/adapt/test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rmSync } from 'fs';
import { readFileSync, rmSync } from 'fs';
import { join } from 'path';
import * as uvu from 'uvu';
import * as assert from 'uvu/assert';
Expand Down Expand Up @@ -112,7 +112,15 @@ suite('prerender', async () => {
// @ts-expect-error
server: { chunks: [] },
static: [],
entries: ['/nested']
entries: [
'/nested',
'/redirects/absolute-url/no-encoding',
'/redirects/absolute-url/encoding',
'/redirects/path-url/no-encoding',
'/redirects/path-url/encoding',
'/redirects/relative-url/no-encoding',
'/redirects/relative-url/encoding'
]
};

const builder = create_builder({
Expand All @@ -130,7 +138,23 @@ suite('prerender', async () => {
dest
});

assert.equal(glob('**', { cwd: prerendered_files }), glob('**', { cwd: dest }));
const expected = glob('**', { cwd: prerendered_files });
const files = glob('**', { cwd: dest });

// test if all files are present
assert.equal(files, expected);

// check each file if content is correct
for (const file of expected.filter((file) => file.endsWith('.html'))) {
const expected_content = readFileSync(join(prerendered_files, file));
const actual_content = readFileSync(join(dest, file));

assert.equal(
actual_content.toString(),
expected_content.toString(),
`content of '${file}' is not equal`
);
}

rmSync(dest, { recursive: true, force: true });
});
Expand Down