Fondu

The document

View as Markdown

Every page a browser loads first is an HTML document around the page: <html>, <head>, <body>. Fondu writes a minimal one; createApp({ document }) replaces it with yours. Layouts (_layout.tsx) are the React around a page; the document is the HTML around React.

The default

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- head: the page's own tags (<title>…), then the styles and scripts -->
  </head>
  <body>
    <!-- body: the page data and the rendered page -->
  </body>
</html>

Your own

document is a function of the parts Fondu renders, returning the document as JSX. A component in its own file reads best:

// app/document.tsx
import type { DocumentParts } from 'fondu'

export default function Document({ head, body, c }: DocumentParts) {
  return (
    <html lang="en" className={theme(c)}>
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="icon" href="/favicon.svg" type="image/svg+xml" />
        {head}
      </head>
      <body>{body}</body>
    </html>
  )
}
// app/server.ts
import { createApp } from 'fondu'

import Document from '#/document.tsx'

export default createApp({ document: Document })
  • head: the page's scripts, then the tags it renders (<title>, <meta>). Put it last in <head>. Its stylesheets go first in <head>, right after <meta charset>: the browser asks for them before anything else.
  • body: the page data (<script data-page>), the rendered page (<div id="app">) and, after a server render, a small script that puts a reloaded page back where it was scrolled before its first paint. Put it alone in <body>: what follows it in the template is sent last, after the page's Suspense boundaries.
  • c: the request's Hono context: a cookie, a header, the user. Above, theme(c) reads the theme a visitor chose, so the first paint has it.
  • nonce: the request's Content Security Policy nonce, for a string document's scripts (below).

The document is rendered without the whitespace you indent it with. It may return a string instead of JSX: put ${head} and ${body} in it.

The page's own tags are rendered by the page or its layout (<title>, <meta>: React puts them in <head>); the document is for what every page shares: <html> attributes, the charset and viewport, icons, fonts.

Fonts

Fonts served by the app itself need nothing more: the Content Security Policy allows the site's own files. With Fontsource, a font is a package and an import in the CSS, and Vite serves the files:

/* app/styles/app.css */
@import '@fontsource-variable/inter';

A font from Google Fonts comes from two other origins: the stylesheet from fonts.googleapis.com, the font files from fonts.gstatic.com. Link the stylesheet in the document, and allow both in the policy:

// app/document.tsx, in <head>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400..700&display=swap" />
// app/server.ts
export default createApp({
  document: Document,
  secureHeaders: {
    contentSecurityPolicy: {
      styleSrc: ["'self'", "'unsafe-inline'", 'https://fonts.googleapis.com'],
      fontSrc: ["'self'", 'data:', 'https://fonts.gstatic.com'],
    },
  },
})

A directive given replaces Fondu's one, so keep what it had: 'self' and 'unsafe-inline' for styles (React's style attributes), 'self' and data: for fonts.

Scripts in the document

The policy runs scripts of the site's own files, or with the request's nonce. Fondu gives the nonce to the inline scripts of a JSX document, so one runs as it is, before the page paints. React's own scripts, which put a streamed Suspense boundary in place, carry it too. A data block (<script type="application/ld+json">) is never run, so it gets no nonce, and no hash on a static page:

// sets the theme a visitor chose (a cookie) before the first paint
const THEME = "document.documentElement.className=/(?:^|; )theme=(light|dark)/.exec(document.cookie)?.[1]??'system'"

<script dangerouslySetInnerHTML={{ __html: THEME }} />

That trusts the document's markup: never put what a request brings in a script, or in dangerouslySetInnerHTML. A string document is not escaped at all, so Fondu leaves its scripts as they are; a script of its own writes the nonce:

document: ({ head, body, nonce }) =>
  `<!doctype html><html><head><script nonce="${nonce}">${THEME}</script>${head}</head><body>${body}</body></html>`

On a static page too, where there is no nonce: the scripts that had it are allowed by their hashes (a static page waits for every Suspense boundary, so it has none of React's). A script from another origin (<script src="https://…">) needs that origin in scriptSrc.

Last updated