The 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, with a complete Typescript stack for both the Next app and for the Firebase Functions. 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 of @jthegedus in the with-firebase-hosting example.
If you're having issues, feel free to tag @sampsonjoliver in the issue you create on the next.js repo
Execute create-next-app with npm or Yarn to bootstrap the example:
npm init next-app --example with-firebase-hosting-and-typescript with-firebase-hosting-and-typescript-app
# or
yarn create next-app --example with-firebase-hosting-and-typescript with-firebase-hosting-and-typescript-appDownload the example:
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-firebase-hosting-and-typescript
cd with-firebase-hosting-and-typescriptSet 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 - login to the Firebase CLI tool with
firebase login
npm installnpm run devnpm run serve
npm run deploynpm run clean- 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.- The Firebase predeploy hooks defined in
firebase.jsonwill handle linting and compiling of the next app and the functions sourceswhenfirebase deployis invoked. The only scripts you should need aredev,cleananddeploy. - Specifying
"engines": {"node": "8"}in thepackage.jsonis required for firebase functions to be deployed on Node 8 rather than Node 6 (Firebase Blog Announcement) . This is matched in by specifying target ases2017insrc/functions/tsconfig.jsonso that typescript output somewhat compacter and moderner code.