- npx create next-app .
- do all the code you want.
- create a firebase project
- upgrade to Pay as you go
- login into firebase via cli
- firebase init
- setup project for hosting & functions
- edit your firebase.json file like below
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "nextServer"
}
]
},
"functions": {
"source": ".",
"runtime": "nodejs12"
}
}
- hosting setup
make sure you set the public = public. - functions setup
rewrite source = ** and function = nextServer(we will create our own function)
set funcions source="."
set the runtime =nodejs12
const { https } = require('firebase-functions');
const { default: next } = require('next');
const isDev = process.env.NODE_ENV !== 'production';
const server = next({
dev: isDev,
//location of .next generated after running -> yarn build
conf: { distDir: '.next' },
});
const nextjsHandle = server.getRequestHandler();
exports.nextServer = https.onRequest((req, res) => {
return server.prepare()
.then(() => {
return nextjsHandle(req, res)
});
});
/*
firebase-admin,firebase-functions
require these plugins,install them
*/
npm i firebase-admin firebase-functions
So final package.json file will look like this.we added some custom scipt.
{
"name": "app",
"main": "server.js",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"out": "next build && next export",
"deploy2": "cross-env NODE_ENV=production firebase deploy --only functions,hosting",
"deploy": "firebase deploy --only functions,hosting",
"start": "next start"
},
"dependencies": {
"cross-env": "^7.0.3",
"firebase-admin": "^9.4.2",
"firebase-functions": "^3.13.0",
"next": "10.0.5",
"react": "17.0.1",
"react-dom": "17.0.1"
}
}
//build our Next.js project to generate .next folder
1. npm run build
//Now deploy
2. npm run deploy