mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
Fix page reload on lyrics review (#309)
## Summary Fixes the page reload issue on the lyrics review page by preserving sessionStorage across refreshes. ## Problem After PR #308, the lyrics review page worked on initial navigation but page reload showed "Page not found". This happened because: 1. `history.replaceState()` was used to update the URL to `/app/jobs/{id}/review/` 2. But Next.js internal router reverted this back to `/app/jobs/` 3. The sessionStorage was cleared immediately on read, so reload had no stored path to restore ## Solution - Keep sessionStorage value across reloads (don't clear it on read) - On page reload: browser requests `/app/jobs/`, 404.html redirects back, and we re-read the stored path from sessionStorage - Clear sessionStorage only when navigating to a different job (prevents stale redirects) ## Test plan - [x] Build succeeds - [ ] Navigate to lyrics review page (initial load works) - [ ] Reload the page (should still show lyrics review) - [ ] Navigate to a different job's review (should load new job, not old one) @coderabbitai ignore 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bf7653773a
commit
b2befccd5b
1 file changed
+25
-9
@@ -73,19 +73,22 @@ function getStoredRedirectPath(): string | null {
|
||||
|
||||
const redirectPath = sessionStorage.getItem('spa-redirect-path')
|
||||
if (redirectPath) {
|
||||
sessionStorage.removeItem('spa-redirect-path')
|
||||
// Update the URL in the address bar to reflect the actual route
|
||||
// This allows page reload to work (browser will re-trigger the SPA redirect flow)
|
||||
try {
|
||||
window.history.replaceState(null, '', redirectPath)
|
||||
} catch {
|
||||
// Ignore errors (e.g., if the URL is invalid)
|
||||
}
|
||||
// DON'T remove sessionStorage here - we need it to survive page reload
|
||||
// Next.js will revert any history.replaceState changes, so the URL stays at /app/jobs/
|
||||
// On reload, the browser requests /app/jobs/, 404.html redirects back here, and we re-read the path
|
||||
// The sessionStorage is cleared when user navigates to a different job or page
|
||||
return redirectPath
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Clear the stored redirect path - called when we're done with it
|
||||
function clearStoredRedirectPath(): void {
|
||||
if (typeof window !== 'undefined') {
|
||||
sessionStorage.removeItem('spa-redirect-path')
|
||||
}
|
||||
}
|
||||
|
||||
// Parse route from a stored redirect path
|
||||
function parseRouteFromPath(path: string): { jobId: string | null; routeType: RouteType } {
|
||||
// Remove query string and hash for matching
|
||||
@@ -109,11 +112,24 @@ export function JobRouterClient() {
|
||||
const [storedRedirectPath] = useState(() => getStoredRedirectPath())
|
||||
|
||||
// Parse route: use stored redirect path if available, otherwise use Next.js params
|
||||
// The URL is updated via history.replaceState in getStoredRedirectPath() to support page reload
|
||||
// Note: sessionStorage persists across reloads to support page refresh
|
||||
const { jobId, routeType } = storedRedirectPath
|
||||
? parseRouteFromPath(storedRedirectPath)
|
||||
: parseRoute(slug)
|
||||
|
||||
// Clear stored redirect path when navigating to a different job
|
||||
// This prevents stale redirects when clicking "Review Lyrics" on a different job
|
||||
useEffect(() => {
|
||||
if (storedRedirectPath && jobId) {
|
||||
const storedJobId = parseRouteFromPath(storedRedirectPath).jobId
|
||||
// If the stored job ID doesn't match the current job ID, clear it
|
||||
// This handles the case where user navigates to a different job
|
||||
if (storedJobId && storedJobId !== jobId) {
|
||||
clearStoredRedirectPath()
|
||||
}
|
||||
}
|
||||
}, [storedRedirectPath, jobId])
|
||||
|
||||
const { user, isLoading: authLoading } = useAuth()
|
||||
const [accessState, setAccessState] = useState<AccessState>({ status: "loading" })
|
||||
|
||||
|
||||
Reference in new issue
Block a user