繁体   English   中英

更新服务器后如何清除缓存

[英]How can I clear the cache after updating the Server

我开发了一个应用程序,但是当我从应用程序推送最新版本到服务器时,CSS 和 JS 文件没有更新。 旧文件正在显示。 更新文件后如何清除缓存?

像 jcubic 一样编辑

现在我以这种方式使用破坏者。

索引.html:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AIS</title>
    <script src="index.php"></script>
    <link rel="stylesheet" href="<?= with_hash('style.css') ?>">
</head>

索引.php:

<php?

function with_hash($url) {
   return $url . "?" . md5(file_get_contents($url));
}
?>

错误信息:

Refused to apply style from 'http://127.0.0.1:5500/%3C?=%20with_hash(%27style.css%27)%20?%3E' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

您可以使用所谓的缓存克星。 在您的应用程序中为 JS 和 CSS 的 URL 添加唯一 ID。 I personally use the hash of the content of the file, you can get it you write the HTML file in PHP or any other server-side script, but PHP, if you have a single HTML file, is the easiest.

在 PHP 中,它可能如下所示:

<php?

function with_hash($url) {
   return $url . "?" . md5(file_get_contents($url));
}
?><!DOCTYPE HTML>
...
<link rel="stylesheet" href="<?= with_hash('produktion.css') ?>">

如果应用程序适用于 static 托管(如 Netlify),则需要使用 hash 生成 HTML 文件。 您可以使用 Webpack 轻松做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM