-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: set dynamic base when rendering page #9220
Conversation
This now works for both |
"is this a breaking change or not" - tricky. I don't know how people use Will this have any weird implications on hydration (or possible future work in that area in Svelte core)? With this change, hydration changes the links. |
One solution to the breaking change question could be to make the behaviour opt-in: export default {
kit: {
paths: {
base: '.',
assets: '.' // probably unnecessary to specify this, since `assets` defaults to `base`
}
}
}; That way if someone is doing something esoteric like styling internal links differently... a[href^=/] {
color: red;
} ...the behaviour won't change. We could swap the default round in 2.0. One consideration around hydration is whether changing an |
Could we combine the two changesets into one? I hate to say it, but it does seem like this could be breaking
We should probably make it |
Yeah, I'm working on the
|
I'd be curious why |
Implementation-wise it was a clusterfuck. But it's also just confusing API, and prevents you from saying whether you want to use relative paths independently of whether there's a base path configured |
Just tested with my app and it works, thanks so much for supporting this feature. Edit: this was not correct: It would be great if sveltekit could support the following when
and transform those absolute path to relative otherwise, we have to wrap the url into a function to make them use of
import { base } from '$app/paths';
export function url(p: string) {
return `${base}${p}`;
} |
That's a separate feature request, and one that we've considered and refused: #8669 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments. General observation (but not for this PR): render.js
is becoming very big, maybe that method could benefit from some splits by concern so that if you're only looking for a specific thing in there you know you only have to look into method X - although this probably gets hard to do right since it's all concatenated into a string in the end.
|
||
if (segments.length === 1 && paths.base !== '') { | ||
// if we're on `/my-base-path`, relative links need to start `./my-base-path` rather than `.` | ||
base = `./${paths.base.split('/').at(-1)}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why split.at(-1)? What if the base path spans more than one /
, like /foo/bar
? We know the base has to start with a slash, so why not
base = `./${paths.base.split('/').at(-1)}`; | |
base = `.${paths.base}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider a basepath like /a/b/c
. If you're rendering that route, then {base}/d
should resolve to /a/b/c/d
.
With non-relative paths, that's easy — just use base
as written. But if you're using a relative path, then it needs to be ./c
to produce ./c/d
to resolve to /a/b/c/d
(or ../b/c/d
, or ../../a/b/c/d
, but there's no point in doing that).
With your suggestion, it would be ./a/b/c
, which would resolve to /a/b/a/b/c/d
In my opinion this is different as here no special path syntax would be required. Happy to open a new issue if there is a chance it could be re-examined in that context |
It's the same thing whether there's leading path syntax or not — API-wise it's highly confusing, and implementation-wise it's virtually impossible. You can use |
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Agree, this has been on my wishlist for a while. I actually made a start on it recently, but scrapped it because it would have interfered with the PR in question — it should really be a pure refactor unattached to any other changes |
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
Well done 🎉 🚀 . Thank you for adding this support 🙇🏼♂️. Previously I had to fork |
Fixes #9162 by providing a fallback for the base path when
global
is unavailableFollow-up to #9150. This sets
base
to a relative path when server-rendering, meaning that something like this......will be rendered like so, depending on the page being rendered:
When the element is hydrated, the attribute will change to reflect the actual base path:
This means that paths will work even if the site is running on the Internet Archive or somewhere. It needs some neatening up — ideally we wouldn't need to store
original_base
, and we also need to getassets
working the same way — but you get the idea. Will finish this up tomorrow.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.