【问题标题】:Ionic 2, Chrome keeps loading from disk cacheIonic 2,Chrome 不断从磁盘缓存加载
【发布时间】:2016-12-21 19:26:22
【问题描述】:

我正在开发一个移动应用程序并使用以下命令来构建和运行在浏览器中工作的版本(拉入所有必要的挂钩,而 ionic serve 没有)

ionic build browser --desktop --testing && http-server platforms/browser/www/

昨天和几周后,这一切都很好。我停止并启动相同的命令,它构建/编译所有内容,一切都很棒。

然后我更新了谷歌浏览器。现在,无论我做什么,Chrome 都会不断从磁盘缓存中提取所有这些资源,即使在我删除并重新创建它们之后也是如此。有什么想法可以解决吗?我不想每次重新加载时都必须清除缓存,这似乎会导致其他问题。

我不知道为什么或如何改变,我的 Ionic2 应用程序中没有项目或配置设置不同。

使用 cmd-shift-R 而不仅仅是 cmd-R 重新加载似乎会强制 Chrome 不从磁盘缓存加载,但我很困惑,想了解这里发生了什么......

【问题讨论】:

    标签: google-chrome ionic-framework ionic2 browser-cache


    【解决方案1】:

    Chrome 缓存很多,但您可以强制它从服务器加载资源,而不是使用缓存清除将它们从缓存中取出:

    加载模板:

    var timestamp = (new Date()).getTime();
    
    $ionicPopup.show({
        scope: $scope,
        title: 'Work!',
        templateUrl: '/templates/roll-timer-popup.html?update='+timestamp
    });
    

    加载脚本/样式表:

    <script src="myscripts.js?update=123..."></script>
    
    <link rel="stylesheet" type="text/css" href="theme.css?update=123...">
    

    对于脚本/样式表,您可以动态创建它们以及时间戳并在之后插入它们。

    或者当您的脚本文件捆绑在一起时,您可以使用脚本将时间戳写入您的最终 index.html 文件以通过使用 nodejs 脚本进行部署,因为我为我的一个项目制作了这个脚本:

    const fs = require('fs');
    
    var filename = process.argv[2];
    
    var regExp = /[.|\w]+[.]js/;
    var contentToAttach = ['<!-- CONTENT ATTACHED BY '+process.argv[1]+' -->','you can put other content in here that is put into at the end of your file'];
    
    fs.readFile(filename, {
        flag : 'r',
        encoding: 'utf-8'
    },(err, oldFileContent) => {
        if (err) throw err;
        var fileContent = oldFileContent;
        var oldScriptName;
        var now = (new Date()).getTime();
        var lastIndexPos = 0;
        while (oldScriptName = oldFileContent.match(regExp)) {
            var newScriptName = oldScriptName + '?update=' + now;
            var newIndexPos = fileContent.indexOf(oldScriptName);
            if (newIndexPos !== lastIndexPos) {
                fileContent = fileContent.replace(oldScriptName, newScriptName);
                lastIndexPos = newIndexPos;
            }
            else {
                // same filename found
                var fildContentSlice = fileContent.slice(newIndexPos+oldScriptName.length);
                fildContentSlice = fildContentSlice.replace(oldScriptName, newScriptName);
                fileContent = fileContent.slice(0,newIndexPos+oldScriptName.length)+fildContentSlice;
            }
            oldFileContent = oldFileContent.replace(oldScriptName, '');
        }
        var wholeContent = fileContent+'\n\r'+contentToAttach.join('\n\r');
        fs.writeFile(filename,wholeContent, (err) => {
            if (err) throw err;
            console.log('File: '+filename+' is updated!');
        });
    });
    

    它将 ?update=123... 插入到它可以在文件中找到的每个脚本标记中。 要在 shell 中执行此脚本,请编写:

    node updateScript.js path_to_your_html_file
    

    希望对你有帮助。

    【讨论】:

    • 谢谢,但这不是我正面临的问题。 Ionic2 将所有内容捆绑到恰好具有相同名称的 perdy 包中,Chrome 决定通过缓存这些包来提供帮助。所以,这实际上就是一切。对于已部署的应用程序来说会很好,并且我提到的 ctrl+shift+R 重新加载运行良好......我只是想了解发生了什么,因为我的项目中没有任何变化,只是将 Chrome 更新到最新版本:-\很烦人
    • 不客气。抱歉,我不知道 chrome 是如何实现的,但我已经更新了我的答案以帮助您处理捆绑的脚本文件。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 2017-11-19
    • 2018-01-11
    • 2022-12-20
    • 2019-05-27
    • 1970-01-01
    • 2015-05-16
    相关资源
    最近更新 更多