Web Applications

Handling Auth Redirects in Next.js Without the Flicker

You’ve all seen it: a protected page loads, shows its content for a split second, then snaps to the login screen. That flash of private content before the redirect is a bad look — and on some apps, a real leak. It happens because the redirect decision is made too late, in the browser, after the page already rendered.

Illustration of a web application dashboard — Handling Auth Redirects in Next.js Without the Flicker
A web app is judged on what happens behind the screen as much as on it.

Why the flicker happens

The page renders, a useEffect runs, it sees no session, and only then redirects. By that point the protected UI has already painted. Moving the check earlier removes the flash entirely.

Decide on the server

In Next.js, check the session on the server — in middleware or a Server Component — and redirect there. The browser never receives the protected HTML for an unauthenticated user, so there’s nothing to flash. Middleware is ideal for whole sections of the app: it runs before the page, checks the session cookie, and redirects unauthorized requests before render.

For finer control, check the session inside a Server Component and redirect from there. Either way, the decision happens on the server, not after a client round trip.

Infographic summarising “Handling Auth Redirects in Next.js Without the Flicker”: You’ve all seen it: a protected page loads, shows its content for a split second, then snaps to the login screen.; The page renders, a useEffect runs, it sees no session, and only then redirects.; In Next.js, check…
The key points, at a glance.

If you must do anything client-side, render a neutral loading state — never the real content — until auth resolves. But the clean answer is to stop the protected page from rendering for the wrong user in the first place.

keep reading

Related articles

Usually replies within a day
let’s talk

Tell me what you want to build

A website, an app, some automation — or something you’re still figuring out. Tell me the problem and I’ll give you a straight answer on what it takes. No pressure, no sales team.