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

breaking: fix path resolution #11276

Merged
merged 2 commits into from
Dec 13, 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
5 changes: 5 additions & 0 deletions .changeset/twelve-paws-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
---

breaking: fix path resolution
2 changes: 1 addition & 1 deletion packages/kit/src/core/postbuild/fixtures/base/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"hrefs": [
"/base-path/styles.css",
"/base-path/favicon.png",
"https://external.com",
"https://external.com/",
"/base-path/wheee",
"/base-path/wheee2",
"/base-path/wheee3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"hrefs": [
"/styles.css",
"/favicon.png",
"https://external.com",
"https://external.com/",
"/wheee",
"/wheee2",
"/wheee3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"/styles.css",
"/favicon.png",
"/test&ampersand",
"/test\"quotation",
"/test♡decimal",
"/test♥hex"
"/test%22quotation",
"/test%E2%99%A1decimal",
"/test%E2%99%A5hex"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous results had the character references persisted - will this somehow affect other stuff negatively / in a breaking way now that they no longer are?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured if it did, tests would start failing. Seems fine. Though we're probably now double-decoding at prerender time

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof. Double-decoding is bad and would result in breakages. Maybe we don't have a test for prerendering an encoded URL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do:

If there's a bug, it's very well-hidden, and we can fix it when it reveals itself. It shouldn't block merging

],
"ids": []
}
2 changes: 1 addition & 1 deletion packages/kit/src/core/postbuild/fixtures/ids/output.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"hrefs": ["/#encöded"],
"hrefs": ["/#enc%C3%B6ded"],
"ids": ["before", "after", "encöded"]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"hrefs": ["/styles.css", "/favicon.png", "https://external.com"],
"hrefs": ["/styles.css", "/favicon.png", "https://external.com/"],
"ids": []
}
7 changes: 6 additions & 1 deletion packages/kit/src/core/postbuild/fixtures/meta/output.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"hrefs": ["https://external.com", "/og-image.jpg", "https://example.com/audio.mp3", "/video.mp4"],
"hrefs": [
"https://external.com/",
"/og-image.jpg",
"https://example.com/audio.mp3",
"/video.mp4"
],
"ids": []
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"hrefs": ["/styles.css", "/favicon.png", "https://external.com", "/wheee"],
"hrefs": ["/styles.css", "/favicon.png", "https://external.com/", "/wheee"],
"ids": []
}
31 changes: 6 additions & 25 deletions packages/kit/src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,20 @@ import { BROWSER } from 'esm-env';
*/
export const SCHEME = /^[a-z][a-z\d+\-.]+:/i;

const absolute = /^([a-z]+:)?\/?\//;
const internal = new URL('sveltekit-internal://');

/**
* @param {string} base
* @param {string} path
*/
export function resolve(base, path) {
if (SCHEME.test(path)) return path;
if (path[0] === '#') return base + path;
if (path === '') return base;
// special case
if (path[0] === '/' && path[1] === '/') return path;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this special case about? Is this some extra thing we need to mention in the docs? What does this mean for ''?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protocol-relative URLs should remain protocol-relative, but the new URL(...) technique means we lose that information. so we need to special-case it


const base_match = absolute.exec(base);
const path_match = absolute.exec(path);
let url = new URL(base, internal);
url = new URL(path, url);

if (!base_match) {
throw new Error(`bad base path: "${base}"`);
}

const baseparts = path_match ? [] : base.slice(base_match[0].length).split('/');
const pathparts = path_match ? path.slice(path_match[0].length).split('/') : path.split('/');

baseparts.pop();

for (let i = 0; i < pathparts.length; i += 1) {
const part = pathparts[i];
if (part === '.') continue;
else if (part === '..') baseparts.pop();
else baseparts.push(part);
}

const prefix = (path_match && path_match[0]) || (base_match && base_match[0]) || '';

return `${prefix}${baseparts.join('/')}`;
return url.protocol === internal.protocol ? url.pathname + url.search + url.hash : url.href;
}

/** @param {string} path */
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/src/utils/url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ describe('resolve', (test) => {
test('resolves empty string', () => {
assert.equal(resolve('/a/b/c', ''), '/a/b/c');
});

test('resolves .', () => {
assert.equal(resolve('/a/b/c', '.'), '/a/b/');
});
});

describe('normalize_path', (test) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { redirect } from '@sveltejs/kit';
/** @type {import('@sveltejs/kit').RequestHandler} */
export const GET = (event) => {
const needsEncoding = 'teapot, jane austen';
event.cookies.set('encoding', needsEncoding, { path: '.' });
event.cookies.set('encoding', needsEncoding, { path: '/cookies/encoding' });
redirect(303, '/cookies/encoding');
};