强制浏览器在React中清除缓存

huangapple go评论108阅读模式
英文:

Force browser to clean cache in react

问题

尝试使用缓存破坏技术来清除每个发布的缓存。使用 new Date().getTime()Hash() 方法来创建一个唯一的版本值,并在文件路径后附加该版本值。

这应该将版本号应用于构建目录中的 main.js 文件,这样我就不必为所有的 js 和 CSS 文件添加版本号。

  1. <link style="stylesheet" href={'main.css?v=${value}'}/>
  1. <script src={'main.js?v=${value}'} />
英文:

Trying to use Cache Busting Technique to clear the cache for each release. Used new Date().getTime() or Hash() method to create a unique version value and append the version value after the file path.

This should apply the version number to the main.js file from the build directory, so I don't have to add the version number to all js and CSS files.

1.

  1. <link style="stylesheet" href={'main.css?v=${value}'}/>

2.

  1. <script src={'main.js?v=${value}'} />

答案1

得分: 0

在 React 中,你可以使用第三方库,如 react-cache-buster 来进行缓存破坏(Cache Busting)。
该库允许客户端在生产环境中发布新版本时自动检查新版本,并在发布新版本时清除缓存并重新加载页面。

例如:

  1. import React from 'react';
  2. import CacheBuster from 'react-cache-buster';
  3. import { version } from '../package.json';
  4. import Loading from './loading';
  5. const App = () => {
  6. const isProduction = process.env.NODE_ENV === 'production';
  7. return (
  8. <CacheBuster
  9. currentVersion={version}
  10. isEnabled={isProduction} // 如果为 false则禁用该库
  11. isVerboseMode={false} // 如果为 true则该库将在控制台中写入详细日志
  12. loadingComponent={<Loading />} // 如果不传递则在检查新版本时不会显示任何内容
  13. metaFileDirectory='.' // 如果公共资源托管在服务器根目录之外的其他位置
  14. >
  15. // 你的实际根组件...
  16. </CacheBuster>
  17. );
  18. };
  19. export default App;

了解更多,请参阅 https://www.npmjs.com/package/react-cache-buster

英文:

In react, you can use third-party library such as react-cache-buster for Cache Busting.
This library allows clients to automatically check the new version when a new version is released in the production environment, and if a new version is published, clearing the cache and reload the page.

For example:

  1. import React from &#39;react&#39;;
  2. import CacheBuster from &#39;react-cache-buster&#39;;
  3. import { version } from &#39;../package.json&#39;;
  4. import Loading from &#39;./loading&#39;;
  5. const App = () =&gt; {
  6. const isProduction = process.env.NODE_ENV === &#39;production&#39;;
  7. return (
  8. &lt;CacheBuster
  9. currentVersion={version}
  10. isEnabled={isProduction} //If false, the library is disabled.
  11. isVerboseMode={false} //If true, the library writes verbose logs to console.
  12. loadingComponent={&lt;Loading /&gt;} //If not pass, nothing appears at the time of new version check.
  13. metaFileDirectory={&#39;.&#39;} //If public assets are hosted somewhere other than root on your server.
  14. &gt;
  15. // Your actual root component...
  16. &lt;/CacheBuster&gt;
  17. );
  18. };
  19. export default App;

To learn more, see https://www.npmjs.com/package/react-cache-buster

huangapple
  • 本文由 发表于 2023年7月14日 04:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定