【问题标题】:Run Firebase NextJS app function on root url在根 url 上运行 Firebase NextJS 应用程序功能
【发布时间】:2020-04-22 05:40:26
【问题描述】:

我正在尝试在根 url 上运行通过 Firebase 部署的 NextJS 应用,但它似乎只能在 /next 上运行,这给我的资产路径带来了一些问题,我只想让它在根上运行网址。如果我现在转到根 url,我会收到此错误:


如何让它在根 URL https://europe-west2-myfirstflutterapp-xxxxx.cloudfunctions.net/ 而不是 https://europe-west2-myfirstflutterapp-xxxxx.cloudfunctions.net/next 上运行?

我的firebase.json

{
  "hosting": {
    "public": "./public",
    "rewrites": [
      {
        "source": "**",
        "function": "next"
      }
    ]
  },
  "functions": {
    "source": "functions"
  }
}

我的functions/index.js

'use strict';

const functions = require('firebase-functions');
const next = require('next');
const path = require('path');

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

exports.next = functions.region('europe-west2').https.onRequest(async (req, res) => {
  console.log('File: ' + req.originalUrl); // log the page.js file that is being requested
  await app.prepare();
  handle(req, res);
});

【问题讨论】:

  • 您能否编辑问题以更详细地显示您正在尝试的实际 URL 路径,以及该功能如何无法按照您期望的方式工作?
  • @DougStevenson 完成!
  • 您已将默认的.next dist 目录更改为next。您正在运行构建版本的 nextjs (next build) 还是实时开发服务器?

标签: node.js firebase google-cloud-functions next.js


【解决方案1】:

Cloud Functions 不允许您在“cloudfunctions.net”下自动为您分配的主机名的根目录下触发功能。

如果您打算使用 Firebase 托管作为 Cloud Functions 的代理,则必须改用您的 Firebase 托管域名。默认情况下,这将是“your-project.firebaseapp.com”或“your-project.web.app”。这将使用 firebase.json 中的重写规则。

【讨论】:

  • “重定向”是什么意思?在正式的网络术语中,这意味着您的浏览器被发送到与第一个不同的 URL。真的是这样吗?云函数执行了吗?函数日志中是否还有任何内容?
  • 我搞砸了 GCP 资源位置,看起来这是问题所在,它现在可以工作了。感谢您帮助我!
猜你喜欢
  • 2017-12-24
  • 2014-11-15
  • 1970-01-01
  • 2020-09-06
  • 2023-03-26
  • 2021-03-12
  • 2019-04-21
  • 2020-01-08
  • 2011-09-03
相关资源
最近更新 更多