When you deploy with Firebase App Hosting, the App Hosting preset will be run automatically at build time.
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.
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.
npm install -g firebase-tools@latest
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" }]
}
]
}
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
Deploy to Firebase Hosting by running a Nuxt build and then running the firebase deploy command.
npm run build -- --preset=firebase
firebase deploy
You can set options for Firebase functions in your nuxt.config.ts file:
export default defineNuxtConfig({
nitro: {
firebase: {
gen: 2,
httpsOptions: {
region: 'europe-west1',
maxInstances: 3,
},
},
},
});
You can set a 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"
}
}
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
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.
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"
}
}