Skip to content

Instantly share code, notes, and snippets.

@Mearman
Created January 15, 2024 14:17
Show Gist options
  • Save Mearman/5627fd9ab9e273eb1acaf02ece737f27 to your computer and use it in GitHub Desktop.
Save Mearman/5627fd9ab9e273eb1acaf02ece737f27 to your computer and use it in GitHub Desktop.

Revisions

  1. Mearman created this gist Jan 15, 2024.
    56 changes: 56 additions & 0 deletions 404.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    <!DOCTYPE html>
    <html>

    <head>
    <title>Page Loading...</title>
    <script>

    function fetchAndReplaceContent(basePath, additionalSegments) {
    if (additionalSegments.length === 0) {
    document.body.innerHTML = '<h1>Page not found.</h1>';
    return;
    }

    var projectPath = basePath + additionalSegments.join('/') + '/index.html';

    fetch(projectPath).then(response => {
    if (response.ok) {
    return response.text();
    } else {
    // Try the next level up
    additionalSegments.pop();
    fetchAndReplaceContent(basePath, additionalSegments);
    }
    }).then(html => {
    if (html) {
    document.open();
    document.write(html);
    document.close();
    }
    }).catch(error => {
    console.error('Error loading content:', error);
    document.body.innerHTML = '<h1>Error loading page.</h1>';
    });
    }

    document.addEventListener('DOMContentLoaded', function () {
    var basePath = '/web/';
    var currentPath = window.location.pathname;

    if (currentPath.startsWith(basePath)) {
    var pathSegments = currentPath.split('/').filter(Boolean);
    var additionalSegments = pathSegments.slice(1); // Exclude the base path segment
    fetchAndReplaceContent(basePath, additionalSegments);
    } else {
    document.body.innerHTML = '<h1>Page not found.</h1>';
    }
    });

    </script>
    </head>

    <body>
    <h1>Loading...</h1>
    </body>

    </html>