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:

Cook produces this fragment:

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:

  1. User clicks a link
  2. html-star intercepts the click and fetches the URL with the X-Requested-With: htmlstar header
  3. The server returns _fragment.html instead of index.html
  4. 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:

OptionDefaultDescription
enabledtrueEnable 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:

Pages that do not contain an element matching the selector are silently skipped.