-
-
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
Redirects in load
functions break when running client-side
#5952
Comments
As a simple workaround I've done the following: const location = '/login';
if (browser) return await goto(location);
else throw redirect(302, location); This worked for me in browser and SSR, but yes ideally EDIT: may not work with links using |
I'm experiencing the same situation without disabling SSR: // file: src/routes/+layout.ts
import {
envelopeGuard,
envelopeGuardRedirect,
} from '$lib/utils/guards';
import { accounts } from '$lib/stores/accountsStore';
import { get } from 'svelte/store';
export async function load({ url }) {
const { pathname } = url;
const accountsArray = get(accounts);
const isRedirect = envelopeGuard({ pathname, accountsArray });
const location = '/';
if (isRedirect) {
throw envelopeGuardRedirect(location);
} else {
return {
key: pathname,
};
}
} // file: src/lib/utils/guards.ts
import { redirect } from '@sveltejs/kit';
export function envelopeGuard({ pathname, accountsArray }) {
if (
accountsArray &&
accountsArray.length === 0 &&
pathname === '/create-envelope'
) {
return true;
}
return false;
}
export const envelopeGuardRedirect = (location: string) =>
redirect(302, location); Then when triggering the guard and navigating to
|
Using |
This is happening because of a failed This line in
...is being transformed by Vite into this: import { HttpError, Redirect } from '/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/index/private.js'; Later, when your code gets imported, it's turned into this: import { base } from "/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/app/paths.js";
import { redirect } from "/node_modules/.vite/deps/@sveltejs_kit.js?v=5bea743a";
export function load() {
throw redirect(307, base + "/about");
} That We can prevent the // from client.js
import { HttpError, Redirect } from '/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/index/private.js';
// from index.js?v=e48ba282, which is imported by your `+page.ts` (it exports `redirect`)
import { HttpError, Redirect } from '/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/index/private.js?v=e48ba282'; You'll notice it's the same URL, but with a cachebusting query parameter. I'm at the limits of my Vite knowledge, but maybe @bluwy or @dominikg would understand how to ensure that the query parameter is always present or always absent? I suspect it's related to the fact that we're importing client/start.js in the SSR'd HTML without going via |
This is outside the portions of Vite I'm familiar with. Hopefully @bluwy or @dominikg will know. Leaving a few pointers here of what I did find. It looks to me like it has something to do with optimized deps as you mentioned: Also, there's a method that can be used for removing it: As to whether it should always or never be there, I'm not sure. |
This comment was marked as duplicate.
This comment was marked as duplicate.
As a simple workaround I created a function for now: import { browser } from '$app/env'
import { redirect } from '@sveltejs/kit'
import { goto } from '$app/navigation'
const redirectWorkaround: typeof redirect = (status, location) => {
if (!browser) throw redirect(status, location)
else {
goto(location)
}
} EDIT: as @madeleineostoja pointed out: make sure you don't have |
as @WaltzingPenguin said, using |
It would be nice if Redirect also prevents child load functions that are awaiting the current load function from executing |
@bdanoit agree, but please don't piggyback on unrelated issues — something like that deserves a new issue with a repro |
load
functions break if SSR is disabledload
functions break when running client-side
IFAIA I'm not sure yet if kit/packages/kit/src/vite/dev/index.js Line 66 in eecbbfc
I might have to spin up locally to see what's up. |
With regards to Windows: Maybe this needs the same treatment as this? kit/packages/kit/src/vite/dev/index.js Lines 290 to 295 in eecbbfc
|
a hunch i haven't yet tested is that if we used the same The logic is basically this: if import { start } from '/node_modules/@sveltejs/kit/src/runtime/client/start.js'; ...otherwise (i.e. it's in a workspace root), use import { start } from '/@fs/path/to/repo/node_modules/@sveltejs/kit/src/runtime/client/start.js'; As I understand it that would match Vite's internal resolution logic more closely. As things stand we're doing some things with |
We should watch to see if this fixes it: vitejs/vite#9730 and vitejs/vite#9759 update from blu: it doesn't fix it |
Is there any way to mark a route as “never prefetch” with |
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. |
@Rich-Harris This looks quite similar with vitejs/vite#7621. We are using absolute fs paths to import modules, too, and the version hash made the same module to be different in the browser. The reason is something like:
If the version hash is the root cause, I think vitejs/vite#9730 might help. But @benmccann said no, so there might have some other issues here 🤔 |
Is it worth a temporary patch to remove the use of const redirect_symbol = Symbol.for('sveltekit:redirect')
export class Redirect {
[redirect_symbol] = true;
constructor(status, location) {
this.status = status;
this.location = location;
}
}
export function is_redirect(obj) {
return (redirect_symbol in obj)
} |
I agree, this bug is preventing me from migrating my app because it breaks key functionalities. |
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 |
Progress update — there's now an PR against Vite that fixes this issue: vitejs/vite#9848 |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
It's merged 🥹🎉 |
Just bumped all my deps and it's working perfectly for me, looks like this issue can be closed 🎉 |
Retested the initial reproduction with these packages:
Error is still present. |
It's been merged to main, but not released. |
Oh, then I have no idea why my env started working. Sorry for the false positive |
Okay NOW it’s released, but in the It might be worth bumping vite now while leaving this open to track, since it’s such a breaking issue on sveltekit. Or at least mentioning it in the warning in the docs so users can override the version themselves |
There should be at update/warning update for users about vite new version(3.1.0-beta.1). |
Just do a server side redirect |
Describe the bug
There appears to be no way to use the new redirect method and disable SSR globally.
Reproduction
src/routes/page.ts
with the contents:src/hooks.ts
with the contents:Repo: https://github.com/WaltzingPenguin/sveltekit-redirect-hooks
Logs
No response
System Info
Severity
blocking an upgrade
Additional Information
No response
The text was updated successfully, but these errors were encountered: