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

Don't normalize paths during prerendering #4761

Merged
merged 5 commits into from
May 11, 2022
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
5 changes: 5 additions & 0 deletions .changeset/violet-ligers-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Prevent naive path normalization during prerendering
46 changes: 23 additions & 23 deletions packages/kit/src/core/build/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function prerender({ config, entries, files, log }) {
}

if (is_html && !file.endsWith('.html')) {
return file + (config.kit.trailingSlash === 'always' ? 'index.html' : '.html');
return file + (file.endsWith('/') ? 'index.html' : '.html');
}

return file;
Expand Down Expand Up @@ -166,13 +166,12 @@ export async function prerender({ config, entries, files, log }) {
const resolved = resolve(encoded, href);
if (!is_root_relative(resolved)) continue;

const parsed = new URL(resolved, 'http://localhost');
const { pathname, search } = new URL(resolved, 'http://localhost');

if (parsed.search) {
if (search) {
// TODO warn that query strings have no effect on statically-exported pages
}

const pathname = normalize_path(parsed.pathname, config.kit.trailingSlash);
enqueue(decoded, decodeURI(pathname), pathname);
}
}
Expand Down Expand Up @@ -202,28 +201,29 @@ export async function prerender({ config, entries, files, log }) {
const location = response.headers.get('location');

if (location) {
mkdirp(dirname(dest));

log.warn(`${response.status} ${decoded} -> ${location}`);

writeFileSync(
dest,
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
);

let resolved = resolve(encoded, location);
const resolved = resolve(encoded, location);
if (is_root_relative(resolved)) {
resolved = normalize_path(resolved, config.kit.trailingSlash);
enqueue(decoded, decodeURI(resolved), resolved);
}

if (!prerendered.redirects.has(decoded)) {
prerendered.redirects.set(decoded, {
status: response.status,
location: resolved
});
if (!response.headers.get('x-sveltekit-normalize')) {
mkdirp(dirname(dest));

log.warn(`${response.status} ${decoded} -> ${location}`);

writeFileSync(
dest,
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
);

if (!prerendered.redirects.has(decoded)) {
prerendered.redirects.set(decoded, {
status: response.status,
location: resolved
});

prerendered.paths.push(normalize_path(decoded, 'never'));
prerendered.paths.push(normalize_path(decoded, 'never'));
}
}
} else {
log.warn(`location header missing on redirect received from ${decoded}`);
Expand Down Expand Up @@ -258,10 +258,10 @@ export async function prerender({ config, entries, files, log }) {
for (const entry of config.kit.prerender.entries) {
if (entry === '*') {
for (const entry of entries) {
enqueue(null, normalize_path(config.kit.paths.base + entry, config.kit.trailingSlash)); // TODO can we pre-normalize these?
enqueue(null, config.kit.paths.base + entry); // TODO can we pre-normalize these?
}
} else {
enqueue(null, normalize_path(config.kit.paths.base + entry, config.kit.trailingSlash));
enqueue(null, config.kit.paths.base + entry);
}
}

Expand Down
7 changes: 1 addition & 6 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { load_template } from '../config/index.js';
import { sequence } from '../../hooks.js';
import { posixify } from '../../utils/filesystem.js';
import { parse_route_id } from '../../utils/routing.js';
import { normalize_path } from '../../utils/url.js';

/**
* @param {import('types').ValidatedConfig} config
Expand Down Expand Up @@ -205,11 +204,7 @@ export async function create_plugin(config, cwd) {
if (req.url === '/favicon.ico') return not_found(res);

if (!decoded.startsWith(config.kit.paths.base)) {
const suggestion = normalize_path(
config.kit.paths.base + req.url,
config.kit.trailingSlash
);
return not_found(res, `Not found (did you mean ${suggestion}?)`);
return not_found(res, `Not found (did you mean ${config.kit.paths.base + req.url}?)`);
}

/** @type {Partial<import('types').Hooks>} */
Expand Down
9 changes: 2 additions & 7 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ export async function respond(request, options, state) {

if (is_data_request) {
decoded = decoded.slice(0, -DATA_SUFFIX.length) || '/';

const normalized = normalize_path(
url.pathname.slice(0, -DATA_SUFFIX.length),
options.trailing_slash
);

url = new URL(url.origin + normalized + url.search);
url = new URL(url.origin + url.pathname.slice(0, -DATA_SUFFIX.length) + url.search);
}

if (!state.prerender || !state.prerender.fallback) {
Expand All @@ -92,6 +86,7 @@ export async function respond(request, options, state) {
return new Response(undefined, {
status: 301,
headers: {
'x-sveltekit-normalize': '1',
location:
// ensure paths starting with '//' are not treated as protocol-relative
(normalized.startsWith('//') ? url.origin + normalized : normalized) +
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/prerendering/options/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.1",
"scripts": {
"dev": "node ../../cli.js dev",
"build": "node ../../cli.js build",
"build": "node ../../cli.js build --verbose",
"preview": "node ../../cli.js preview",
"check": "tsc && svelte-check",
"test": "npm run build && uvu test"
Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/prerendering/options/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="%svelte.assets%/favicon.png" />
%svelte.head%
</head>
<body>
Expand Down
19 changes: 19 additions & 0 deletions packages/kit/test/prerendering/trailing-slash/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "prerendering-test-trailing-slash",
"private": true,
"version": "0.0.1",
"scripts": {
"dev": "node ../../cli.js dev",
"build": "node ../../cli.js build --verbose",
"preview": "node ../../cli.js preview",
"check": "tsc && svelte-check"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"svelte": "^3.43.0",
"svelte-check": "^2.5.0",
"typescript": "~4.6.2",
"uvu": "^0.5.2"
},
"type": "module"
}
12 changes: 12 additions & 0 deletions packages/kit/test/prerendering/trailing-slash/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="%svelte.assets%/favicon.png" />
%svelte.head%
</head>
<body>
%svelte.body%
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>hello</h1>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions packages/kit/test/prerendering/trailing-slash/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import path from 'path';
import adapter from '../../../../adapter-static/index.js';

/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter({
fallback: '200.html'
}),

prerender: {
default: true
},

trailingSlash: 'always',

vite: {
build: {
minify: false
},
clearScreen: false,
server: {
fs: {
allow: [path.resolve('../../../src')]
}
}
}
}
};

export default config;
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.