Router Navigation
BareMetal.router methods
BareMetal.router.reload()
Executes a hard reload of the current SPA page by calling window.location.reload(). This is useful when you want to force the browser to wipe the entire SPA state and fetch fresh assets.
BareMetal.router.handleRoute()
Forces the router to evaluate the current window.location.pathname, fetch the corresponding HTML, and swap the DOM. Usually called automatically via popstate or link clicks, but can be invoked manually if you change the URL via history.pushState().
Route Cleanups (onCleanup)
When the router navigates away from a page, it destroys the active modules. You should register cleanup logic to prevent memory leaks across route changes.
DOM Swapping & Page-Specific Assets
When the BareMetal Router performs an SPA navigation, it swaps the <body> using innerHTML. This introduces two critical web security/architecture caveats:
-
Page-Specific Styles (
data-baremetal="style")
The router deliberately strips out all<link>and<style>tags from the<head>of the newly fetched page to prevent CSS bloat, unless they include thedata-baremetal="style"attribute. If a page has custom CSS, you MUST add this attribute so the router preserves it during navigation. -
Page-Specific Scripts (
<script src="...">)
Browsers do not execute<script>tags injected viainnerHTML. If a page relies on an external script placed in its<body>, it will fail to load during SPA navigation. You must either make the library a global dependency (in the<head>of the initial load), or dynamically inject the script via JS (document.createElement('script')) inside your module'smount()function.