Skip to content

Commit

Permalink
breaking: fix path resolution (#11276)
Browse files Browse the repository at this point in the history
* breaking: fix path resolution

* use prettier they said, it will make development faster they said

---------

Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
Rich-Harris and Rich-Harris authored Dec 13, 2023
1 parent 9439190 commit 04fecb7
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 35 deletions.
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"
],
"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;

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');
};

0 comments on commit 04fecb7

Please sign in to comment.