Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Svelt redirect not working when used in a seperate library #12914

Closed
@brettchaldecott

Description

@brettchaldecott

Describe the bug

We are developing a third-party library that integrates with an OAuth provider. As part of the OAuth process, a user redirect is required. When using the redirect function directly within a SvelteKit app, the redirect works correctly. However, when this function is implemented in a third-party Svelte library and then linked to the SvelteKit project via npm link, the redirect no longer functions as expected.

This issue appears to be specific to Svelte version 5, as it works properly with version 4.

Reproduction

Create a new svelte library

npx sv create test-lib
cd test-lib
npm install

Create a redirect handler in the test library

The redirect handler in the lib path: lib->auth->authHandler.ts

import type {RequestEvent} from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';

export async function authHandler ({
    request,
    params,
    url: originURL,
  }: RequestEvent) {
    return redirect(
      // @ts-ignore
      302,
      "/test_stuff/"
    );
  }

Add the exports

Create the appropriate index.ts files to export the authHandler function: lib->auth->index.ts

export * from './authHandler.js'

And a further one in the lib directory: lib->index.ts

export * from './auth';

Compile the library
npm run build

Create a new sveltekit server

npx sv create test-server
cd test-server
npm install

Create an API call

Create the +server.ts in routes->api->auth->+server.ts

import {authHandler} from 'test-lib';
import type {RequestEvent} from '@sveltejs/kit';
import { redirect, Redirect } from '@sveltejs/kit';

export async function GET(requestEvents: RequestEvent) {
   await authHandler(requestEvents);
}

Invoke the command

curl http://localhost:5173/api/auth

Logs

Request event
Error: {"status":302,"location":"/test_stuff"}
    at Module.coalesce_to_error (/Users/brettchaldecott/Development/upwork/kinde/sveltekit/test-server/node_modules/@sveltejs/kit/src/utils/error.js:11:5)
    at Module.handle_fatal_error (/Users/brettchaldecott/Development/upwork/kinde/sveltekit/test-server/node_modules/@sveltejs/kit/src/runtime/server/utils.js:72:47)
    at resolve (/Users/brettchaldecott/Development/upwork/kinde/sveltekit/test-server/node_modules/@sveltejs/kit/src/runtime/server/respond.js:547:17)
    at async Module.respond (/Users/brettchaldecott/Development/upwork/kinde/sveltekit/test-server/node_modules/@sveltejs/kit/src/runtime/server/respond.js:327:20)
    at async file:///Users/brettchaldecott/Development/upwork/kinde/sveltekit/test-server/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22

System Info

System:
    OS: macOS 14.6.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 36.06 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.18.0 - ~/.nvm/versions/node/v20.18.0/bin/node
    Yarn: 1.22.11 - /usr/local/bin/yarn
    npm: 10.8.2 - ~/.nvm/versions/node/v20.18.0/bin/npm
    pnpm: 9.12.3 - ~/.nvm/versions/node/v20.18.0/bin/pnpm
  Browsers:
    Brave Browser: 130.1.71.114
    Chrome: 130.0.6723.70
    Safari: 17.6
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.3.1 
    @sveltejs/kit: ^2.0.0 => 2.7.3 
    @sveltejs/package: ^2.0.0 => 2.3.7 
    @sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.0 
    svelte: ^5.0.0 => 5.1.4 
    vite: ^5.0.11 => 5.4.10

Severity

blocking an upgrade

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions