Skip to content

Conversation

@goulinkh
Copy link

@goulinkh goulinkh commented Feb 9, 2026

closes #11078

When having a custom base path:

{
  kit: {
  	paths: {
  		base: '/ui'
  	}
  }
}

And making a request to a different server hosted on the same domain, +layout.server.ts:

export const load: LayoutServerLoad = async ({ fetch }) => {
  const workingResponse = await fetch('/ui/api'); // works if api is defined in sveltekit
  const failingResponse = await fetch('/profile'); // returns 404, instead of making request and providing cookies
}

Making a request to the same origin but to a different server not with the configured paths.base, currently doesn't work but this should be allowed and possible as they share the same domain name.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link

changeset-bot bot commented Feb 9, 2026

🦋 Changeset detected

Latest commit: 208b2ab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

1 similar comment
@svelte-docs-bot
Copy link

@goulinkh goulinkh force-pushed the fix-custom-base-fetch branch from 0df3081 to fe1a7a2 Compare February 9, 2026 16:15
@goulinkh goulinkh force-pushed the fix-custom-base-fetch branch from fe1a7a2 to db812b3 Compare February 9, 2026 16:22
@PatrickG
Copy link
Member

PatrickG commented Feb 9, 2026

I don't think this is the correct fix.
The request shouldn't even be handled by the respond function.
It must be fixed somewhere in the fetch function in src/runtime/server/fetch.js

Maybe changing

if (url.origin !== event.url.origin) {
to something like

if (url.origin !== event.url.origin || (paths.base && !decodeURIComponent(url.pathname).startsWith(paths.base))) {

@goulinkh
Copy link
Author

goulinkh commented Feb 9, 2026

Hi @PatrickG, apologies for the quick fix attempt, I lack knowledge in sveltekit codebase but tried to file the fix.

Your suggestion makes sense, I've applied it and it seems to work locally.

Cheers!

@goulinkh goulinkh requested a review from PatrickG February 11, 2026 10:59
async (r) => (result = await r.json())
);
}, 100);
}, 50);
Copy link
Member

Choose a reason for hiding this comment

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

Why was this reduced?

Copy link
Author

Choose a reason for hiding this comment

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

The test was flaky, it was failing randomly in the CI and locally, reducing the abort signal timing helped.

Should I revert this change?

@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'@sveltejs/kit': major
'@sveltejs/kit': patch

It should only be major for a breaking change (of a previously valid behaviour?). However, I don't think anyone would purposely try to have the load fetch trigger the SvelteKit server response if it's requesting an entirely different path

Comment on lines 83 to 86
expect(await page.locator('[data-testid="fetch-url"]').textContent()).toContain(
`${baseURL}/not-base-path/`
);
expect(await page.locator('[data-testid="fetch-response"]').textContent()).toContain(
Copy link
Member

Choose a reason for hiding this comment

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

Passing a locator and awaiting the expect is the recommended Playwright API

Suggested change
expect(await page.locator('[data-testid="fetch-url"]').textContent()).toContain(
`${baseURL}/not-base-path/`
);
expect(await page.locator('[data-testid="fetch-response"]').textContent()).toContain(
await expect(page.locator('[data-testid="fetch-url"]')).toHaveText(
`${baseURL}/not-base-path/`
);
await expect(page.locator('[data-testid="fetch-response"]')).toHaveText(

Comment on lines 91 to 115
test('fetch to root succeeds', async ({ page, baseURL }) => {
await page.goto('/path-base/fetch/link-root/');
// fetch to root with trailing slash
expect(await page.locator('[data-testid="fetch1-url"]').textContent()).toContain(`${baseURL}/`);
const fetch1Response = await page.locator('[data-testid="fetch1-response"]').textContent();
const fetch1Redirect = await page.locator('[data-testid="fetch1-redirect"]').textContent();
expect(
// production
fetch1Response?.includes('did you mean to visit') ||
// dev
fetch1Redirect === '/path-base'
).toBe(true);

// fetch to root without trailing slash should be relative
expect(await page.locator('[data-testid="fetch2-url"]').textContent()).toBeFalsy();
expect(await page.locator('[data-testid="fetch2-response"]').textContent()).toBe('relative');

// fetch to root with custom base path with trailing slash
expect(await page.locator('[data-testid="fetch3-url"]').textContent()).toBeFalsy();
expect(await page.locator('[data-testid="fetch3-response"]').textContent()).toBe('root');

// fetch to root with custom base path without trailing slash
expect(await page.locator('[data-testid="fetch4-url"]').textContent()).toBeFalsy();
expect(await page.locator('[data-testid="fetch4-redirect"]').textContent()).toBe('/path-base/');
});
Copy link
Member

@teemingc teemingc Feb 11, 2026

Choose a reason for hiding this comment

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

I'm not sure if we need this test. It doesn't help validate that the non-base path URL is fetchable. Does this test fail before the fix is applied?

Copy link
Author

Choose a reason for hiding this comment

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

no it was working before as well, I will remove the base path URL fetches 👍

Copy link
Member

@teemingc teemingc left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I left a few comments

@goulinkh goulinkh requested a review from teemingc February 11, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetch() does not work properly on the server when base path is set

3 participants