How to deploy next.js app on Firebase Hosting?

筅森魡賤 提交于 2020-07-20 08:07:38

问题


I am trying to deploy next.js app on Firebase hosting. But I don't understand which files to push to the server. When I run npm run build and pushed the build folder to firebase. But gives error that No index.html file found.

Here is the image of output of build folder. I have just created a simple component for testing purposes.

Output of build command


回答1:


On package.json you need to add npm scripts for building and exporting like.

  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start",
    "export": "next export"
  },

And then you can run

npm run build && npm run export

Next build will build your project for shipping and export will put your files ready for hosting on a static hosting server (like firebase hosting).

npm run export

will create an out/ directory and place all your files there ready for uploading.

Note:

If your app needs to generate dynamic pages at the runtime, you can't deploy it as a static app.

Read more




回答2:


Check this out first. This is the official example provided by the next.js in their github repo.

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.

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.

PS : I know posting links as answers is not the best way, but my rep power is not enough to put this as a comment.


Update

I recently found out that firebase git repo has a nextjs example, be sure to check this out too. Path in the repo => firebase/functions-samples/nextjs-with-firebase-hosting



来源:https://stackoverflow.com/questions/56284434/how-to-deploy-next-js-app-on-firebase-hosting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!