在当前版本的flutter 3.0.1中,我们在index.html中有一个service worker。到目前为止,我在flutter.dev上找不到任何关于如何在屏幕或代码更新时强制缓存刷新的文档。唯一的刷新方式是使用浏览器刷新按钮等。
关于SO有很多过时的建议,通过在这里或那里附加一些东西来手动更改版本号,但这并不适用于serviceworker。每次构建时,运行flutter build web
的service worker都会自动获得一个新的版本号。但是,这不会在浏览器中强制刷新缓存。
默认情况下,这个service worker js正在监听statechange
,然后调用console.log('Installed new service worker.');
。这只在你点击浏览器的刷新按钮时调用,所以提示或强制刷新新内容是没有帮助的。
我尝试使用flutter build web --pwa-strategy=none
,这也不影响缓存。
有什么好主意可以强制刷新吗?在其他网站,我已经建立了很容易版本css/js文件,这将强制刷新没有任何用户交互,但我没有看到任何清晰的路径与flutter build web
。
这是web/index.html
中的serviceWorker js。
运行flutter build web
后,var serviceWorkerVersion = null
在部署到主机和web上时将变成类似var serviceWorkerVersion = '1767749895';
的东西。但是,此serviceWorkerVersion更新不会强制刷新内容。
<script>
var serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'main.dart.js';
scriptTag.type = 'application/javascript';
document.body.append(scriptTag);
}
if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing || reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('Loading app from service worker.');
loadMainDartJs();
}
});
// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
});
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
你试过吗?在"index.html
. conf"文件中添加no-cache
。好像在做Flutter 3.0。请记住,如果这样做,您的pwa将无法在脱机模式下工作。
<head>
...
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="pragma" content="no-cache" />
...
</head>
您可以使用Firestore侦听器来实时查看特定文档或Firebase远程配置,以检查是否需要清除缓存。
一旦需要清除网页缓存,只需运行:
import 'dart:html' as html show window;
html.window.location.reload();
https://firebase.google.com/docs/remote-config