No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home React

Deploy Next.js to Firebase for SSR and API Route

by npn
December 15, 2020
in React
Reading Time: 3 mins read
0
Deploy Next.js to Firebase for SSR and API Route
0
SHARES
1.7k
VIEWS
Share on FacebookShare on Twitter

Contents

  • 1 Next.js template with TypeScript installed
  • 2 About deploying Firebase in Next.js
  • 3 nextjs-typescript-starter-with-firebase
    • 3.1 How to use
    • 3.2 Explanation
    • 3.3 Other notes
  • 4 Summary
Rate this post

Next.js template with TypeScript installed

In the previous article, I introduced the template of Next.js with TypeScript installed.
This time, I will introduce how to deploy the application created with this template to Firebase.

I also created a template with Firebase settings installed, so if you want to deploy to Firebase, create a project from here.
https://github.com/redimpulz/nextjs-typescript-starter-with-firebase

About deploying Firebase in Next.js

https://qiita.com/1amageek/items/0d01dca2148df345224a

As explained in detail in the above article, for projects that do server-side processing such as SSR and API Route ,

  • Firebase Hosting: Delivery of static content
  • Cloud Functions: Server-side processing

You can deploy serverless by using these two services.

nextjs-typescript-starter-with-firebase

This template is based on nextjs-typescript-starter and has settings to deploy to Firebase.

How to use

yarn deploy

Since the deploy command is described in the npm script, npm run deployit can be deployed with.

ADVERTISEMENT

Explanation

.firebaserc

{
  "projects": {
    "default": "<project-name-here>"
  }
}

Please rewrite the project name to your own

firebase.json

{
    "hosting": {
        "public": "public",
        "ignore": ["firebase.json", "** /. *", "** / node_modules / **"],
        "rewrites": [{ "source": "**", "function": "nextApp" }]
    },
    "functions": {
        "source": ".",
        "predeploy": ["npm install", "npm run build"],
        "runtime": "nodejs10"
    }
}
  • Firebase Hosting reflects the contents of the public directory
  • I try to pass Firebase Hosting requests to Cloud Functions
  • Next.js supports Node.js version 10.13 or later, so runtime":"nodejs10"specify

functions/index.js

const { https } = require('firebase-functions');
const { default: next } = require('next');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev, conf: { distDir: '.next' } });
const handle = app.getRequestHandler();

exports.nextApp = https.onRequest((req, res) => {
  console.log('File: ' + req.originalUrl);
  return app.prepare().then(() => handle(req, res));
});
  • It is a function that is the entry point of Cloud Funtions, and the request is passed to the Next.js application.

Other notes

To use Node.js version 10 or later with Cloud Functions, you need to have a pay-as-you-go Blaze plan

Summary

I introduced how to make Next.js compatible with SSR / API Route and deploy it to Firebase.
In the case of Next.js, I think that deploying to Vercel is the easiest, but I think that some people want to deploy to Firebase, so please refer to it as a good reference!

Tags: API RouteNext.jsSSR
ShareTweetShare
Previous Post

How to use React Hook: useState

Next Post

After the anger of the adblocking industry, Google changes its Manifest V3 API

npn

Related Posts

React

Migrate from Hot Reload to Fast Refresh

December 16, 2020
1.5k
React

[React] React installation for Mac

December 16, 2020
320
React

How to use React Hook: useState

December 15, 2020
433
React

The interest of next/image from Next.js 10

December 15, 2020
596
Javascript

The story of migrating 70,000 lines of JavaScript code to TypeScript

December 15, 2020
508
Layout and Routing

AntD Basic layout – Create Website layout with Ant Design

September 23, 2020
5.1k
Next Post

After the anger of the adblocking industry, Google changes its Manifest V3 API

Discussion about this post

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
Exit mobile version