home page redirect loading fix

This commit is contained in:
2026-04-21 10:39:13 +05:30
parent 05fcb0a0d5
commit 11d8e5daa6
2 changed files with 12 additions and 7 deletions

View File

@@ -1,12 +1,14 @@
import { type ReactNode, Suspense, useState, useEffect } from 'react' import { type ReactNode, Suspense, useState, useLayoutEffect } from 'react'
import { Navigate, useLocation } from 'react-router-dom' import { Navigate, useLocation } from 'react-router-dom'
import { useAuth } from '../contexts/AuthContext' import { useAuth } from '../contexts/AuthContext'
import { PageLoader } from './PageLoader' import { PageLoader } from './PageLoader'
// Mounts only once Suspense has resolved (chunk is ready). // Mounts only once Suspense has resolved (chunk is ready).
// Signals the parent to hide the loader and reveal content. // useLayoutEffect fires before the browser paints, so setState here causes a
// synchronous re-render — content becomes visible in the same paint with no
// intermediate loader flash for cached chunks.
function ContentReady({ onReady }: { onReady: () => void }) { function ContentReady({ onReady }: { onReady: () => void }) {
useEffect(() => { useLayoutEffect(() => {
onReady() onReady()
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []) }, [])
@@ -19,9 +21,10 @@ export function ProtectedRoute({ children }: Props) {
const { user, loading } = useAuth() const { user, loading } = useAuth()
const location = useLocation() const location = useLocation()
// On page refresh: loading starts true → contentReady=false → loader shows throughout. // Always start false so the loader covers any intermediate render state.
// On in-app navigation: loading is already false → contentReady=true → no loader shown. // For cached chunks, ContentReady's useLayoutEffect fires before the first
const [contentReady, setContentReady] = useState(() => !loading) // paint and flips this synchronously — no visible flash.
const [contentReady, setContentReady] = useState(false)
if (!loading && !user) { if (!loading && !user) {
return <Navigate to="/" state={{ from: location }} replace /> return <Navigate to="/" state={{ from: location }} replace />

View File

@@ -19,7 +19,9 @@ export default function LoginPage() {
useEffect(() => { useEffect(() => {
if (loading) return if (loading) return
if (user) navigate('/write', { replace: true }) if (user) {
import('./HomePage').then(() => navigate('/write', { replace: true }))
}
}, [user, loading, navigate]) }, [user, loading, navigate])
async function handleGoogleSignIn() { async function handleGoogleSignIn() {