本文介绍了如何在create-react-app中为index.html指定Cache-Control标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循create-react-app.dev的生产版本文档:

I'm trying to follow the guidance on create-react-app.dev's Production Build documentation:

使用index.html中的HTML标头是正确的方法-例如:

Is the correct way to do this to use HTML headers in index.html - eg something like:

<meta http-equiv="Cache-Control" content="max-age: 31536000, no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">

(信用:此堆栈溢出响应此YouTube教程)

如果是这样,我如何遵循文档中的建议,即应为您的构建/静态资产设置"max-age = 31536000,对其他所有内容设置Cache-Control:no-cache"?我不知道如何为不同的资产设置不同的控件.

If so, how do I follow the documentation's suggestion that I should set "max-age=31536000 for your build/static assets, and Cache-Control: no-cache for everything else"? I don't know how to set different controls for different assets.

推荐答案

正如Evans提到的那样,应从服务器端设置此标头.后端编程语言/服务器之间实际设置标头的方式有所不同.

As Evans mentioned this headers should be set from the server side. How you actually set the headers differs between backend programming languages/servers.

以下是一些示例:

  1. Node.js res.setHeader('Cache-Control','no-cache');
  2. Nginx add_header Cache-Control no-cache;

这篇关于如何在create-react-app中为index.html指定Cache-Control标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:56