Skip to content

Commit

Permalink
Fix truncated basepath (#3345)
Browse files Browse the repository at this point in the history
* Fix svelte-kit dev/preview when config.kit.paths.base is set

* add test of truncated base

* changeset

* handle edge cases

* actually i guess this belongs here

* decode then check

* fix newly failing test

Co-authored-by: Andrew Soutar <andrew@andrewsoutar.com>
  • Loading branch information
Rich-Harris and andrewsoutar committed Jan 15, 2022
1 parent f92c3e2 commit eddea57
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-pets-vanish.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Handle requests for /basepath
8 changes: 7 additions & 1 deletion packages/kit/src/core/adapt/prerender/prerender.js
Expand Up @@ -229,7 +229,13 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
if (!is_root_relative(resolved)) continue;

const parsed = new URL(resolved, 'http://localhost');
const pathname = decodeURI(parsed.pathname).replace(config.kit.paths.base, '');

let pathname = decodeURI(parsed.pathname);

if (config.kit.paths.base) {
if (!pathname.startsWith(config.kit.paths.base)) continue;
pathname = pathname.slice(config.kit.paths.base.length) || '/';
}

const file = pathname.slice(1);
if (files.has(file)) continue;
Expand Down
7 changes: 6 additions & 1 deletion packages/kit/src/runtime/server/index.js
Expand Up @@ -105,7 +105,12 @@ export async function respond(incoming, options, state = {}) {
});
}

const decoded = decodeURI(request.url.pathname).replace(options.paths.base, '');
let decoded = decodeURI(request.url.pathname);

if (options.paths.base) {
if (!decoded.startsWith(options.paths.base)) return;
decoded = decoded.slice(options.paths.base.length) || '/';
}

for (const route of options.manifest._.routes) {
const match = route.pattern.exec(decoded);
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/test/apps/options-2/svelte.config.js
Expand Up @@ -3,6 +3,9 @@ import path from 'path';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
paths: {
base: '/basepath'
},
serviceWorker: {
register: false
},
Expand Down
16 changes: 9 additions & 7 deletions packages/kit/test/apps/options-2/test/test.js
Expand Up @@ -3,22 +3,24 @@ import { test } from '../../../utils.js';

/** @typedef {import('@playwright/test').Response} Response */

test.describe.parallel('Service worker', () => {
test('serves /', async ({ page }) => {
await page.goto('/');
test.describe.parallel('paths.base', () => {
test('serves /basepath', async ({ page }) => {
await page.goto('/basepath');
expect(await page.textContent('h1')).toBe('Hello');
});
});

test.describe.parallel('Service worker', () => {
if (!process.env.DEV) {
test('build /service-worker.js', async ({ request }) => {
const response = await request.get('/service-worker.js');
test('build /basepath/service-worker.js', async ({ request }) => {
const response = await request.get('/basepath/service-worker.js');
const content = await response.text();

expect(content).toMatch(/\/_app\/start-[a-z0-9]+\.js/);
});

test('does not register /service-worker.js', async ({ page }) => {
await page.goto('/');
test('does not register /basepath/service-worker.js', async ({ page }) => {
await page.goto('/basepath');
expect(await page.content()).not.toMatch(/navigator\.serviceWorker/);
});
}
Expand Down
@@ -1,7 +1,9 @@
<script context="module">
import { base } from '$app/paths';
/** @type {import('@sveltejs/kit').Load} */
export async function load({ url, fetch }) {
const res = await fetch('/origin.json');
const res = await fetch(`${base}/origin.json`);
const data = await res.json();
return {
Expand Down

0 comments on commit eddea57

Please sign in to comment.