[转]angular 禁止缓存

摘要:
在开发和测试阶段,F12调用调试工具,可以禁用F5缓存刷新。v=2018.10.1.2';要求配置;4.动态加载功能页HTML时添加版本号;即,角度拦截器的请求时间处理;app.factory('interceptor',函数($q,$location){return{request:function(config){if(config.url.indexOf('/login/')==-1&&sessionStorage.session){varsession=JSON.parse(sessionStorage.session);config.headers['id']=session.id;config.hheaders['token']=会话令牌;}//如果(config.url.indexOf('.HTML')˃0){//varnocache='?V=');ifconfig.url+=appVersion;否则{config.url=config.url.substr+appVersion;}}returnconfig||$q。当;}时,响应:函数{if{//todo预处理请求结果}returnresponse | |$q。当;}时,responseError:函数{if{//如果您的响应状态未经授权$location.path;//重定向到登录屏幕}否则{return$q.reject;//拒绝响应}};});5.当动态加载函数页的js控制器和依赖js文件时,添加版本号//延迟加载方法appLoadJs=function{return{ctrl:function($q){//禁用业务模块的js缓存//varnocache='?V=');iffiles[i]+=appVersion;否则{files[i]=files[i].substr+appVersion;}}varwait=$q。defer();要求returnwait。许诺;};};6.定义路由器的每个状态时,动态加载js进行处理

本文转自: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="http://t.zoukankan.com/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 禁止缓存第9张

免责声明:文章转载自《[转]angular 禁止缓存》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MYSQL5.7服务搭建Django之缓存、信号和图片验证码、ORM性能下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python 网络爬虫(二)

一、编写第一个网络爬虫 为了抓取网站,我们需要下载含有感兴趣的网页,该过程一般被称为爬取(crawling)。爬取一个网站有多种方法,而选择哪种方法更加合适,则取决于目标网站的结构。 首先探讨如何安全的下载网页,让后介绍3中爬去网站的常见方法: -- 爬取网站地图; -- 遍历每个网页的数据库 ID; -- 跟踪网页链接; 1、下载网页 要想爬取网页,我们...

[转]JAVA读取外部资源的方法

在java代码中经常有读取外部资源的要求:如配置文件等等,通常会把配置文件放在classpath下或者在web项目中放在web-inf下. 1.从当前的工作目录中读取: try { BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("wkdi...

python获取文件的绝对路径

python获取文件的绝对路径 importos defload_file(): #获取当前文件路径 current_path = os.path.abspath(__file__) #获取当前文件的父目录 father_path = os.path.abspath(os.path.dirname(current_pat...

shiro细节、默认的过滤器、匹配模式和顺序

部分细节 [urls] 部分的配置,其格式是:“url=拦截器[参数],拦截器[参数]”; 如果当前请求的url匹配[urls] 部分的某个url模式,将会执行其配置的拦截器。 anon(anonymous)拦截器表示匿名访问(即不需要登录即可访问) authc(authentication)拦截器表示需要身份认证通过后才能访问 shiro中默认的过滤...

[转] Blob对象

Blob是计算机界通用术语之一,全称写作:BLOB(binary large object),表示二进制大对象。MySql/Oracle数据库中,就有一种Blob类型,专门存放二进制数据。在javascript中,Blob通常表示二进制数据,不过它们不一定非得是大量数据,Blob也可以表示一个小型文本文件的内容。本文将详细介绍Blob 构造函数 Blob(...

requests + bs4 爬取豌豆荚所有应用的信息

1.分析豌豆荚的接口的规律 - 获取所有app的接口url 2.往每一个接口发送请求,获取json数据 解析并提取想要的数据 app_data: 1.图标 app_img_url 2.名字 app_name - 3.下载量 app_download_num - 4.大小 app_size - 5.简介 app_comment - 6.详情页url a...