如何禁用 React SPA 的 firebase 托管缓存?

问题描述 投票:0回答:1

我正在尝试禁用我的 React SPA 的缓存,以便每当我部署到 firebase 时,用户总是拥有新鲜的内容。我尝试将无缓存标头放入 firebase.json,但它们似乎不起作用,并且只有在硬刷新或以隐身模式打开网页后才会加载新内容。

这就是我的 firebase.json 的相关部分:

 "hosting": {
        "headers": [
            {
                "source": "/**",
                "headers": [
                    {
                        "key": "Cache-Control",
                        "value": "no-cache, max-age=0, no-store, must-revalidate"
                    }
                ]
            },
            {
                "source": "**/*.@(jpg|jpeg|gif|png|svg|webp|css|eot|otf|ttf|ttc|woff|woff2|font.css)",
                "headers": [
                    {
                        "key": "Cache-Control",
                        "value": "max-age=86400"
                    }
                ]
            },
            {
                "source": "**/locales/**",
                "headers": [
                    {
                        "key": "Cache-Control",
                        "value": "no-cache, max-age=0"
                    }
                ]
            },
            {
                "source": "/service-worker.js",
                "headers": [
                    {
                        "key": "Cache-Control",
                        "value": "no-cache, max-age=0, no-store, must-revalidate"
                    }
                ]
            },
            {
                "source": "**/*.chunk.@(js|css)",
                "headers": [
                    {
                        "key": "Cache-Control",
                        "value": "no-cache, max-age=0, no-store, must-revalidate"
                    }
                ]
            }
        ],
        "public": "build",
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ],
        "rewrites": [
            {
                "source": "**",
                "destination": "/index.html"
            }
        ]
    },

响应标头显示 X-Cache:MISS 和无缓存标头,但仍然获得旧版本的应用程序: response header screenshot 有什么我遗漏的吗?有更好的方法来强制缓存驱逐吗?

reactjs firebase browser-cache firebase-hosting cache-control
1个回答
0
投票

更新: 我似乎已经想通了。如果我增加 package.json 中的版本,它将在关闭并重新打开选项卡后加载新版本。

希望这对某人有帮助,它让我非常头痛......

© www.soinside.com 2019 - 2024. All rights reserved.