我正在开发一个移动应用程序,并使用以下命令构建并运行一个在浏览器中运行的版本(拉入所有必需的钩子,离子服务不会)
ionic build browser --desktop --testing && http-server platforms/browser/www/
昨天,几周来,这完美无缺。我停下来并启动相同的命令,它构建/编译所有内容,一切都很棒。
然后我更新了Google Chrome。现在,无论我做什么,Chrome都会继续从磁盘缓存中提取所有这些资源,即使在我删除并重新创建它们之后也是如此。我有什么想法可以解决?我不希望每次重新加载时都清除缓存,这似乎会导致其他问题。
我不知道为什么或如何改变,我的Ionic2应用程序中没有项目或配置设置不同。
使用cmd-shift-R代替cmd-R重新加载似乎迫使Chrome无法从磁盘缓存加载,但我很困惑,想要了解这里发生的事情......
答案 0 :(得分:2)
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...">
对于脚本/样式表,您可以动态创建它们以及时间戳,然后插入它们。
或者当您的脚本文件一起使用bundelt时,您可以使用脚本通过使用nodejs-script将时间戳写入您的finally index.html文件以进行部署,因为我为我的一个项目创建了此脚本:
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
希望它有所帮助。