NextJS Firebase onRequest函数重写



我正在使用Vercel Next.js firebase进行一个项目,这里有托管模板。

该模板使用Firebase函数来允许Firebase托管。我正在尝试添加额外的FirebaseonRequest函数,以便向服务器发送请求。

我添加了printTest函数,并在firebase.json文件中设置了重写,但不断出现404个错误。我以前在使用Express和不使用NextJS的情况下成功地实现了这一点。我想我的重写有问题。

我还测试了onCallablepubSub函数,这些函数运行得很好。我想我有重写的问题。如果您能理解为什么现在这样做,我们将不胜感激。

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"
}
}

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

从文档中,这里有一个重要的注释:

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

通过更改顺序并在末尾加入最不严格的规则来解决问题:

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

相关内容

  • 没有找到相关文章

最新更新