kit: Redirects in `load` functions break when running client-side
Describe the bug
There appears to be no way to use the new redirect method and disable SSR globally.
Reproduction
- Create a new skeleton project
- Create
src/routes/page.tswith the contents:
import { redirect } from "@sveltejs/kit";
export function load() {
throw redirect(307, "/about")
}
- Create
src/hooks.tswith the contents:
import type { Handle } from '@sveltejs/kit'
export const handle: Handle = async ({ event, resolve }) => {
return resolve(event, { ssr: false })
}
- Navigating to root now results in an error page, with the redirect class passed to it, instead of being redirected to “/about”
Repo: https://github.com/WaltzingPenguin/sveltekit-redirect-hooks
Logs
No response
System Info
System:
OS: Windows 10 10.0.19043
CPU: (12) x64 AMD Ryzen 5 1600 Six-Core Processor
Memory: 7.50 GB / 15.95 GB
Binaries:
Node: 16.15.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
npm: 7.20.3 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 104.0.5112.81
Edge: Spartan (44.19041.1266.0), Chromium (104.0.1293.54)
Internet Explorer: 11.0.19041.1566
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.64
@sveltejs/kit: next => 1.0.0-next.413
svelte: ^3.44.0 => 3.49.0
vite: ^3.0.0 => 3.0.8
Severity
blocking an upgrade
Additional Information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 22
- Comments: 41 (24 by maintainers)
Commits related to this issue
- Update to latest Svelte Beta Resolves https://github.com/sveltejs/kit/issues/5952 — committed to Marcel-G/sobaka-sample by Marcel-G 2 years ago
Progress update — there’s now an PR against Vite that fixes this issue: https://github.com/vitejs/vite/pull/9848
As a simple workaround I’ve done the following:
This worked for me in browser and SSR, but yes ideally
redirect()should work client side.EDIT: may not work with links using
sveltekit:prefetchhttps://github.com/vitejs/vite/pull/9848
It’s merged 🥹🎉
I’ve opened an issue on the Vite repo:
Would love to try and get it fixed properly rather than bodging in a workaround, as part of the reason for the
instanceofchecks is that TypeScript throws a hissy fit without themUsing
gotoas a workaround breaks as soon as a link hassveltekit:prefetchenabled. Hovering over the link will immediately redirect without requiring a click.I agree, this bug is preventing me from migrating my app because it breaks key functionalities.
Can we please also have the warning in the migration guide https://github.com/sveltejs/kit/discussions/5774 on the “Redirects” section? I was wasting quite a lot of time until I consulted the docs and found this issue.
Unless it’s slated to be resolved very soon it might be worth throwing a warning in the docs, because it’ll catch out anyone that tries to use
throw redirectorthrow errorinloadand leave them scratching their heads atm. This is the last major issue stopping me from pushing a fairly large app migrated to the ‘new’ sveltekit live so hoping it gets sorted upstream soon 🤞🏼I think calling goto in the +page.svelte file should work. Its far from ideal but if it works it can be a temporary workaround.
I’m experiencing the same situation without disabling SSR:
Then when triggering the guard and navigating to
/create-envelopeI get:Is it worth a temporary patch to remove the use of
instanceoffrom SvelteKit until the correct solution can be figured out with Vite? I have encountered a similar issue in the past and just usedSymbols to work around it, instead of digging into Vite internals.We should watch to see if this fixes it: https://github.com/vitejs/vite/pull/9730 and https://github.com/vitejs/vite/pull/9759
update from blu: it doesn’t fix it
a hunch i haven’t yet tested is that if we used the same
runtime_baselogic forclient/start.jsas we do for other things, everything would get resolved the same way.The logic is basically this: if
@sveltejs/kitis installed inside the project, do this……otherwise (i.e. it’s in a workspace root), use
/@fs:As I understand it that would match Vite’s internal resolution logic more closely. As things stand we’re doing some things with
/node_modules/...and other things with/@fs, which might be at least part of the problem here.as @WaltzingPenguin said, using
gotoas a workaround breaks withsveltekit:prefetchwhich will instantly trigger the redirectAs a simple workaround I created a function for now:
EDIT: as @madeleineostoja pointed out: make sure you don’t have
sveltekit:prefetchenabled for these routes if you use this workaround.