Fix navigation during render warning

Move navigation logic from render to useEffect to prevent React warning
about state updates during render. Use ref to track redirect state and
handle both cloud mode (window.location.assign) and standard mode
(router.replace) navigation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Sajid Mehmood 2025-11-12 13:04:13 -05:00
parent e063a177dc
commit 210a676ed9

View file

@ -1,5 +1,6 @@
'use client'; 'use client';
import { Grid, Loading, Column, Row } from '@umami/react-zen'; import { Grid, Loading, Column, Row } from '@umami/react-zen';
import { useEffect, useRef } from 'react';
import Script from 'next/script'; import Script from 'next/script';
import { UpdateNotice } from './UpdateNotice'; import { UpdateNotice } from './UpdateNotice';
import { SideNav } from '@/app/(main)/SideNav'; import { SideNav } from '@/app/(main)/SideNav';
@ -11,16 +12,27 @@ export function App({ children }) {
const config = useConfig(); const config = useConfig();
const { pathname, router } = useNavigation(); const { pathname, router } = useNavigation();
// Avoid navigation during render; perform redirect after commit.
const redirectedRef = useRef(false);
useEffect(() => {
if (redirectedRef.current) return;
if (isLoading) return;
if (error) {
redirectedRef.current = true;
if (process.env.cloudMode) {
window.location.assign('/login');
} else {
router.replace('/login');
}
}
}, [isLoading, error, router]);
if (isLoading || !config) { if (isLoading || !config) {
return <Loading placement="absolute" />; return <Loading placement="absolute" />;
} }
if (error) { if (error) {
if (process.env.cloudMode) { // Navigation handled in effect to prevent React warning in dev.
window.location.href = '/login';
} else {
router.push('/login');
}
return null; return null;
} }