Firebase
Firebase Functions
To use the more recent and recommended generation of firebase functions, set the firebase.gen
option to 2
:
export default defineNuxtConfig({
nitro: {
firebase: {
gen: 2
}
}
})
NITRO_FIREBASE_GEN=2
environment variable.If you already have a deployed version of your website and want to upgrade to 2nd gen, see the Migration process on Firebase docs. Namely, the CLI will ask you to delete your existing functions before deploying the new ones.
Project Setup
You may instead prefer to set up your project with the Firebase CLI, which will fetch your project ID for you, add required dependencies (see above) and even set up automated deployments via GitHub Actions (for hosting only). Learn about installing the firebase CLI.
- Install the latest version of the Firebase CLI.Terminal
npm install -g firebase-tools@latest
- Initialize your Firebase ProjectTerminal
firebase login firebase init hosting
.output/public
as the public directory. In the next step, do not configure your project as a single-page app.Once complete, add the following to your firebase.json
to enable server rendering in Cloud Functions:
{
"functions": { "source": ".output/server" },
"hosting": [
{
"site": "<your_project_id>",
"public": ".output/public",
"cleanUrls": true,
"rewrites": [{ "source": "**", "function": "server" }]
}
]
}
Local Preview
You can preview a local version of your site if you need to test things out without deploying.
npm run build -- --preset=firebase
firebase emulators:start
Build and Deploy
Deploy to Firebase Hosting by running a Nitro build and then running the firebase deploy
command.
npm run build -- --preset=firebase
firebase deploy
Options
You can set options for the firebase functions in your nuxt.config.ts
file:
export default defineNuxtConfig({
nitro: {
firebase: {
gen: 2,
httpsOptions: {
region: 'europe-west1',
maxInstances: 3,
},
},
},
});
Runtime Node.js Version
You can set custom Node.js version in configuration:
export default defineNuxtConfig({
nitro: {
firebase: {
nodeVersion: '18' // Can be '16' or '18' or '20'
},
},
});
Firebase tools use the engines.node
version in package.json
to determine which node version to use for your functions. Nuxt automatically writes to the .output/server/package.json
with configured Node.js version.
You might also need to add a runtime key to your firebase.json
file:
{
"functions": {
"source": ".output/server",
"runtime": "nodejs20"
}
}
If your firebase project has other cloud functions
You may be warned that other cloud functions will be deleted when you deploy your Nuxt project. This is because nitro will deploy your entire project to firebase functions. If you want to deploy only your Nuxt project, you can use the --only
flag:
firebase deploy --only functions:server,hosting
Using Cookies in production
When using Firebase Hosting together with Cloud Functions or Cloud Run, cookies are generally stripped from incoming requests to allow for efficient CDN cache behavior. Only the specially-named __session
cookie is permitted to pass through to your app.
Working with Environment Variables
To set environment variables for your Firebase functions, you need to copy the .env
file to the .output/server
directory.
You can do this by adding a postbuild
script to your package.json
, that will automatically run after the build command:
{
"scripts": {
"postbuild": "cp .env .output/server/.env"
}
}