Zel*_*elf 16 flutter flutter-web
在当前版本的 flutter 3.0.1 中,我们在 index.html 中有一个 Service Worker。到目前为止,我在 flutter.dev 上找不到任何有关如何在屏幕或代码更新时强制刷新缓存的文档。刷新的唯一方法是使用浏览器刷新按钮等。
有很多过时的建议,通过在此处或那里附加一些内容来手动更改版本号,但这不适用于服务工作者。每次构建时,服务工作线程在运行时flutter build web都会自动获取新的版本号。但是,这不会强制刷新浏览器中的缓存。
默认情况下,这个 Service Worker js 正在监听 a statechange,然后调用console.log('Installed new service worker.');这仅在您单击浏览器的刷新按钮时调用,因此提示或强制刷新新内容没有帮助。
我尝试使用flutter build web --pwa-strategy=none,这也不影响缓存。
有什么好主意可以强制刷新吗?在我构建的其他网站中,对 css/js 文件进行版本控制非常容易,这将在没有任何用户交互的情况下强制刷新,但我没有看到任何清晰的flutter build web.
web/index.html.运行后,flutter build web将var serviceWorkerVersion = null变得像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>
Run Code Online (Sandbox Code Playgroud)
你试过这个吗?添加no-cache在index.html. 看起来像是在开发 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6065 次 |
| 最近记录: |