Open charlyoleg2 opened 9 months ago
The base
is set to a relative path during SSR.
https://github.com/sveltejs/kit/blob/4ae1cd152a60a22044ab3b8aa5572a2b79864583/packages/kit/src/runtime/server/page/render.js#L99-L101
However, navigating to another page does not re-render the root layout so the href stays the same (which is incorrect if we're on a different page).
For example, if the base is /parame51
, loading the route /parame51/
will render the href
as ./
. But when we navigate to /parame51/example
the href
is still ./
.
You can workaround this by setting kit.config.paths.relative
to false
. This will always use an absolute URL for anchor elements.
EDIT: The base
seems to be repaired when it reaches the client. But I can't figure out why that isn't in the case in your reproduction.
https://stackblitz.com/edit/sveltejs-kit-template-default-gpxktc?file=src%2Froutes%2FHeader.svelte
I can't reproduce the issue with the given reproduction or my own. The repository you have linked requires installing packages from a private(?) registry.
I have create a more minimalist code base: https://github.com/charlyoleg2/link_issue npm run build npm run preview load the homepage, go to sub2, use the homepage link. You are in sub1, which is unexpected.
I encountered the same issue as well.
I found it that an <a>
tag with href
defined in base
had an unexpected pathname
property when a page transition was occured.
After playing with svelte.config.js, I see that setting paths.relative to false makes things worse: On each page, the homepage-link points to the page itself. And 'npm run dev' has link issues.
With the two following changes, it works:
1- use a trailing slash in the url with base
<a href="{base}/">home</a>
2- in svelte.config.js, set paths.relative to false
Now the homepage-link works as expected in preview and debug mode
To summarize, we have four cases: 1- paths.relative = false and url with trailing slash: No bug, it works 2- paths.relative = false and url without trailing slash: Buggy 3- paths.relative = true and url with trailing slash: Buggy 4- paths.relative = true and url without trailing slash: Buggy
Setting "paths.relative = false" resolved my problem. commit: https://github.com/ykyki/blog/pull/13/commits/5d4e0ca0fd0647ccf77c7947f5c1a931ef8be2d7
If this modification is not made, it will work correctly in develop mode but not in preview mode.
Describe the bug
In +layout.svelte, i have a link
<a href="{base}">home</a>
. For the homepage (src/routes/+page.svelte) and the sub1-page (src/routes/sub1/+page.svelte), the link brings to the expected page (i.e. the homepage). But for the sub2-page (src/routes/sub1/sub2/+page.svelte), the link brings to the sub1-page. I'm using the adapter-static. The bug is present if you start the sveltekit at the homepage: Load the homepage, then navigate to sub2-page, then use the link and you get the bug. But if you start the sveltekit at the sub2-page, the bug is not present: Load the sub2-page, then use the link, you are now at the homepage as expected.Reproduction
You can experiment the bug on this github page: https://charlyoleg2.github.io/parame51 The +layout.svelte that contains the buggy link: https://github.com/charlyoleg2/parame51/blob/main/pkg/desi51-ui/src/routes/%2Blayout.svelte
Logs
System Info
Severity
annoyance
Additional Information
No response