Skip to content

redirect does not work when called from separate library #13816

Closed
@Mimetis

Description

@Mimetis

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions