question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Actions redirect using location is not working on +page.server

See original GitHub issue

Describe the bug

When returning a location in a +page.server.js action, no redirect happens.

Not sure if I’m doing something wrong, but that’s what the docs say: https://kit.svelte.dev/docs/routing#page-actions

export const POST = async ({ request, locals }) => {
    if (true) {
        console.log('Executed');
	return {
	    location: '/'
	};
    }
}

Reproduction

Here’s a link to a minimal reproduction: https://stackblitz.com/edit/sveltekit-actions-location-redirect-not-working?file=src/routes/todos/+page.server.js

  • Open the link
  • run npm install
  • run npm run dev
  • Navigate to the ToDo page
  • Create a new ToDo

You will find the issue in file src/routes/todos/+page.server.js in line 43.

Logs

No response

System Info

System:
    OS: Linux 5.15 Manjaro Linux
    CPU: (4) x64 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
    Memory: 1.91 GB / 7.65 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.7.0 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.17.0 - /usr/bin/npm
  Browsers:
    Chromium: 104.0.5112.79
    Firefox: 103.0.2
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.64 
    @sveltejs/kit: next => 1.0.0-next.424 
    svelte: ^3.44.0 => 3.49.0 
    vite: ^3.0.2 => 3.0.9

Severity

blocking an upgrade

Additional Information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tobylewiscommented, Aug 20, 2022

@stausmar - do your redirect in psge.server.js with return {location:'/whatever'} and then inside your fetch handler in page.svelte do if (response.redirected){ goto(response.headers.location)}

1reaction
stausmarcommented, Aug 20, 2022

Does the location redirect only work with forms? If I call that function using fetch, then it does not work.

Please see the following file src/routes/product/+page.server.js https://stackblitz.com/edit/sveltekit-actions-location-redirect-not-working?file=src/routes/product/+page.server.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

window.location.href doesn't redirect - Stack Overflow
It will run the code in the function and then submit the page instead of redirect. In this case change the type tag...
Read more >
UI Action Redirect Not Working - ServiceNow Community
Solved: I am trying to create a UI Action that will redirect the current user (either through the regular interface or CMS page)...
Read more >
4 Ways to Use JavaScript to Redirect or Navigate to A URL or ...
Many URLs are redirected to new locations, typically using a server configuration and HTTP header properties. But there are many scenarios ...
Read more >
How to Use JavaScript to Redirect a URL - Semrush
Learn 3 of the most common ways to redirect a URL using JavaScript the location object: setting a new window.location.href property, ...
Read more >
Authenticate Using Google with JavaScript - Firebase
You can prompt your users to sign in with their Google Accounts either by opening a pop-up window or by redirecting to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found