Skip to content

Commit

Permalink
fix redirection loop by replacing isDataRequest conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
skovati committed Jan 18, 2024
1 parent 1f71fc9 commit 8f7eeb8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { env } from '$env/dynamic/public';

export const handle: Handle = async ({ event, resolve }) => {
try {
if (event.isDataRequest) {
// sveltekit makes a request for json data from layout.server.ts as its first request for a page.
// we want to ignore that here, and instead run our hook logic for the actual SSR request
// this is mainly so that if we need to redirect, we have a human-readable URL (e.g. /plans/1)
// instead of some internal sveltekit URL (/plans/__data.json?x-sveltekit-invalidate=10)
if (event.request.url.includes("__data.json")) {
return await resolve(event);
}

Expand Down Expand Up @@ -39,11 +43,13 @@ const handleJWTAuth: Handle = async ({ event, resolve }) => {
event.locals.user = user;
return await resolve(event);
}
} else {
event.locals.user = null;
}

// if we're already on the login page, don't redirect
// otherwise we get stuck in a redirect loop
return event.url.pathname.startsWith('/login') || event.url.pathname.startsWith('/auth')
return event.url.pathname.includes('/login') || event.url.pathname.includes('/auth')
? await resolve(event)
: new Response(null, {
headers: {
Expand Down

0 comments on commit 8f7eeb8

Please sign in to comment.