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.
SPA Ascend
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment