Capítulo 403 de 456
Sets custom HTTP response headers on incoming requests for given path patterns, via the headers key in next.config.js (Pages Router). Checked before the filesystem (pages and /public).
{ key, value } response header objects to apply.false to exclude basePath prefixing from matching (external rewrites only).false to exclude automatic locale prefixing.{ type, key, value } conditions (header, cookie, host, query) that must (has) or must not (missing) match for the header to apply.:slug matches one segment; * (zero+), + (one+), ? (zero/one) modifiers; :slug* matches nested paths; regex via :slug(\d{1,}); special chars (){}:*+? must be escaped with \\ when literal.public, max-age=31536000, immutable automatically and cannot be overridden; other responses can set Cache-Control via res.setHeader in API Routes or inside getServerSideProps.module.exports = {
headers() {
return [
{
source: '/blog/:slug',
has: [{ type: 'query', key: 'page', value: 'home' }],
headers: [{ key: 'x-slug', value: ':slug' }],
},
]
},
}
has de query string, reutilizando o parâmetro capturado no valor do header.res.setHeader('Cache-Control', 's-maxage=86400')
| Header comum | Uso |
|---|---|
X-DNS-Prefetch-Control | liga prefetch de DNS |
Strict-Transport-Security | força HTTPS (max-age=63072000; includeSubDomains; preload) |
X-Frame-Options | previne clickjacking (superado por CSP frame-ancestors) |
Permissions-Policy | controla APIs/features do browser |
X-Content-Type-Options | nosniff, evita MIME sniffing |
Referrer-Policy | controla info de referrer enviada |
| Version | Changes |
|---|---|
| v13.3.0 | missing added |
| v10.2.0 | has added |
| v9.5.0 | Headers added |
headers roda antes do filesystem, então pode sobrepor arquivos estáticos.* para isso.has/missing permitem headers condicionais por header/cookie/query/host recebido.basePath ou i18n configurados, source é prefixado automaticamente a menos que basePath: false ou locale: false.source, has/missing, path matching.