简体   繁体   English

angular2 禁用浏览器缓存

[英]angular2 disable browser caching

I am using angular2 for web development, and Jenkins for continuous integration, when i release code at the end of every sprint, it gets deployed on CI Server.我使用 angular2 进行 Web 开发,使用 Jenkins 进行持续集成,当我在每个冲刺结束时发布代码时,它会部署在 CI 服务器上。

But, when users load the UI, they do not get the new UI changes by default, they have to clear the cache ( I do not want users to clear the cache or disable there cache just for this UI)但是,当用户加载 UI 时,默认情况下他们不会获得新的 UI 更改,他们必须清除缓存(我不希望用户仅为此 UI 清除缓存或禁用缓存)

How can I handle programatically for the browser to not cache old files and reload the new changes by default (atleast in development)我如何以编程方式处理浏览器不缓存旧文件并默认重新加载新更改(至少在开发中)

Note: I presently set:注意:我目前设置:

import { enableProdMode } from '@angular/core';
enableProdMode();

None of the documentation states this to be the reason and removing it does not help either.没有任何文档说明这是原因,删除它也无济于事。

Two popular ways of accomplishing this "cache busting" are:完成这种“缓存破坏”的两种流行方法是:

  1. Using a query string on the end of your file request that gives a version number.在提供版本号的文件请求末尾使用查询字符串。 For example, when you create a javascript file you would name it "my-file.js".例如,当您创建一个 javascript 文件时,您可以将其命名为“my-file.js”。 Then in your HTML you would request it as:然后在您的 HTML 中,您可以将其请求为:
<script type="text/javascript" src="my-file.js?v=1.0.0"></script>

When you make some changes to your file you update your request to:当您对文件进行一些更改时,您将请求更新为:

<script type="text/javascript" src="my-file.js?v=1.0.1"></script>

You can use whatever query string you want as long as you change it.只要您更改它,您就可以使用任何您想要的查询字符串。 The browser sees it as a different file, but it should have no effect on what file your server sends as a response.浏览器将其视为不同的文件,但它应该不会影响您的服务器作为响应发送的文件。

  1. If you are using a bundler like webpack or systemJS they can automatically include a hash as part of the file name.如果您使用的是 webpack 或 systemJS 之类的打包器,它们可以自动包含一个哈希作为文件名的一部分。 This hash can change based on the file contents so that when the contents change the file name changes and thus the file is no longer cached.该散列可以根据文件内容而改变,以便当内容改变时文件名改变并且文件不再被缓存。 The caveat with this is that you need to update the file name you are requesting in your HTML.需要注意的是,您需要更新您在 HTML 中请求的文件名。 Again, most tools have a way to automatically do this for you.同样,大多数工具都有一种方法可以自动为您执行此操作。

A webpack example config to accomplish this is:完成此操作的 webpack 示例配置是:

output: {
        path: 'dist',
        publicPath: '/',
        filename: 'js/[name].[chunkhash].js'
    },

and then use the HtmlWebpackPlugin to auto-generate your index.html with the correct file names injected (with inject: true):然后使用 HtmlWebpackPlugin 自动生成您的 index.html 并注入正确的文件名(inject: true):

 plugins: [
            new HtmlWebpackPlugin({
                filename: '../index.html',
                template: './index.html',
                inject: true
            }), ...

More info on webpack file naming:关于 webpack 文件命名的更多信息:

https://github.com/webpack/docs/wiki/Configuration#output https://github.com/webpack/docs/wiki/Configuration#output

More info on html webpack plugin:有关 html webpack 插件的更多信息:

https://github.com/ampedandwired/html-webpack-plugin#basic-usage https://github.com/ampedandwired/html-webpack-plugin#basic-usage

I fixed it using a special .htaccess file.我使用特殊的 .htaccess 文件修复了它。 Just uncomment the "BROWSER CACHING" part: https://gist.github.com/julianpoemp/bcf277cb56d2420cc53ec630a04a3566只需取消注释“浏览器缓存”部分: https ://gist.github.com/julianpoemp/bcf277cb56d2420cc53ec630a04a3566

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

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