Javascript 如何清除reactjs中的浏览器缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51207570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:47:51  来源:igfitidea点击:

how to clear browser cache in reactjs

javascriptreactjspage-refresh

提问by Sunil Choudhary

every time I make changes to my website and try to update those changes to the running website, I've to hard refresh browser. is there any way to do it by code

每次我对我的网站进行更改并尝试将这些更改更新到正在运行的网站时,我都必须硬刷新浏览器。有没有办法通过代码来做到这一点

i already try to search it but most of saying it can not be done. referance post : How to programmatically empty browser cache?

我已经尝试搜索它,但大多数人说它无法完成。参考帖子:如何以编程方式清空浏览器缓存?

**Response headers :

**响应头:

HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Fri, 06 Jul 2018 10:01:23 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Content-Encoding: gzip

**Request headers :

**请求头:

GET / HTTP/1.1
Host: --example--.com
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8,hi;q=0.7
Cookie: fingerPrint=e4a9037b8f674722f414b0b59d8d663c

回答by Mustkeem K

For this specific case what you can do is to tell browser not to cache your page, by using these meta tags inside <head>tag:

对于这种特定情况,您可以通过在标签内使用这些元标签来告诉浏览器不要缓存您的页面<head>

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>

However there are multiple ways to achieve this. You could send the appropriate headers from your back end. Or if you are trying to disable cache temporally you can do it by disabling the browser cache. To do so, Follow below steps.

然而,有多种方法可以实现这一点。您可以从后端发送适当的标头。或者,如果您尝试暂时禁用缓存,则可以通过禁用浏览器缓存来实现。为此,请按照以下步骤操作。

In your developer tools. Find network tab and disable cache. Like here in the image.

在您的开发人员工具中。找到网络选项卡并禁用缓存。就像图片中的这里。

enter image description here

在此处输入图片说明

Hope this resolves.

希望这能解决。

回答by durga patra

Make a style.cssfile in your public folder then link this CSS file in your index.htmlfile.

在您的公共文件夹中创建一个style.css文件,然后将该 CSS 文件链接到您的index.html文件中。

Ex:

前任:

 <link rel="stylesheet" type="text/css" href="%PUBLIC_URL%/style.css?v=0.0.1" />

Always update the version of this CSSfils before doing build your code.

在构建代码之前,请始终更新此CSS文件的版本。

回答by Joseph Stalin

It's possible, you can simply use jQuery. please take a look at the post How to programmatically empty browser cache?

这是可能的,您可以简单地使用 jQuery。请看一下 如何以编程方式清空浏览器缓存?

回答by Thanmai C

Even if the below answers didn't work. And you are facing a problem something like your app being cached, making the whole app display an old buildthen probably you can check this link https://github.com/facebook/create-react-app/issues/1910#issuecomment-355245212. After that do an npm start, it worked for me.

即使以下答案不起作用。而且您正面临诸如您的应用程序被缓存之类的问题,使整个应用程序显示为旧版本,那么您可能可以查看此链接https://github.com/facebook/create-react-app/issues/1910#issuecomment- 355245212。之后做一个 npm start,它对我有用。

回答by rootkill

You can use the chrome dev tools. There is an option in the Network Tabto disable the cache.

您可以使用chrome 开发工具网络选项卡中有一个选项可以禁用缓存。