-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Base hrefs are not supported in prerendering #8559
Comments
Yeah, I think We could in theory treat the existence of a Accounting for |
Thanks for looking into this! Well, on most cases with a present I suppose the best of both worlds would be to take the base Btw it might be useful to expose the function that generates the relative paths. Things like |
A warning is a good idea, I like that. The <base href="{'../'.repeat($page.url.pathname.split('/').length - 1)}/{lang}/"> A tiny bit clunky but arguably simple enough that we don't need to add it to the framework |
I am also in need of This setup works fine with Next.js because it uses absolute paths by default: Unrelated to OP's issue, but related to mine: SvelteKit renders
It is not a big deal but I thought I should ask. |
together with setting config.kit.paths.relative to false this closes #8559
Describe the bug
It appears that base hrefs (e.g.
<base href="/en/">
) are not supported at the moment. I stumbled against two issues.One issue is that the prerenderer/crawler grabs all hrefs and resolves the relative references against the current path. The result is something like this:
subsection
resolved from/en/section
=>/section/subsection
section
resolved from/en/section/subsection
=>/section/section
Note that this works fine in the browser, the problem is only with the prerenderer. I tinkered around with the code and managed to fix it by resolving against
base
in this line and thebase
itself is fairly easy to obtain in thecrawl()
that's called above.The second issue is that the asset paths are relative. And the base href screws up all of them as
./_app
gets resolved to/en/_app
in the browser. This could be solved by allowing to turn off the asset relativity. It might be as simple as allowing/
inconfig.kit.paths.assets
. Currently it is refused by a param check with theError: config.kit.paths.assets option must be an absolute path, if specified.
andError: config.kit.paths.assets option must not end with '/'.
checks.Reproduction
https://github.com/tontonsb/sveltekit-base-prerendering
Try
npm run dev
and visithttp://localhost:5173/en
to observe that you can either follow links or directly visit/en
,/en/section
and/en/section/subsection
.To observe the problem with the crawler:
npm run build
and you'll seeNot found: /section/subsection
git checkout lang-optional
andnpm run build
. Now/section/subsection
will work (as the[[lang=lang]]
prefix is optional in this branch), but you'll stumble againstNot found: /section/section
when it tries to resolvesection
from/section/subsection
.config.kit.prerender.handleHttpError = 'warn'
will throw you into infinite404 /section/section/section/sect...
:)To observe the problem with assets:
git checkout assets
andnpm run build && npm run preview
.http://localhost:4173/en
.Not found: /en/_app/immutable/start-c96f4d66.js
.Logs
Example of the crawler error when running
npm run build
:Example of the asset errors after
npm run build && npm run preview
in branchassets
:Error: Not found: /en/_app/immutable/start-c96f4d66.js at resolve (file:///home/juris/sveltekit-base-prerendering/.svelte-kit/output/server/index.js:2493:18) at resolve (file:///home/juris/sveltekit-base-prerendering/.svelte-kit/output/server/index.js:2358:34) at Object.#options.hooks.handle (file:///home/juris/sveltekit-base-prerendering/.svelte-kit/output/server/index.js:2537:59) at respond (file:///home/juris/sveltekit-base-prerendering/.svelte-kit/output/server/index.js:2356:43) at processTicksAndRejections (node:internal/process/task_queues:96:5)
System Info
Severity
serious, but I can work around it
Additional Information
It seems that localization with a path prefix is not really possible unless one is willing to manually import a lang store and add a
/{$lang}/
prefix to all internal links.While some workarounds could be done as outlined above, the best solution would be official support for path prefixes. Be it with base href or other method of prepending the prefix to internal links.
On a related note it would be good to support something like a set of prerendering param values. E.g. I would like to render all my pages for
lang in ['', 'en']
even if no internal link points to some of them. Something like the['*']
inconfig.kit.prerender.entries
, but with fixed param values. In the case of prefix param it might even be something like['/*', '/en/*']
in the same config param.The text was updated successfully, but these errors were encountered: