[转]angular 禁止缓存
本文转自:https://www.cnblogs.com/jonney-wang/p/9797906.html
angular 单页面开发,会存在和管理很多HTML和JS文件,缓存有时是个麻烦。
在开发和测试阶段,F12调出调试工具,禁止缓存F5刷新下就好了。
但是在客户那里缓存就体验效果不好,甚至认为有问题,联系客服,影响工作效率。
主要做几点就可以了,最主要的一点就是HTML和JS动态加载,点击菜单时再去加载。
项目中的库文件一般不需要管他,一百年不变,解决缓存的主要是经常变化的部分,
如:修改了页面布局,前端js逻辑发生变动。。。
最主要的策略是,为项目加版本号,不管是HTML还是js、css文件,更新发布后,
不需要客户/实施同事 F12清缓存,只需F5刷一下即可。
1、在主页面HTML上禁止缓存
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
2、项目主样式,加版本号; <link href="static/iconFont/iconfont.css?v=1" rel="stylesheet">
3、require的main文件管理常用文件的版本号;

var appVersion = '?v=2018.10.1.2';
require.config({
paths: {
'lodash': 'static/lodash.min',
'jquery': 'static/jquery-1.11.3/jquery.min',
'jqueryMig': 'static/jquery-migrate-1.4.1.min',
'autocomplete': 'static/jquery-autocomplete/jquery.autocomplete.min',
'bootstrap': 'static/bootstrap-3.3.7-dist/js/bootstrap.min',
'angular': 'node_modules/angular/angular.min',
'ui-router': 'node_modules/angular-ui-router/release/angular-ui-router.min',
'select': 'static/bootstrap-select.min',
'select-zh': 'static/bootstrap-select-zh_CN.min',
'laydate': 'static/laydate/laydate',
'layer': 'static/layer/layer',
'app': 'app.js'+appVersion,
'masterRt': '01-master/masterRouter.js'+appVersion,
'autoSvc': 'service/autoSvc.js'+appVersion,
'layerSvc': 'service/layerSvc.js'+appVersion,
'datefmt': 'prototype/date.js'+appVersion,
},
shim: {
'bootstrap': ['jquery'],
'jqueryMig': ['jquery'],
'select': {deps: ['bootstrap'], exports: 'select'},
'select-zh': {deps: ['select'], exports: 'select-zh'},
'ui-router': ['angular'],
'app': ['ui-router'],
'masterRouter': ['app'],
'autocomplete': ['jquery','jqueryMig']
}
});

4、动态加载功能页面HTML时,加版本号;即angular拦截器的request时处理;

app.factory('interceptor', function ($q, $location) {
return {
request: function (config) {
if (config.url.indexOf('/login/') === -1 && sessionStorage.session) {
var session = JSON.parse(sessionStorage.session);
config.headers['id'] = session.id;
config.headers['token'] = session.token;
} // 禁止HTML缓存
if(config.url.indexOf('.html') > 0){
//var nocache = '?v=' + new Date().getTime();
var idx = config.url.indexOf('?v=');
if(idx === -1)
config.url += appVersion;
else{
config.url = config.url.substr(0, idx) + appVersion;
}
}
return config || $q.when(config);
},
response: function (response) {
if (response.config.url.indexOf('service') > -1) {
//todo 预处理请求结果
}
return response || $q.when(response);
},
responseError: function (response) {
if (response.status === 401) {// If our response status is unauthorized
$location.path('/main/index');// Redirect to the login screen
} else {
return $q.reject(response);// Reject our response
}
}
};
});

5、动态加载功能页面的js控制器和依赖js文件时,加版本号;

// 延迟加载方法
app.loadJs = function (files) {
return {
ctrl: function ($q) {
// 禁止业务模块的js缓存
//var nocache = '?x=' + new Date().getTime();
for(var i=0; i<files.length; i++){
var idx = files[i].indexOf('?v=');
if(idx === -1)
files[i] += appVersion;
else{
files[i] = files[i].substr(0, idx) + appVersion;
}
}
var wait = $q.defer();
require(files, function () {
wait.resolve();
});
return wait.promise;
}
};
};

6、路由器的每个state定义时,动态加载js处理;

.state('main.login', {
url: '/login',
templateUrl: modulePath + 'login.html',
controller: 'loginCtrl',
resolve: app.loadJs([modulePath + 'login.js'])
})

这样处理完,发布前端项目时,注意修改项目版本号,我这里测试发布在Nginx,转发后台处理的请求。
发布后F5刷新即可,效果:
[转]angular 禁止缓存的更多相关文章
- angular 禁止缓存
angular 单页面开发,会存在和管理很多HTML和JS文件,缓存有时是个麻烦. 在开发和测试阶段,F12调出调试工具,禁止缓存F5刷新下就好了. 但是在客户那里缓存就体验效果不好,甚至认为有问题, ...
- HTML页面和JSP页面禁止缓存
一.JSP页面禁止缓存: 防止浏览器缓存当前访问的JSP动态页面,可以采用如下的方式进行设置,此效果如下的“HTML禁止缓存”: % 将过期日期设置为一个过去时间response.setHeader( ...
- Ajax禁止缓存的几个解决方案
最常用的方法是 方法1:服务器端代码加入 代码如下 复制代码 response.setHeader("Cache-Control", "no-cache, must-r ...
- Chrome调试javacript禁止缓存
/********************************************************************* * Chrome调试javacript禁止缓存 * 说明: ...
- RequireJS禁止缓存
通过配置文件可以禁止加载缓存的JS文件, 这个在开发过程中非常有用具体做法如下 require.config({ paths: { "E":"/Scripts/MyMod ...
- 【前端_js】Chrome禁止缓存的方法
在前端开发中,浏览器缓存使得我们改了代码后页面不变,得经常手动清理缓存. 1.按如下操作即可禁用浏览器缓存, 这种方法基本能够做到完全禁止缓存,然而缺点是必须要将开发模式一直打开,占用屏幕空间.而且, ...
- 网页禁止右键,禁止F12,禁止选中,禁止复制,禁止缓存等操作
一.禁止右键 //方法一 document.onmousedown = function () { ) { return false; } } //方法二 document.oncontextmenu ...
- 使用 Angular RouteReuseStrategy 缓存(路由)组件
使用 Angular RouteReuseStrategy 缓存组件 Cache components with Angular RouteReuseStrategy RouteReuseStrate ...
- angular页面缓存与页面刷新
angularJS学习笔记:页面缓存与页面刷新 遇到的问题 现在存在这样一个问题,登录前与登录成功后是同一个页面,只不过通过ngIf来控制哪部分显示,图像信息如下: 所以,整体工作不是很难,无非就 ...
随机推荐
- VS Code 之 smarty 扩展
VS Code 中的 Smarty 扩展: https://github.com/imperez/vscode-smarty 目前(v0.2.0)不支持定制定界符.可以通过 trick 的方式篡改. ...
- intentservice 内部类
https://blog.csdn.net/u010746364/article/details/50503586
- 利用Qt Designer 进行 空间提升propomotion 的时候异常: NO such file or directory
1. 因为在提升的时候,只设置了 类名,以及文件名,但是没有给定Qt 的uic 的指定搜索路径,因此报错 在生成的ui_xxxx.h文件必然找不到这个文件. 如下图: 2. 解决方法 在项目的属性中: ...
- github使用步骤
首先需要注册一个github账号 1.认识github首页界面 2.如何新建一个自己的仓库 3.创建README文件 4.创建自己的文件 5.解析文件 6.生成地址 7.如何修改编辑文件
- 安装Pangolin
Pangolin是一个用于OpenGL显示/交互以及视频输出的一个轻量级 快速开发库 一:安装必要的库 1.Glew sudo apt-get install libglew-dev 2.Cmake ...
- VSCode插件开发全攻略(八)代码片段、设置、自定义欢迎页
更多文章请戳VSCode插件开发全攻略系列目录导航. 代码片段 代码片段,也叫snippets,相信大家都不陌生,就是输入一个很简单的单词然后一回车带出来很多代码.平时大家也可以直接在vscode中创 ...
- J2EE导论 | 疑惑篇
J2EE是Java程序员从新手进阶的一个必经之路.要体会所谓的工业级代码,就必须要融入和经历更为复杂的开发.部署环境,需要同更多的模块.组件做信息流交换,比较和使用不同的框架,逐一去琢磨和考察它们的必 ...
- BCrypt实现密码的加密
这里设计到一个新的知识点,下来准备找找资料学习一下:Spring Security 我们都知道,密码这种东西存到数据库是不能以明文直接存入的,而是要经过加密,而且加密还颇多讲究 比如以前的 MD5加密 ...
- FFmpeg命令行工具学习(四):FFmpeg 采集设备
在使用 FFmpeg 作为编码器时,可以使用FFmpeg采集本地的音视频采集设备的数据,然后进行编码.封装.传输等操作. 例如,我们可以采集摄像头的图像作为视频,采集麦克风的数据作为音频,然后对采集的 ...
- 吴恩达机器学习笔记29-神经网络的代价函数(Cost Function of Neural Networks)
假设神经网络的训练样本有