【问题标题】:javascript how to force hard refresh of current pagejavascript如何强制硬刷新当前页面
【发布时间】:2021-05-24 06:47:29
【问题描述】:

我在 web 应用中有一个按钮,点击该按钮时应该hard-重新加载页面及其所有组件。
我对其中一个 javascript 文件 (file1.js) 进行了更改,但更改未更新。

根据: herehere 使用 window.location.reload(true); 应该从服务器重新加载页面。
在我的程序中,单击按钮会触发这行代码,但更改仍然没有显示出来。
代码通过 webpack 打包。

点击按钮时,Chrome 调试器中的网络标签会显示与重新加载页面相关的网络请求。
其中之一是主页。

  • 它的请求标头显示cache-control: no-cache
  • 它的响应标头显示Cache-Control

在列表的更下方,我看到 app.bundle.js,其中包括特定文件 file1.js
app.bundle.jsGeneral 部分显示 Status Code: 200 (from memory cache)

很明显,并非页面的所有元素都是从服务器重新加载的。
以下是 Chrome 调试器“网络”选项卡中的部分。
如何强制加载所有页面,而不是从缓存中加载?

Chrome 调试器中的 Network 选项卡中的部分
# Network request fields for getting https://staging.bldlog.com/index

General:
   Request URL: https://staging.bldlog.com/index
   Request Method: GET
   Status Code: 200 
   ...

Request Headers:
   :authority: staging.bldlog.com
   :method: GET
   :path: /index
...
   cache-control: no-cache


Response Headers:
   access-control-allow-headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range


# --------------------------------------------------------------

# Network request fields for getting https://staging.bldlog.com/V1/build/mlj/app.bundle.js

General:
   Request URL: https://staging.bldlog.com/V1/build/mlj/app.bundle.js
   Request Method: GET
   Status Code: 200  (from memory cache)

【问题讨论】:

标签: javascript caching browser-cache cache-control


【解决方案1】:

解决方案是指定 { 'Cache-Control': 'no-cache, no-store, must-revalidate' }; 按照here 中的说明在请求标头中添加

【讨论】:

  • 你的意思是响应头吗?
  • 我将它放在请求标头中,在 javascript 代码中。这会导致浏览器不返回缓存响应,而是重定向到后端。
猜你喜欢
  • 2011-01-07
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 2012-08-13
  • 2010-10-30
  • 1970-01-01
  • 2011-07-15
  • 2021-11-16
相关资源
最近更新 更多