Transform the headers defined in
_headersto the format needed in yourfirebase.json.
Both Netlify sites and Cloudflare Pages support _headers files as a way to define custom headers when serving websites.
If you deploy your website with Firebase Hosting, you may have discovered that it completely ignores _headers files; headers must be defined in a firebase.json file instead, with has a slightly different syntax.
This package grabs your _headers file, parse the rules and convert them into Firebase's format, and insert them within firebase.json's hosting.headers property for you.
npm install headers-for-firebase- Edit your
firebase.jsonto include two comments, where the headers will be included:{ "hosting": { "headers": [ + /* _headers */ + /* end _headers */ ] } } - Call
headers-for-firebasebefore publishing, either in the CLI or programmatically.
Assuming you have a build script within your package.json that generates your _headers file, write a new postbuild script that calls headers-for-firebase:
{
"package": "your-package-name",
"scripts": {
+ "postbuild": "headers-for-firebase"
}
}By default headers-for-firebase assumes there are two files: ./_site/_headers and ./firebase.json; you can customize the paths by running
$ headers-for-firebase --headers "./path/to/headers" --firebase "./path/to/firebase.json"Assuming you have a build.js script that generates your _headers file; you can add these two lines:
import { addHeadersToFirebaseConfigFile } from "headers-for-firebase";
// call this function after `_headers` is generated; it will return a Promise
addHeadersToFirebaseConfigFile("./path/to/headers", "./path/to/firebase.json");Transform the headers defined in the content of headersPath, and saved them in firebasePath.
Returns a Promise with the number of targets successfully added; rejects if
- any of the paths are unavailable, or
- the content of
firebasePathdoes not include the two required comments.
Carlos Molina Avendaño ©, released under MIT License.