Complete wip: New Application Architecture
Add support for client side routing and updating without loading the AppShell
This commit is contained in:
parent
0890ae3ef9
commit
b33ffa3371
@ -11,8 +11,8 @@ const index = new Elysia()
|
|||||||
console.log(`Request ${request.method} ${request.url}`);
|
console.log(`Request ${request.method} ${request.url}`);
|
||||||
})
|
})
|
||||||
.onAfterHandle(({ request, responseValue }) => {
|
.onAfterHandle(({ request, responseValue }) => {
|
||||||
if (request.headers.get("hx-request") === "true") {
|
if (request.headers.get("shell-loaded") === "true") {
|
||||||
return responseValue; // Return the <main> element if the request is an HTMX request
|
return responseValue; // Return the <main> element if the AppShell has already been loaded
|
||||||
}
|
}
|
||||||
return AppShell(responseValue); // Return the <main> element wrapped by the AppShell
|
return AppShell(responseValue); // Return the <main> element wrapped by the AppShell
|
||||||
})
|
})
|
||||||
|
|||||||
@ -4,6 +4,7 @@ export function Blog() {
|
|||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<h1>Blog</h1>
|
<h1>Blog</h1>
|
||||||
|
<a href="/">Home</a>
|
||||||
</main>
|
</main>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -4,6 +4,7 @@ export function Home() {
|
|||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<h1>Home</h1>
|
<h1>Home</h1>
|
||||||
|
<a href="/blog">Blog</a>
|
||||||
</main>
|
</main>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -9,4 +9,3 @@
|
|||||||
logPageInfo();
|
logPageInfo();
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|||||||
@ -1,16 +1,43 @@
|
|||||||
// Client-side script that runs on page load
|
// Client-side script that runs on page load
|
||||||
// Example: TypeScript with type annotations
|
// Example: TypeScript with type annotations
|
||||||
(() => {
|
|
||||||
const setupPage = (): void => {
|
|
||||||
const links: NodeListOf<HTMLAnchorElement> = document.querySelectorAll('a');
|
|
||||||
console.log(`Found ${links.length} links on the page`);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Run setup when DOM is ready
|
async function loadContent(url: string) {
|
||||||
if (document.readyState !== 'loading') {
|
const response = await fetch(url, {
|
||||||
setupPage();
|
headers: {
|
||||||
} else {
|
'shell-loaded': 'true'
|
||||||
document.addEventListener('DOMContentLoaded', setupPage);
|
}
|
||||||
|
});
|
||||||
|
const html = await response.text();
|
||||||
|
const mainElement = document.querySelector('main');
|
||||||
|
if (mainElement) {
|
||||||
|
mainElement.outerHTML = html;
|
||||||
|
// Re-attach handlers to new links after content swap
|
||||||
|
attachLinkHandlers();
|
||||||
}
|
}
|
||||||
})();
|
}
|
||||||
|
|
||||||
|
function attachLinkHandlers() {
|
||||||
|
const links: NodeListOf<HTMLAnchorElement> = document.querySelectorAll('a');
|
||||||
|
console.log('Found links:', links.length);
|
||||||
|
|
||||||
|
links.forEach(link => {
|
||||||
|
console.log('Attaching listener to:', link.href);
|
||||||
|
link.onclick = async (e) => {
|
||||||
|
console.log('clicked', link.href);
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
e.stopImmediatePropagation();
|
||||||
|
window.history.pushState({}, '', link.href);
|
||||||
|
await loadContent(link.href);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen for back/forward button clicks
|
||||||
|
window.addEventListener('popstate', async (event) => {
|
||||||
|
await loadContent(window.location.href);
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
attachLinkHandlers();
|
||||||
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user