Git Product home page Git Product logo

devotion's Introduction

with Firebase Hosting example

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. Each individual page bundle is served in a new call to the Cloud Function which performs the initial server render.

If you are having issues, feel free to tag @jthegedus in the issue you create on the next.js repo

Make sure that firebase is set up and you have the projectID
  • Install Firebase Tools: npm i -g firebase-tools
  • Create a project through the firebase web console
  • Login to the Firebase CLI tool with firebase login
  • Grab the projectID from firebase projects:list or the web consoles URL: https://console.firebase.google.com/project/<projectID>

How to use

Using create-next-app

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example with-firebase-hosting with-firebase-hosting-app
# or
yarn create next-app --example with-firebase-hosting with-firebase-hosting-app

Update .firebaserc: adding your firebase project ID

Download manually

Download the example:

curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-firebase-hosting
cd with-firebase-hosting

Update .firebaserc: adding your firebase project ID

Install it and run:

npm install
npm run dev
# or
yarn
yarn dev

# to run Firebase locally for testing:
npm run serve

# to deploy it to the cloud with Firebase:
npm run deploy

Typescript

To use Typescript, simply follow Typescript setup as normal (package.json scripts are already set).

i.e: npm install --save-dev typescript @types/react @types/node

Then you can create components and pages in .tsx or .ts

Only src/next.config.js and firebaseFunctions.js must remain in *.js format.

Good to know

  • firebase.json outlines the catchall rewrite rule for our Cloud Function.
  • The empty public/.gitignore file is to ensure public/ dir exists as it is required for Firebase Hosting. It is configured (by default) that dotfiles (public/.*) are ignored from bein publicly served.
  • The Cloud Function is named nextjsFunc (changeable in firebaseFunctions.js and firebase.json).
  • public/* files are statically served through Firebase hosting, not through NextJs server.
  • Specifying "engines": {"node": "10"} in package.json is required and the latest supported by firebase functions.

Customization

Next App is in src/ directory.

The crucial files for the setup:

  • .firebaserc
  • firebase.json
  • firebaseFunctions.js
  • src/next.config.js
  • In package.json: firebase-* packages and engines field

Caveat

Because firebase functions require "engines": {"node": "10"} (or 8) to be specified (in package.json), if you are using yarn (instead of npm), you will need to add flag --ignore-engines.

References

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.