Skip to content

Commit db812b3

Browse files
committed
fix: resolve fetch issue with same host URLs differing from paths.base
1 parent 8e3586b commit db812b3

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

.changeset/honest-cobras-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': major
3+
---
4+
5+
fix: `fetch` not working when URL is same host but different than `paths.base`

packages/kit/src/runtime/server/respond.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,9 @@ export async function internal_respond(request, options, manifest, state) {
285285
let route = null;
286286

287287
if (base && !state.prerendering?.fallback) {
288-
if (!resolved_path.startsWith(base)) {
289-
return text('Not found', { status: 404 });
288+
if (resolved_path.startsWith(base)) {
289+
resolved_path = resolved_path.slice(base.length) || '/';
290290
}
291-
resolved_path = resolved_path.slice(base.length) || '/';
292291
}
293292

294293
if (is_route_resolution_request) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export async function load({ fetch }) {
2+
const response = await fetch('/not-base-path/');
3+
return {
4+
responseContentType: response.headers.get('content-type')
5+
};
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
/** @type {{ data: { responseContentType: string } }} */
3+
const { data } = $props();
4+
</script>
5+
6+
<p>{data.responseContentType}</p>

packages/kit/test/apps/options/test/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ test.describe('Routing', () => {
236236
await page.click('[href="/path-base/routing/link-outside-app-target/target/"]');
237237
await expect(page.locator('h2')).toHaveText('target: 0');
238238
});
239+
240+
test('fetch outside base path succeeds', async ({ request }) => {
241+
const response = await request.get('/path-base/routing/link-outside-base/');
242+
expect(response.status()).toBe(200);
243+
expect(await response.text()).toContain('text/plain');
244+
});
239245
});
240246

241247
test.describe('Async', () => {

0 commit comments

Comments
 (0)