将Firebase托管根目录重定向到云功能不起作用 [英] Redirect Firebase Hosting root to a Cloud Function is not working
问题描述
我正在将Firebase Hosting与firebase.json
文件一起使用,该文件应该将所有流量转发到云功能(prerender),该功能会填充SEO的meta和og标签.
I'm using Firebase Hosting with a firebase.json
file that is supposed to forward all traffic to a cloud function (prerender) which populates meta and og tags for SEO.
{
"hosting": {
"public": "dist/prod",
"rewrites": [
{
"source": "**",
"function": "prerender"
}
]
}
}
我的prerender
函数正在处理请求并呈现HTML文件.效果很好:
My prerender
function is is handling the request and rendering the HTML file. This works fine:
export const prerender = functions.https.onRequest((req, res) => {
console.log('prerender function: "' + req.path + '"');
...
}
在https://xxx.cloudfunctions.net/prerender
到达终点时,我可以在Firebase仪表板的功能"->日志"下正确获取呼叫:
When hitting the end point at https://xxx.cloudfunctions.net/prerender
, I correctly get the call in the Firebase Dashboard under Functions -> Logs:
prerender function: "null"
但是,当调用https://mypage.firebaseapp.com
时,没有任何日志,并且似乎在dist/prod
文件夹中呈现了index.html
.
However, when calling https://mypage.firebaseapp.com
, I get no logs and it seems to render the index.html
inside of my dist/prod
folder.
重写是否缺少任何内容?我尝试将/
重写为相同的功能,但没有成功.任何提示,不胜感激!
Is there anything I'm missing with the rewrites? I tried rewriting /
to the same function, but no success. Any hints much appreciated!
推荐答案
您应该能够完全按照显示的方式将所有URL路由到函数.我猜您在dist/prod目录中仍然有一个index.html文件.在测试项目中,我只是将根index.html重命名为其他名称,并且将对/
的请求路由到我的函数中.
You should be able to route all URLs to a function in exactly the way you're showing. I'm guessing that you still have an index.html file located in your dist/prod directory. In my test project, I simply renamed the root index.html to something else, and requests to /
were routed to my function.
事实证明,如果存在与客户端请求URL匹配的静态Web内容,则将提供这些内容而不是委派给该函数.对于任何传入的URL都是如此.真正确保将 all 请求路由到您的函数的唯一方法是在部署之前从dist/prod
文件夹中删除 all 内容.
It turns out that if there is static web content that matches the client request URL, that will be served instead of delegating to the function. This is true for any incoming URL. The only way to truly ensure that all requests are routed to your function is to remove all content from your dist/prod
folder before deploying.
我认为关键信息在重写:
仅当以下位置不存在文件或文件夹时,才应用重写规则: 指定的源,并在以下位置返回文件的实际内容 目的地,而不是HTTP重定向.
A rewrite rule is only applied if a file or folder does not exist at the specified source, and returns the actual content of the file at the destination instead of an HTTP redirect.
这篇关于将Firebase托管根目录重定向到云功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!