Jon*_*nas 6 flutter vercel flutter-web
我的 flutter web 应用程序无法启动,我在浏览器控制台中看到以下错误:
仅当部署在 vercel 上时才会出现此行为。在 firebase 托管上部署时,我没有收到此错误。
此外,此错误仅发生在嵌套路由中。当我打开 URL 中没有子路径的已部署应用程序时,它会起作用。
错误一定发生在loadEntrypoint
函数的某个地方
<script>
window.addEventListener('load', function (ev) {
console.log("LOAD!");
// Download main.dart.js
_flutter.loader.loadEntrypoint({
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
}
}).then(function (engineInitializer) {
console.log("INIT");
return engineInitializer.initializeEngine();
}).then(function (appRunner) {
console.log("RUN");
return appRunner.runApp();
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
有趣的是,它说Failed to register a ServiceWorker for scope ('https://domainname.net/home/')
即使我加载了页面https://domainname.net/home/questionnaire
。一般来说,我希望它会在以下位置注册 ServiceWorker https://domainname.net
,但我对 ServiceWorker 不太了解......
我对此特别感到困惑,因为这只发生在 vercel 上,但当错误发生在 中的某处时web/index.js
,它假设我的托管提供商不应该影响此行为?
有任何想法吗?
临时解决方法:(参考)
将这两行添加到您的index.html
:
<script>
window.addEventListener('load', function(ev) {
// Download main.dart.js
_flutter.loader.loadEntrypoint({
entrypointUrl: "/main.dart.js", // <-- THIS LINE
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
serviceWorkerUrl: "/flutter_service_worker.js?v=", // <-- THIS LINE
},
onEntrypointLoaded: function(engineInitializer) {
engineInitializer.initializeEngine().then(function(appRunner) {
appRunner.runApp();
});
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
我已经找到了解决这个问题的方法。
当我用以下脚本替换上面问题中的脚本时,似乎解决了问题。
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)
不幸的是,我还没有真正弄清楚问题到底是什么以及为什么要解决它,但我注意到这个问题是在我更新了我的 flutter 版本并重新生成我的 index.html 后才发生的。我现在使用的脚本是较旧的 flutter 版本使用的脚本。我不确定这是否会产生其他影响,但现在我将使用旧的脚本。
归档时间: |
|
查看次数: |
4357 次 |
最近记录: |