# preact-buildless-frontend
> Build-less ESM frontends that run directly in the browser without bundlers. Use this skill when creating static frontends, SPAs without build tools, prototypes, or when the user explicitly wants no Vite/Webpack/bundler. Covers import maps, CDN imports, cache-busting, hash routing, and performance patterns.
- Author: Ivan Charapanau
- Repository: av/skills
- Version: 20260130105616
- Stars: 2
- Forks: 0
- Last Updated: 2026-02-06
- Source: https://github.com/av/skills
- Web: https://mule.run/skillshub/@@av/skills~preact-buildless-frontend:20260130105616
---
---
name: preact-buildless-frontend
description: Build-less ESM frontends that run directly in the browser without bundlers. Use this skill when creating static frontends, SPAs without build tools, prototypes, or when the user explicitly wants no Vite/Webpack/bundler. Covers import maps, CDN imports, cache-busting, hash routing, and performance patterns.
---
# Build-less ESM Frontend
Create frontends that run directly in the browser using ES modules—no bundler, no build step.
## Starter Template
Copy from `assets/starter/` for a working baseline:
- `index.html` — import map + module entry
- `app.js` — Preact + signals + hash routing
- `index.css` — CSS variables, dark mode
Run locally:
```bash
npx serve assets/starter # or python3 -m http.server 3000
```
## Core Patterns
### 1. Import Maps
Use `
```
Generate this map dynamically (server middleware) or commit a static version.
### 2. CDN Imports
Import third-party ESM directly from CDN. Pin versions:
```js
import { signal } from 'https://cdn.jsdelivr.net/npm/@preact/signals@1.3.0/dist/signals.module.js';
```
Prefer mapping through import map to keep source clean:
```js
import { signal } from '@preact/signals'; // resolved via import map
```
### 3. Cache-Busting
Two approaches:
**A) Versioned URLs (recommended):**
- Append `?v=` to local `.js` and `.css`
- Set `Cache-Control: immutable` headers
**B) ETag/Last-Modified:**
- Keep stable URLs, let browser revalidate
For dynamic injection, rewrite `index.html` at serve time. For static hosting, commit versioned URLs manually.
### 4. Subpath Mounting
If served under a subpath (e.g., `/app`), use ``:
```html
```
Ensures relative imports resolve correctly.
## Structure
Minimal:
```
frontend/
index.html
index.css
app.js
```
Growing app:
```
frontend/
index.html
index.css
app.js # entry + router
state.js # signals/atoms
api.js # fetch helpers
components/
nav.js
pages/
home.js
settings.js
```
## Configuration
Inject environment variables via global `window.ENV` (no build replacement).
**index.html:**
```html
```
**config.js:**
```js
window.ENV = {
API_URL: "https://api.example.com"
};
```
Exclude `config.js` from caching or generate at runtime.
## Routing
Hash-based routing (no server config needed):
```js
const route = signal(location.hash.slice(1) || '/');
window.addEventListener('hashchange', () => {
route.value = location.hash.slice(1) || '/';
});
// Links: About
// Read: route.value === '/about'
```
For history API routing, the server must serve `index.html` for all routes.
## Lazy Loading
Load features on demand:
```js
button.onclick = async () => {
const { heavyFeature } = await import('./heavy.js');
heavyFeature();
};
```
Rule: if not needed for first paint, load lazily.
## Error Handling
Wrap dynamic imports:
```js
async function loadPage(name) {
try {
return await import(`./pages/${name}.js`);
} catch (e) {
console.error(`Failed to load ${name}:`, e);
return { default: () => html`Failed to load page.
` };
}
}
```
## Type Safety
Use JSDoc + `jsconfig.json` for full type checking without TypeScript build step.
**jsconfig.json:**
```json
{ "compilerOptions": { "checkJs": true, "module": "ESNext" } }
```
**Code usage:**
```js
/** @type {import('./types.js').User} */
const user = await api.getUser();
```
## Performance
**Startup:**
- One `