Skip to content

Commit

Permalink
chore: throw more helpful error when encoding uri fails during preren…
Browse files Browse the repository at this point in the history
…dering (#9053)

closes #9045
  • Loading branch information
dummdidumm authored Feb 15, 2023
1 parent ec4242f commit 8a950aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-teachers-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

chore: throw more helpful error when encoding uri fails during prerendering
14 changes: 7 additions & 7 deletions packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pathToFileURL } from 'node:url';
import { installPolyfills } from '../../exports/node/polyfills.js';
import { mkdirp, posixify, walk } from '../../utils/filesystem.js';
import { should_polyfill } from '../../utils/platform.js';
import { is_root_relative, resolve } from '../../utils/url.js';
import { decode_uri, is_root_relative, resolve } from '../../utils/url.js';
import { escape_html_attr } from '../../utils/escape.js';
import { logger } from '../utils.js';
import { load_config } from '../config/index.js';
Expand Down Expand Up @@ -207,15 +207,15 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
// this seems circuitous, but using new URL allows us to not care
// whether dependency_path is encoded or not
const encoded_dependency_path = new URL(dependency_path, 'http://localhost').pathname;
const decoded_dependency_path = decodeURI(encoded_dependency_path);
const decoded_dependency_path = decode_uri(encoded_dependency_path);

const headers = Object.fromEntries(result.response.headers);

const prerender = headers['x-sveltekit-prerender'];
if (prerender) {
const encoded_route_id = headers['x-sveltekit-routeid'];
if (encoded_route_id != null) {
const route_id = decodeURI(encoded_route_id);
const route_id = decode_uri(encoded_route_id);
const existing_value = prerender_map.get(route_id);
if (existing_value !== 'auto') {
prerender_map.set(route_id, prerender === 'true' ? true : 'auto');
Expand Down Expand Up @@ -257,7 +257,7 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
}

if (hash) {
const key = decodeURI(pathname + hash);
const key = decode_uri(pathname + hash);

if (!expected_hashlinks.has(key)) {
expected_hashlinks.set(key, new Set());
Expand All @@ -266,7 +266,7 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
/** @type {Set<string>} */ (expected_hashlinks.get(key)).add(decoded);
}

enqueue(decoded, decodeURI(pathname), pathname);
enqueue(decoded, decode_uri(pathname), pathname);
}
}
}
Expand All @@ -293,7 +293,7 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
if (written.has(file)) return;

const encoded_route_id = response.headers.get('x-sveltekit-routeid');
const route_id = encoded_route_id != null ? decodeURI(encoded_route_id) : null;
const route_id = encoded_route_id != null ? decode_uri(encoded_route_id) : null;
if (route_id !== null) prerendered_routes.add(route_id);

if (response_type === REDIRECT) {
Expand All @@ -302,7 +302,7 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
if (location) {
const resolved = resolve(encoded, location);
if (is_root_relative(resolved)) {
enqueue(decoded, decodeURI(resolved), resolved);
enqueue(decoded, decode_uri(resolved), resolved);
}

if (!headers['x-sveltekit-normalize']) {
Expand Down
15 changes: 15 additions & 0 deletions packages/kit/src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ export function decode_params(params) {
return params;
}

/**
* The error when a URL is malformed is not very helpful, so we augment it with the URI
* @param {string} uri
*/
export function decode_uri(uri) {
try {
return decodeURI(uri);
} catch (e) {
if (e instanceof Error) {
e.message = `Failed to decode URI: ${uri}\n` + e.message;
}
throw e;
}
}

/**
* URL properties that could change during the lifetime of the page,
* which excludes things like `origin`
Expand Down

0 comments on commit 8a950aa

Please sign in to comment.