Skip to content

redirect does not work when called from separate library #13816

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

Open
Mimetis opened this issue May 23, 2025 · 2 comments
Open

redirect does not work when called from separate library #13816

Mimetis opened this issue May 23, 2025 · 2 comments

Comments

@Mimetis
Copy link

Mimetis commented May 23, 2025

Describe the bug

This bug has been already addressed here : #12914
Since it's closed, and the bug remains, I re open another one.

The redirect method from @sveltejs/kit, when called from another library, is not working, and return a json object containing only the Redirect object, serialized.

The action code, from the application itself, called my-app, is pretty simple :

import { createMyLibraryRedirectFunction } from 'my-library';
import type { Actions } from './$types';

export const actions = {
	default: async (event) => {
		const data = await event.request.formData();

		const url = data.get('url');
		if (typeof url !== 'string') throw new Error('Invalid URL');

		createMyLibraryRedirectFunction(url);
	}
} satisfies Actions;

and the code from createMyLibraryRedirectFunction from the library, called my-library is also really simple:

import { redirect } from "@sveltejs/kit";

export function createMyLibraryRedirectFunction(url: string = "/") {
  redirect(302, url);
}

The result is

  • No redirect happens
  • A json payload in the console
Redirect {
  status: 302,
  location: 'https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=....'
}

After some investigations, as mentioned in the other issue, this code is causing the failure:

} catch (e) {
const err = normalize_error(e);
if (err instanceof Redirect) {
return action_json_redirect(err);
}

instanceof is not working as expected.

I have tried several things:

  • Publish the library to npmjs to be sure we don't have multiple @sveltejs/kit that could cause that kind of error
  • Put @sveltejs/kit in different places in the package.json : dependencies, devDependencies, peerDependencies
  • Returning a Response instead of calling redirect (forbidden in Actions, by design)

Reproduction

I ve created a repository containing my-app and my-library to reproduce the issue : https://github.com/[Mimetis/svelte-issue-redirect-from-library](https://github.com/Mimetis/svelte-issue-redirect-from-library)

From the my-library, launch a npm run dev to bundle the library:

> npm run dev

rollup v4.41.0
bundles src/index.ts → dist...
created dist in 505ms

from the my-app

  • launch the application using npm run dev.
  • click on the button to trigger the action (that will call the library)

Instead of being redirected, we have a terminal output like this:

  VITE v6.3.5  ready in 724 ms

  ➜  Local:   http://localhost:5174/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help
  
Redirect { status: 302, location: 'https://google.com' }

Logs

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (18) x64 12th Gen Intel(R) Core(TM) i9-12900K
    Memory: 44.17 GB / 50.99 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.15.0 - ~/.nvm/versions/node/v22.15.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.15.0/bin/npm
    bun: 1.0.1 - ~/.bun/bin/bun
  npmPackages:
    @sveltejs/adapter-auto: ^6.0.0 => 6.0.1 
    @sveltejs/kit: ^2.21.1 => 2.21.1 
    @sveltejs/vite-plugin-svelte: ^5.0.0 => 5.0.3 
    svelte: ^5.0.0 => 5.33.0 
    vite: ^6.2.6 => 6.3.5

Severity

annoyance

Additional Information

No response

@CaptainCodeman
Copy link
Contributor

Instead of using instanceof Redirect, you should use isRedirect

It's likely caused by having a copy of the Redirect class somewhere along the way so it isn't an instance of the exact same thing ... this can be avoided by not bundling it in your lib project (one possible cause)

@Mimetis
Copy link
Author

Mimetis commented May 24, 2025

Instead of using instanceof Redirect, you should use isRedirect

I'm not the one using instanceof Redirect ....

This part of code is coming from @sveltejs/kit:

if (err instanceof Redirect) {

I have updated the post to make more clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants