2017-07-26 05:52:49 +00:00
# With Firebase Hosting example
## How to use
2017-12-03 04:30:17 +00:00
### Using `create-next-app`
Download [`create-next-app` ](https://github.com/segmentio/create-next-app ) to bootstrap the example:
```
npm i -g create-next-app
create-next-app --example with-firebase-hosting with-firebase-hosting-app
```
### Download manually
2017-07-26 05:52:49 +00:00
Download the example [or clone the repo ](https://github.com/zeit/next.js ):
```bash
2017-12-07 02:12:42 +00:00
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-firebase-hosting
2017-07-26 05:52:49 +00:00
cd with-firebase-hosting
```
It is recommended to use a package manager that uses a lockfile and caching for faster dev/test cycles:
- [Yarn ](https://github.com/yarnpkg/yarn )
- [npm5.1.x ](https://github.com/npm/npm )
- [pnpm ](https://github.com/pnpm/pnpm )
Set up firebase:
2017-10-05 06:28:46 +00:00
- install Firebase Tools: `npm i -g firebase-tools`
2017-07-26 05:52:49 +00:00
- create a project through the [firebase web console ](https://console.firebase.google.com/ )
- grab the projects ID from the web consoles URL: https://console.firebase.google.com/project/< projectId >
- update the `.firebaserc` default project ID to the newly created project
Install project:
2017-10-05 06:28:46 +00:00
```bash
npm install
```
2017-07-26 05:52:49 +00:00
Run Next.js development:
```bash
npm run next
```
Run Firebase locally for testing:
```bash
npm run serve
```
2017-10-05 06:28:46 +00:00
Deploy it to the cloud with Firebase:
2017-07-26 05:52:49 +00:00
```bash
npm run deploy
```
## The idea behind the 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.
2017-07-30 12:02:16 +00:00
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 ](https://medium.com/@jthegedus/next-js-on-cloud-functions-for-firebase-with-firebase-hosting-7911465298f2 ).
2017-07-26 05:52:49 +00:00
2017-10-05 06:28:46 +00:00
## Important / Caveats
2017-07-26 05:52:49 +00:00
* The empty `placeholder.html` file is so Firebase Hosting does not error on an empty `public/` folder and still hosts at the Firebase project URL.
* `firebase.json` outlines the catchall rewrite rule for our Cloud Function.
* Testing on Firebase locally requires a complete build of the Next.js app. `npm run serve` handles everything required.
2017-07-30 12:02:16 +00:00
* **Any npm modules dependencies used in the Next.js app (`app/` folder) must also be installed as dependencies for the Cloud Functions project (`functions` folder).**