簡體   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