Download the example or clone the repo:
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-firebase-hosting
cd with-firebase-hostingIt is recommended to use a package manager that uses a lockfile and caching for faster dev/test cycles:
Set up firebase:
- install Firebase Tools:
npm i -g firebase-tools - create a project through the firebase web console
- grab the projects ID from the web consoles URL: https://console.firebase.google.com/project/
- update the
.firebasercdefault project ID to the newly created project
Install project:
npm installRun Next.js development:
npm run nextRun Firebase locally for testing:
npm run serveDeploy it to the cloud with Firebase:
npm run deployThe goal is to host the Next.js app on Firebase Cloud Functions with Firebase Hosting rewrite rules so our app is served from our Firebase Hosting URL. Each individual page bundle is served in a new call to the Cloud Function which performs the initial server render.
This is based off of the work at https://github.com/geovanisouza92/serverless-firebase & https://github.com/jthegedus/firebase-functions-next-example as described here.
- The empty
placeholder.htmlfile is so Firebase Hosting does not error on an emptypublic/folder and still hosts at the Firebase project URL. firebase.jsonoutlines the catchall rewrite rule for our Cloud Function.- Testing on Firebase locally requires a complete build of the Next.js app.
npm run servehandles everything required. - Any npm modules dependencies used in the Next.js app (
app/folder) must also be installed as dependencies for the Cloud Functions project (functionsfolder).