Skip to content

Commit cc98f08

Browse files
committed
Refactor error handler to return content string instead of direct DOM manipulation
1 parent 3984c62 commit cc98f08

File tree

1 file changed

+33
-59
lines changed

1 file changed

+33
-59
lines changed

public/js/main.js

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,39 @@ function initRouter() {
443443
errorHandler: (path) => {
444444
console.log('Error handler called for path:', path);
445445

446-
// Use a more direct approach that bypasses the router's transition mechanism
447-
// This will be called immediately when the route is not found
446+
// Create the error page content as a string
447+
// This is what the router expects to receive from the error handler
448+
const errorContent = `
449+
<pf-header></pf-header>
450+
<div class="content-container" style="display: flex; justify-content: center; align-items: center; min-height: 60vh;">
451+
<div class="error-page">
452+
<h1>404 - Page Not Found</h1>
453+
<p>The page "${path}" could not be found.</p>
454+
<a href="/" class="back-link">Go back to home</a>
455+
</div>
456+
</div>
457+
<pf-footer></pf-footer>
458+
`;
459+
460+
// Remove the initial loading overlay
448461
setTimeout(() => {
449-
// Get the root element
450-
const rootElement = document.getElementById('app');
451-
if (!rootElement) {
452-
console.error('Root element #app not found');
453-
return;
462+
const initialOverlay = document.getElementById('initial-loading-overlay');
463+
if (initialOverlay && initialOverlay.parentNode) {
464+
console.log('Removing initial loading overlay');
465+
initialOverlay.style.opacity = '0';
466+
setTimeout(() => {
467+
if (initialOverlay.parentNode) {
468+
initialOverlay.parentNode.removeChild(initialOverlay);
469+
}
470+
}, 150);
454471
}
455472

456-
console.log('Creating error page for path:', path);
473+
// Dispatch the route-changed event to trigger any listeners
474+
window.dispatchEvent(new CustomEvent('route-changed', {
475+
detail: { path, route: null }
476+
}));
457477

458-
// First, ensure all transition overlays are removed
478+
// Clean up any transition overlays
459479
const cleanupOverlays = () => {
460480
// Remove any remaining transition overlays
461481
const overlays = document.querySelectorAll('.transition-overlay');
@@ -477,57 +497,11 @@ function initRouter() {
477497
}
478498
}
479499
});
480-
481-
// Explicitly remove the initial loading overlay
482-
const initialOverlay = document.getElementById('initial-loading-overlay');
483-
if (initialOverlay && initialOverlay.parentNode) {
484-
console.log('Removing initial loading overlay');
485-
initialOverlay.style.opacity = '0';
486-
setTimeout(() => {
487-
if (initialOverlay.parentNode) {
488-
initialOverlay.parentNode.removeChild(initialOverlay);
489-
}
490-
}, 150);
491-
}
492500
};
493501

494-
// Clean up overlays first
502+
// Clean up overlays
495503
cleanupOverlays();
496504

497-
// Clear any existing content
498-
rootElement.innerHTML = '';
499-
500-
// Create the error page content directly
501-
const errorContent = document.createElement('div');
502-
errorContent.className = 'error-page-container';
503-
errorContent.innerHTML = `
504-
<pf-header></pf-header>
505-
<div class="content-container" style="display: flex; justify-content: center; align-items: center; min-height: 60vh;">
506-
<div class="error-page">
507-
<h1>404 - Page Not Found</h1>
508-
<p>The page "${path}" could not be found.</p>
509-
<a href="/" class="back-link">Go back to home</a>
510-
</div>
511-
</div>
512-
<pf-footer></pf-footer>
513-
`;
514-
515-
// Append the error content to the root element
516-
rootElement.appendChild(errorContent);
517-
518-
console.log('Error page content appended to DOM');
519-
520-
// Dispatch the route-changed event to trigger any listeners (like the one that removes the initial overlay)
521-
window.dispatchEvent(new CustomEvent('route-changed', {
522-
detail: { path, route: null }
523-
}));
524-
525-
// Also dispatch the transition-end event to ensure proper cleanup
526-
document.dispatchEvent(new CustomEvent('spa-transition-end'));
527-
528-
// Clean up overlays again after a short delay to catch any that might have been created during the transition
529-
setTimeout(cleanupOverlays, 100);
530-
531505
// Set up a safety interval to periodically check for and remove any overlays that might appear
532506
const safetyInterval = setInterval(cleanupOverlays, 500);
533507

@@ -536,10 +510,10 @@ function initRouter() {
536510
clearInterval(safetyInterval);
537511
console.log('Safety interval cleared');
538512
}, 3000);
539-
}, 50); // Slight delay to ensure DOM is ready
513+
}, 50);
540514

541-
// Return an empty string since we're handling the rendering directly
542-
return '';
515+
// Return the error content string for the router to handle
516+
return errorContent;
543517
}
544518
});
545519

0 commit comments

Comments
 (0)