简体   繁体   English

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

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

I have developed an app, but when I push the latest version from the app to the Server, the CSS and JS files are not updated.我开发了一个应用程序,但是当我从应用程序推送最新版本到服务器时,CSS 和 JS 文件没有更新。 The old files are showing.旧文件正在显示。 How can I clear the cache after updating the files?更新文件后如何清除缓存?

EDITED like jcubic像 jcubic 一样编辑

Now I use the buster in this way.现在我以这种方式使用破坏者。

index.html:索引.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>

index.php:索引.php:

<php?

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

Error message:错误信息:

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.

You can use what's called cache buster.您可以使用所谓的缓存克星。 Add a unique ID to the URL of the JS and CSS in your application.在您的应用程序中为 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. 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.

In PHP it can look like this:在 PHP 中,它可能如下所示:

<php?

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

If the application is for static hosting like Netlify you will need to generate an HTML file with the hash.如果应用程序适用于 static 托管(如 Netlify),则需要使用 hash 生成 HTML 文件。 You can easily do that with Webpack.您可以使用 Webpack 轻松做到这一点。

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

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