-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Describe the problem
We're deploying svelte kit under different subpaths of our domain (e.g. our-domain.tld/app1/ our-domain.tld/app2/ etc). It's a bit tedious to always have to remember to include the base when linking to a local app path:
<script>
import { base } from "$app/paths";
</script>
<a href="{base}/local/application/path">Link</a>Describe the proposed solution
It would be very nice if ~ could be used as syntactic sugar for {base} in the href prop on links, and when using navigation functionality like the SvelteKit provided goto method etc:
<a href="~/local/application/path">Link</a>One obvious problem with this is that using ~ in routes could cause problems when creating relative links.
Alternatives considered
One alternative is to just create a custom link component:
<script>
import { base } from "$app/paths";
export let href;
// other props
function transformUrl(url) {
if (!url.startsWith("~")) return url;
return base + url.slice(1);
}
</script>
<a href={transformUrl(href)}>
<slot />
</a>One disadvantage with this is that you have to handle how to style the anchor tags somehow. And also this doesn't cover things like goto. To fix this, you could just export the transformUrl function and use that directly. However, when you have to write <a href={tranformUrl("~/my/link")}>link</a> you might be better off just sticking to <a href="{base}/application/path">link</a>
I started to look into if you could create a vite plugin for this, but it seems a bit complicated to cover all cases (like goto and links generated through functions etc).
As a side note, we already have %sveltekit.assets%, so adding ~ hopefully wouldn't add that much overhead.
Importance
nice to have
Additional Information
Would make it more aesthetically pleasing and ergonomic to create links, but using {base} works perfectly fine. I also understand that this might lead to undesirable breaking changes for people relying on ~ in their routes, hence might not be worth doing.
Nevertheless, I feel it's worth sharing that having to add {base} to every URL in your app feels kind of awkward.