具有云功能的Firebase托管-如何清除/刷新CDN缓存? [英] Firebase hosting with cloud functions - how to purge/refresh CDN cache?

查看:103
本文介绍了具有云功能的Firebase托管-如何清除/刷新CDN缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照此视频中的说明进行操作,到目前为止,一切正常,直到需要清除旧HTML的CDN为止。

I'm following the instructions from this video and so far things are working perfectly right up to the point where I need to purge the CDN of old HTML:

https://www.youtube.com/watch?v=7_2CJs_VZk4

到目前为止,我已经使用Firebase Cloud Functions创建了一个静态HTML页面,并将其存储在Firebase Cloud Storage中。每当我的Firebase数据库中的数据更新时,就会呈现一个新的和更新的静态HTML页面并将其保存到Cloud Storage。

So far I've created a static HTML page using Firebase Cloud Functions, and it is stored on Firebase Cloud Storage. Whenever data from my Firebase Database is updated, a new and updated static HTML page is rendered and is saved to the Cloud Storage.

Firebase Hosting会提供该静态HTML页面,并且它存储在CDN上。
根据上面的视频,我为CDN设置了较长的刷新时间,因为我只希望在对Firebase数据库中的数据进行更改时进行更新(如果未进行任何更改,则不刷新CDN) )

Firebase Hosting serves that static HTML page, and it is stored on the CDN. As per the above video, I've set a long refresh time for the CDN because I only want it to update when I make a change to the data in Firebase Database (no refreshing the CDN if no change has been made)

要执行此操作,在更新页面时,必须清除CDN上存储的旧HTML页面,并用新的HTML页面替换。

For this to work, when the page is updated, the old HTML page stored on the CDN must be purged and replaced with the new HTML page.

时间33:19的视频建议以下代码:

The video at time 33:19 suggests the following code:

const purgeUrl = `${ORIGIN}/${path}`;
await request(purgeUrl, { method: "PURGE" });

不幸的是,它似乎不起作用。我可以看到页面在Cloud Storage上已更新,但是当刷新URL时,不会显示新的HTML页面。
显示新HTML的唯一方法是再次重新部署firebase代码(终端-firebase部署)。

Unfortunately it doesn't seem to work. I can see that the page has updated on Cloud Storage, but when I refresh the URL the new HTML page is not displayed. The only way I can show the new HTML is by re-deploying the firebase code again (terminal - firebase deploy).

这是我的代码(在node.js):

Here is my code (in node.js):

const ORIGIN = "examplesite-30ccf.firebaseapp.com/";

function getFacts(){
  return database.ref('/web app/test').once('value').then(snap =>snap.val());
}

exports.updateHomePage = functions.database.ref('/web app/test')
.onWrite((snap, context) => {

  return getFacts()
  .then(result1 => {
    console.log("result1", result1);
      return bucket
      .file('public_html/index.html')
      .save(HomePage(result1), {
        gzip: true,
        metadata: {
          contentType: "text/html; charset=utf-8",
          cacheControl: "max-age=300, s-maxage=31536000" // indef CDN cache since we purge manually
        }
      }); 
  }).then(doNotUse => {

    const purgeUrl = `${ORIGIN}`;
    console.log("purged URL", purgeUrl);
    return request(purgeUrl, { method: "PURGE" });
}).catch(error => {
      // Handle errors of asyncFunc1() and asyncFunc2()
  });

});`

Firebase Functions日志中没有崩溃或异常。任何帮助深表感谢。谢谢

There is no crash or exceptions in the Firebase Functions logs. Any help is much appreciated. Thanks

推荐答案

我在尝试解决其他问题时无意中找到了答案。

I inadvertently found the answer while trying to fix another issue.

您需要使用付费的Firebase计划才能进行外部API调用:
https:// firebase.google.com/pricing/

You need to be on a paid Firebase plan to make external API calls: https://firebase.google.com/pricing/

因此,只要升级了计划,PURGE方法就可以工作。
这是允许您清除CDN(Typescript中的Node.js)的代码:

So the PURGE method does work so long as you have upgraded you plan. Here is the code that allows you to purge the CDN (Node.js in Typescript):

import * as request from "request-promise";

const purgeUrl = `examplesite.com`;
await request(purgeUrl, { method: "PURGE" });

这篇关于具有云功能的Firebase托管-如何清除/刷新CDN缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆