Fragments
Cook generates a content-only HTML fragment alongside every page. These fragments contain only the inner HTML of a target element (default: <main>), enabling client-side navigation libraries like html-star to swap page content without a full reload.
How it works
For every index.html in dist/, Cook extracts the innerHTML of the configured selector and writes it as a sibling file. Given this page:
<!doctype html><html lang="en"><head><title>About</title></head><body> <header><nav>...</nav></header> <main> <h1>About Us</h1> <p>We build things.</p> </main> <footer>...</footer></body></html>
Cook produces this fragment:
<h1>About Us</h1> <p>We build things.</p>
Using fragments with html-star
Client-side routers request fragments by sending a custom header. html-star sends X-Requested-With: htmlstar when navigating between pages. On the server (or in Cook's dev server), this header triggers the fragment response instead of the full page.
A typical flow:
- User clicks a link
- html-star intercepts the click and fetches the URL with the
X-Requested-With: htmlstarheader - The server returns
_fragment.htmlinstead ofindex.html - html-star swaps the
<main>content and updates the URL
This gives users instant page transitions while keeping full-page HTML available for direct navigation and search engines.
Configuration
Fragments are enabled by default. Configure them in config/main.js:
export default { fragments: { enabled: true, // set false to disable selector: 'main', // CSS selector for the content element filename: '_fragment.html' // output filename },};
| Option | Default | Description |
|---|---|---|
enabled | true | Enable fragment generation |
selector | 'main' | CSS selector whose innerHTML becomes the fragment |
filename | '_fragment.html' | Name of the generated fragment file |
Custom selectors
If your layout uses a different content wrapper, change the selector:
export default { fragments: { selector: '#content', filename: '_partial.html', },};
Pages that do not contain an element matching the selector are silently skipped.