将Firebase托管根目录重定向到云功能不起作用

编程入门 行业动态 更新时间:2024-10-19 17:20:07
本文介绍了将Firebase托管根目录重定向到云功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将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 + '"'); ... }

在xxx.cloudfunctions/prerender到达终点时,我可以在Firebase仪表板的功能"->日志"下正确获取呼叫:

When hitting the end point at xxx.cloudfunctions/prerender, I correctly get the call in the Firebase Dashboard under Functions -> Logs:

prerender function: "null"

但是,当调用mypage.firebaseapp时,没有任何日志,并且似乎在dist/prod文件夹中呈现了index.html.

However, when calling mypage.firebaseapp, 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托管根目录重定向到云功能不起作用

本文发布于:2023-11-01 02:08:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547941.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:根目录   重定向   不起作用   功能   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!