Next Js-Firebase部署问题

时间:2020-07-09 13:16:18

标签: javascript reactjs firebase next.js server-side-rendering

我正在构建一个新的Next Js应用程序,它是通过链接gitlab Next js项目使该应用程序部署在vercel中的一种直接方法。

对于同一项目,我需要将其部署在firebase中。

我尝试过的事情:

->制成firebase init

这给出了 firebase.json

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

但是上面的错误类似,

enter image description here

从该错误中我可以得知它试图获取index.html,但是我不确定npm run build之后的位置。.

所以我尝试给页面提供目录和index.js文件,

{
  "hosting": {
    "public": "pages",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  },
    "rewrites": [{
      "source": "/pages/**",
      "destination": "/index.js"
    },
    {
      "source": "**",
      "destination": "/index.js"
    }]
}

但这只是将 index.js 中可用的代码输出到用户界面,例如

import React, { Component } from "react";
import Router from "next/router";

export default class Index extends Component {
  componentDidMount = () => {
    Router.push("/landing",'');
  };

  render() {
    return <div />;
  }
}

gitlab-ci.yml 文件如下,

image: node:12.13.0-alpine

stages:
  - deploy

cache:                  
  paths:                
    - node_modules/     
  key: "$CI_BUILD_REPO" 

deploy-prod:
  stage: deploy
  only:
    - master
  script:
    - npm install
    - npm run build
    - npm install -g firebase-tools
    - firebase -V
    - firebase use anvisysytems --token "token_hidden"
    - firebase deploy --only hosting -m "Pipe $CI_PIPELINE_ID Build $CI_BUILD_ID" --token "token_hidden"
    

请帮助我实现获取正确的index.html的结果,该结果将在构建Next Js应用程序后生成,并使应用程序内容加载到UI中,而不是错误(如上图)或代码(如索引)。 UI中的js代码呈现。)

1 个答案:

答案 0 :(得分:1)

Firebase 只能托管静态文件,

要将 NEXT js 项目作为静态文件托管,您可以使用导出选项,然后将其部署到 Firebase。

https://nextjs.org/docs/advanced-features/static-html-export