我在firebase上托管SPA,几乎所有路径都被重写为index.html
。我正在使用基于Webpack哈希的缓存清除,因此我想始终避免缓存我的index.html
文件,而不是其他任何文件。我发现很难做到这一点。具体来说,我的文件布局如下所示
/
├── index.html
├── login.html
├── js
│ ├── login.ba22ef2579d744b26c65.bundle.js
│ └── main.6d0ef60e45ae7a11063c.bundle.js
└── public
└── favicon-16x16.ico
"sources": "index.html"
在从文档中阅读此报价之前,我是从幼稚开始的。
每个定义必须具有与原始请求路径匹配的源密钥,而不管使用glob表示法的任何重写规则如何。
好的,所以我想我不需要在路径上指定一个简单的glob,而是指定要在其中放置这些头文件的文件。由于大多数路径都重定向到index.html,因此我需要一个全局域,以排除所有我不想放置这些标头的路径。
供参考,我的firebase.json
托管部分如下所示:
{
"hosting": {
"public": "dist",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"cleanUrls": true,
"trailingSlash": false,
"headers": [
{
"source": <<<WHAT-GOES-HERE?>>>,
"headers": [
{
"key": "Cache-Control",
"value": "no-cache, no-store, must-revalidate"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Expires",
"value": "0"
}
]
}
]
}
}
因此,举一些重定向到index.html且不应缓存的示例
mysite.com
mysite.com/
mysite.com/foo/bar/baz
mysite.com/index.html
注意:如果最后一个缓存了,因为我在实践中没有用到,我可以住。
还有不会重定向到index.html且不应缓存的内容
**/*.* (ideally excluding index.html)
mysite.com/login
我自己得到的最接近的显示器**/!(login|*.*)
几乎适用于上面列出的所有内容,但莫名其妙地不适用于mysite.com
或mysite.com/
。这两页没有被这个问题匹配,我不知道为什么。
这是我正在使用的配置。逻辑是对所有静态文件(如images, css, js
etc)使用缓存。对于所有其他文件,即将缓存"source": "/**"
设置为no-cache。因此,对于所有其他文件,可能不会应用example.com,example.com / index.html,example.com / about-us,example.com / about-us.html缓存。
{
"hosting": {
"public": "dist",
"headers": [
{
"source": "/**",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache, no-store, must-revalidate"
}
]
},
{
"source":
"**/*.@(jpg|jpeg|gif|png|svg|webp|js|css|eot|otf|ttf|ttc|woff|woff2|font.css)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=604800"
}
]
}
],
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句