有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?

使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。


当前回答

如果有兴趣,我已经找到了我的解决方案,让浏览器在。net MVC (.net fw 4.8)和使用包的情况下刷新.css和.js。 我想让浏览器仅在部署新程序集之后刷新缓存文件。

根据撒利亚多尼的回答,我的解决方案如下:

store your application base url in the web config app settings (the HttpContext is not yet available at runtime during the RegisterBundle...), then make this parameter changing according to the configuration (debug, staging, release...) by the xml transform In BundleConfig RegisterBundles get the assembly version by the means of reflection, and... ...change the default tag format of both styles and scripts so that the bundling system generates link and script tags appending a query string parameter on them.

下面是代码

public static void RegisterBundles(BundleCollection bundles)
{
   string baseUrl = system.Configuration.ConfigurationManager.AppSettings["by.app.base.url"].ToString();
       
   string assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
   
   Styles.DefaultTagFormat = $"<link href='{baseUrl}{{0}}?v={assemblyVersion}' rel='stylesheet'/>";
   Scripts.DefaultTagFormat = $"<script src='{baseUrl}{{0}}?v={assemblyVersion}'></script>";
}

你会得到这样的标签

<script src="https://example.org/myscriptfilepath/script.js?v={myassemblyversion}"></script>

您只需要记住在部署之前构建一个新版本。

Ciao

其他回答

我实现了这个简单的解决方案(还没有在生产环境中):

function verificarNovaVersio() {
    var sVersio = localStorage['gcf_versio'+ location.pathname] || 'v00.0.0000';
    $.ajax({
        url: "./versio.txt"
        , dataType: 'text'
        , cache: false
        , contentType: false
        , processData: false
        , type: 'post'
     }).done(function(sVersioFitxer) {
        console.log('Versió App: '+ sVersioFitxer +', Versió Caché: '+ sVersio);
        if (sVersio < (sVersioFitxer || 'v00.0.0000')) {
            localStorage['gcf_versio'+ location.pathname] = sVersioFitxer;
            location.reload(true);
        }
    });
}

我有一个小文件位于html的位置:

“versio.txt”:

v00.5.0014

这个函数在我的所有页面中都被调用,因此在加载时它会检查localStorage的版本值是否低于当前版本,并执行a

location.reload(true);

...强制从服务器而不是缓存重新加载。

(显然,您可以使用cookie或其他持久客户端存储而不是localStorage)

我选择这个解决方案是因为它很简单,因为只维护一个“version .txt”文件就会强制重载整个站点。

queryString方法很难实现,而且也是缓存的(如果您从v1.1更改到以前的版本将从缓存加载,那么这意味着缓存不会刷新,将所有以前的版本保存在缓存中)。

我是一个小新手,我很感激你的专业检查和审查,以确保我的方法是一个很好的方法。

希望能有所帮助。

<meta http-equiv="pragma" content="no-cache" />

参见https://stackoverflow.com/questions/126772/how-to-force-a-web-browser-not-to-cache-images

我有一个案例,我将在线拍摄客户端的照片,如果照片被更改,就需要更新div。浏览器仍然显示着旧照片。因此,我使用了调用随机GET变量的方法,该变量每次都是唯一的。这就是它,如果它能帮助到任何人的话

<img src="/photos/userid_73.jpg?random=<?php echo rand() ?>" ...

编辑 正如其他人指出的那样,下面是更有效的解决方案,因为它只会在图像被更改时重新加载图像,通过文件大小识别这种更改:

<img src="/photos/userid_73.jpg?modified=<? filemtime("/photos/userid_73.jpg")?>"

我有同样的问题,我所做的就是改变文件名链接到我的index.html文件,然后进入index.html文件,更新他们的名字,不是最好的做法,但如果它工作。浏览器将它们视为新文件,因此它们会被重新下载到用户的设备上。

例子: 我想更新一个css文件,它的命名为styles.css,把它改成styles.css

进入index。html进行更新,并将其更改为

对于webpack用户:-

我在webpack配置中添加了chunkash。这解决了每次部署时缓存失效的问题。我们还需要注意index.html/ asset。清单不是缓存在你的CDN或浏览器。在webpack配置中的块名配置将看起来像这样:-

文件名:[chunkhash] - $ {Date.now ()} . js

或者如果你使用contenthash那么

文件名:[contenthash] - $ {Date.now ()} . js