<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="Single-Page App Boilerplate with Lazy Loading Components">
<title>SPA Boilerplate - Home</title>
<style>
/* Normalize / Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f5f5f5;
}
/* Header/Nav */
header {
background: #fff;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
position: sticky;
top: 0;
z-index: 100;
}
nav {
max-width: 1200px;
margin: 0 auto;
padding: 1rem;
display: flex;
gap: 1rem;
}
nav a {
text-decoration: none;
color: #007bff;
font-weight: 500;
padding: 0.5rem 1rem;
border-radius: 4px;
transition: background 0.2s;
}
nav a:hover,
nav a.active {
background: #007bff;
color: #fff;
}
/* Main Content */
main {
max-width: 1200px;
margin: 2rem auto;
padding: 0 1rem;
}
#app {
min-height: 60vh;
background: #fff;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.loading {
text-align: center;
padding: 2rem;
color: #666;
}
.error {
text-align: center;
padding: 2rem;
color: #dc3545;
}
h1 {
margin-bottom: 1rem;
color: #222;
}
/* Responsive */
@media (max-width: 768px) {
nav {
flex-direction: column;
align-items: stretch;
}
nav a {
text-align: center;
}
main {
margin: 1rem auto;
padding: 0 0.5rem;
}
}
</style>
</head>
<body>
<header>
<nav>
<a href="#/" class="active">Home</a>
<a href="#/about">About</a>
<a href="#/contact">Contact</a>
<a href="#/admin">Admin (Heavy Component)</a>
</nav>
</header>
<main>
<div id="app">
<div class="loading">Click a link to load a component...</div>
</div>
</main>
<script type="module">
// SPA Router with Lazy Loading Support
// In a real app, replace the loadComponent() switch with dynamic imports:
// e.g., return import(`./components/${path}.js`)
// Each component.js exports: { render(container) { ... } }
class SPARouter {
constructor() {
this.outlet = document.getElementById('app');
this.updateActiveLink();
window.addEventListener('hashchange', () => {
this.updateActiveLink();
this.route();
});
// Initial route
this.route();
}
updateActiveLink() {
const path = window.location.hash.slice(1) || '/';
document.querySelectorAll('nav a').forEach(link => {
link.classList.toggle('active', link.getAttribute('href') === `#${path}`);
});
document.title = `SPA Boilerplate - ${path.charAt(0).toUpperCase() + path.slice(1) || 'Home'}`;
}
async route() {
const path = window.location.hash.slice(1) || '/';
this.outlet.innerHTML = '<div class="loading">Loading component...</div>';
try {
const component = await this.loadComponent(path);
this.outlet.innerHTML = '';
component.render(this.outlet);
} catch (error) {
console.error('Component load failed:', error);
this.outlet.innerHTML = '<div class="error">404 - Component not found</div>';
}
}
async loadComponent(path) {
// Simulate network delay for lazy loading demo (1-3 seconds)
// In production: use dynamic import('./components/${path}.js')
await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000));
switch (path) {
case '/':
return {
render(el) {
el.innerHTML = `
<h1>Welcome to Home</h1>
<p>This is the home component. Loaded instantly (no delay).</p>
<p>SPA boilerplate with lazy loading via dynamic imports.</p>
`;
}
};
case 'about':
return {
render(el) {
el.innerHTML = `
<h1>About Us</h1>
<p>This component was lazy-loaded! Check the Network tab.</p>
<ul>
<li>Vanilla JS</li>
<li>Hash-based routing</li>
<li>Dynamic component loading</li>
<li>Responsive design</li>
</ul>
`;
}
};
case 'contact':
return {
render(el) {
el.innerHTML = `
<h1>Contact</h1>
<form>
<label>Email: <input type="email" placeholder="your@email.com"></label>
<label>Message: <textarea placeholder="Your message..."></textarea></label>
<button type="submit">Send</button>
</form>
`;
}
};
case 'admin':
return {
render(el) {
el.innerHTML = `
<h1>Admin Dashboard (Heavy Component)</h1>
<p>This simulates a large component with charts/tables. Loaded lazily!</p>
<div style="height: 400px; background: linear-gradient(45deg, #ff6b6b, #4ecdc4); display: flex; align-items: center; justify-content: center; border-radius: 8px;">
Placeholder for heavy content (e.g., Chart.js, DataTable)
</div>
`;
}
};
default:
throw new Error('Component not found');
}
}
}
// Initialize the SPA
new SPARouter();
</script>
</body>
</html>
Key Features:
Extract components to ./components/about.js, etc., for a full production setup! 🚀