【问题标题】:NextJS Firebase onRequest Function RewritesNextJS Firebase onRequest 函数重写
【发布时间】:2021-06-10 21:27:04
【问题描述】:

我正在使用找到 here 的 Vercel Next.js firebase-with-hosting 模板开展一个项目。

模板使用 Firebase 函数来允许 Firebase 托管。我正在尝试添加其他 Firebase onRequest 函数,以便可以向服务器发送请求。

我添加了 printTest 函数并在 firebase.json 文件中设置了重写,但不断收到 404 错误。我之前用 Express 和没有 NextJS 成功地实现了这一点。我认为我的重写有问题。

我还测试了 onCallable 和 pubSub 函数,这些函数效果很好。我的猜测是我有一个重写问题。任何有助于理解为什么现在可以工作的帮助将不胜感激。

const { join } = require('path'); // From NextJS Vercel Base Build
const { default: next } = require('next'); // From NextJS Vercel Base Build
const isDev = process.env.NODE_ENV !== 'production'; // From NextJS Vercel Base Build
const nextjsDistDir = join('src', require('./src/next.config.js').distDir); // From NextJS Vercel Base Build
const admin = require('firebase-admin'); // Firebase Admin SDK for NodeJS.
const functions = require('firebase-functions'); // For NextJS + Firebase Functions + Firebase Hosting.
const serviceAccount = require('./firebaesAdminServiceAccountKey.json'); // Service account key for Firebase Admin SDK.
    
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++ Firebase Admin Initialization ++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
});
    
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++ Firebase Firestore Initialization ++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
const dba = admin.firestore();
    
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++ Nextjs Configuration ++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
const nextjsServer = next({
    dev: isDev,
    conf: {
        distDir: nextjsDistDir,
    },
});
    
const nextjsHandle = nextjsServer.getRequestHandler();
    
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++ Cloud Functions ++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
// Nextjs Cloud Function to allow for Firebase Hosting.
exports.nextjsFunc = functions.https.onRequest((req, res) => {
    return nextjsServer.prepare().then(() => nextjsHandle(req, res));
});
    
exports.printTest = functions.https.onRequest((req, res) => {
    console.log('THIS WORKS!');
    res.status(200).send();
});

这是带有重写的 firebase.json 文件。

{
    "hosting": {
        "public": "public",
        "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
        "rewrites": [
          {
            "source": "**",
            "function": "nextjsFunc"
          },
          {
            "source": "/printTest",
            "function": "printTest"
          }
        ]
    },
    "functions": {
        "source": ".",
        "predeploy": [
          "npm --prefix \"$PROJECT_DIR\" install",
          "npm --prefix \"$PROJECT_DIR\" run build"
        ],
        "runtime": "nodejs10"
    }
}

【问题讨论】:

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


    【解决方案1】:

    URL 模式** 表示您的所有请求都发送到nextjsFunc(),其中包括/printTest。由于这是第一个重写配置规则,因此以下任何内容都将被忽略。

    来自doc,这里有一个重要说明:

    Hosting 使用与请求路径匹配的 URL 模式应用由 first 规则定义的重写。因此,您需要在 rewrites 属性中故意对规则进行排序。

    通过更改顺序并将最不严格的规则放在最后来解决问题:

    "rewrites": [
      {
        "source": "/printTest",
        "function": "printTest"
      },
      {
        "source": "**",
        "function": "nextjsFunc"
      }  
    ]
    

    【讨论】:

      猜你喜欢
      • 2022-09-26
      • 2021-02-15
      • 2018-12-06
      • 2019-11-16
      • 2020-10-13
      • 2019-11-05
      • 2021-12-09
      • 2019-06-24
      • 1970-01-01
      相关资源
      最近更新 更多