[英]How to change cache polilcy for static assets in React app (created with create-react-app)
I have created an app using React.js (with create-react-app) and I cannot find a way to change the cache policy (I am interested in max-age) for my static assets.我已经使用 React.js(使用 create-react-app)创建了一个应用程序,但我找不到为我的 static 资产更改缓存策略(我对 max-age 感兴趣)的方法。 By default, the static assets of my app (including when I use the production build with 'npm run build') have a max-age of 0.默认情况下,我的应用程序的 static 资产(包括当我使用带有“npm run build”的生产构建时)的 max-age 为 0。
All I found in the official create-react-app docs was this: https://create-react-app.dev/docs/production-build/#static-file-caching我在官方 create-react-app 文档中找到的只是: https://create-react-app.dev/docs/production-build/#static-file-caching
It did not help me change the cache policy for my static assets (I have images).它没有帮助我更改我的 static 资产的缓存策略(我有图像)。
I also tried using the "fetch" method and setting a custom header, but I ended up having a duplicate value "max-age=99999, max-age=0".我还尝试使用“获取”方法并设置自定义 header,但我最终得到了重复值“max-age=99999,max-age=0”。 It looks like create-react-app bypasses your custom headers when it comes to cache policy.在缓存策略方面,create-react-app 似乎绕过了您的自定义标头。
How do I set a custom header?如何设置自定义 header? Thank in advance!预先感谢!
If your web server can't set those headers, Workbox and Service Worker patterns would be your best bet for client-side caching.如果您的 web 服务器无法设置这些标头,那么 Workbox和Service Worker 模式将是您进行客户端缓存的最佳选择。 Were you also able to review these create-react-app docs?您是否还能够查看这些 create-react-app 文档?
You could also try using a web server like Nginx to proxy create-react-app , remove its headers, and add your own:您也可以尝试使用 web 服务器(如 Nginx)代理 create-react-app ,删除其标题,并添加您自己的:
location / {
...
// hide create-react-app response header
proxy_hide_header max-age;
// send your own header to client
add_header max-age 99999;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.