Replies: 3 comments 4 replies
-
Context
Why Fix (minimum viable)
// app/(protected)/dashboard/page.tsx
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
// prevent caching of this route
export const dynamic = 'force-dynamic' // or: export const revalidate = 0
export default async function Page() {
const token = cookies().get('session')?.value
if (!token) redirect('/login')
return <main>Private</main>
}
await fetch('https://api.example.com/me', { cache: 'no-store' })
// middleware.ts at project root
import { NextResponse } from 'next/server'
export function middleware(req: Request) {
const has = req.headers.get('cookie')?.includes('session=')
if (!has && req.url.includes('/dashboard')) {
return NextResponse.redirect(new URL('/login', req.url))
}
return NextResponse.next()
}
export const config = { matcher: ['/dashboard/:path*'] } Checklist
Docs
If this solves it, please Mark as answer so others can find it. |
Beta Was this translation helpful? Give feedback.
-
Are you able to put some logging? When is the redirect being calculated? On navigation? or is it prefetched? Are you using My guess would be that the static shell for this pages is hitting the redirect cuz it is being made without dynamicIO enabled - please do try with |
Beta Was this translation helpful? Give feedback.
-
Thanks for bringing this up. What Joseph mentioned about When you use PPR with A couple of things you can try:
In short: the fix is usually making sure Did enabling |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I'm trying to enable and understand PPR on my project.
All my clicks are redirecting me to my /sign-in page, I have no idea why, I could use some guidance to debug this.
I only check and redirect to sign-in in 2 places:
If I navigate directly it works, but navigation using my link it falls into my page auth guard, which to my understanding seem to be the recomended way of doing auth according to nextjs guide.
Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions