How to redirect in SvelteKit endpoints

Wanted to do redirects in your SvelteKit endpoints and tried to return { redirect: ‘/success’ } just to find out it doesn’t work?

Well, you’re in luck because you can just use the standard Location header to do redirects:

export async function get(…


This content originally appeared on DEV Community and was authored by Dana Woodman

Wanted to do redirects in your SvelteKit endpoints and tried to return { redirect: '/success' } just to find out it doesn't work?

Well, you're in luck because you can just use the standard Location header to do redirects:

export async function get() {

  // Do some magic here... ✨

  return {
    headers: { Location: '/success' },
    status: 302
  }
}

Remember to make sure use the proper status code for your redirect.

Learn more about the Location header on MDN.

Whelp, that's it for today, hope this saved you some headaches!

Follow me on Dev.to, Twitter and Github for more web dev and startup related content


This content originally appeared on DEV Community and was authored by Dana Woodman


Print Share Comment Cite Upload Translate Updates
APA

Dana Woodman | Sciencx (2021-04-21T02:29:55+00:00) How to redirect in SvelteKit endpoints. Retrieved from https://www.scien.cx/2021/04/21/how-to-redirect-in-sveltekit-endpoints/

MLA
" » How to redirect in SvelteKit endpoints." Dana Woodman | Sciencx - Wednesday April 21, 2021, https://www.scien.cx/2021/04/21/how-to-redirect-in-sveltekit-endpoints/
HARVARD
Dana Woodman | Sciencx Wednesday April 21, 2021 » How to redirect in SvelteKit endpoints., viewed ,<https://www.scien.cx/2021/04/21/how-to-redirect-in-sveltekit-endpoints/>
VANCOUVER
Dana Woodman | Sciencx - » How to redirect in SvelteKit endpoints. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/21/how-to-redirect-in-sveltekit-endpoints/
CHICAGO
" » How to redirect in SvelteKit endpoints." Dana Woodman | Sciencx - Accessed . https://www.scien.cx/2021/04/21/how-to-redirect-in-sveltekit-endpoints/
IEEE
" » How to redirect in SvelteKit endpoints." Dana Woodman | Sciencx [Online]. Available: https://www.scien.cx/2021/04/21/how-to-redirect-in-sveltekit-endpoints/. [Accessed: ]
rf:citation
» How to redirect in SvelteKit endpoints | Dana Woodman | Sciencx | https://www.scien.cx/2021/04/21/how-to-redirect-in-sveltekit-endpoints/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.