How-to
Website
Web app
Set cache and security headers
This recipe sets the HTTP headers a host sends with a WebFluent build: how long a browser may keep each file, and the security headers that only work as real headers. You need a host that lets you set headers, or one that reads _headers (Netlify, Cloudflare Pages). The hosting recipes, such as Serve a site with nginx, show where each host takes them. With build.csp on, this is the build/_headers file a build writes:
/*
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
X-Frame-Options: DENY
How it works
/*applies the headers under it to every path of the site.Content-Security-Policyis the same policy every page already carries as a<meta>tag, plusframe-ancestors, which a browser ignores in a<meta>tag (Security).X-Content-Type-Options,Referrer-PolicyandX-Frame-Optionsonly work as real HTTP headers, so they are in this file and nowhere else.
The build writes no cache headers: those are yours to set, by the kind of file.
Set cache headers
To let browsers keep files without serving a stale site, set Cache-Control by the kind of file:
| Files | Cache-Control | Why |
|---|---|---|
|
HTML, |
|
their names do not change between builds |
|
|
|
a stale worker holds a stale site (Make a site work offline) |
|
processed images under |
|
the name changes when the file does |
|
|
your choice | they keep the names you gave them |
no-cache still lets the browser keep a copy. The browser asks the server each time, and a 304 Not Modified answer means it does not download the file again.
Set security headers
Netlify and Cloudflare Pages read build/_headers as it is. On any other host, set the same headers in its configuration, as the hosting recipes show: nginx, Apache, Caddy and Vercel. Serve the site over HTTPS, and turn on HSTS once you are sure HTTPS works everywhere. Review a site's security before you deploy has the full checklist.
After the deploy, check the headers it sends.