【发布时间】:2021-04-01 00:28:30
【问题描述】:
我正在开发一个托管在 Ubuntu 中的 Blazor WebAssembly 应用程序,并使用 Nginx 作为代理。但是,当我发布应用程序并将其上传到服务器时,我所做的任何 UI 更改都不会反映在客户端上。该应用程序是 PWA。
service-worker.js
// In development, always fetch from the network and do not enable offline support.
// This is because caching would make development more difficult (changes would not
// be reflected on the first load after each change).
self.addEventListener('fetch', () => { });
service-worker.published.js
// Caution! Be sure you understand the caveats before publishing an application with
// offline support. See https://aka.ms/blazor-offline-considerations
self.importScripts('./service-worker-assets.js');
self.addEventListener('install', event => event.waitUntil(onInstall(event)));
self.addEventListener('activate', event => event.waitUntil(onActivate(event)));
self.addEventListener('fetch', event => event.respondWith(onFetch(event)));
const cacheNamePrefix = 'offline-cache-';
const cacheName = `${cacheNamePrefix}${self.assetsManifest.version}`;
const offlineAssetsInclude = [ /\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/ ];
const offlineAssetsExclude = [ /^service-worker\.js$/ ];
async function onInstall(event) {
console.info('Service worker: Install');
// Fetch and cache all matching items from the assets manifest
const assetsRequests = self.assetsManifest.assets
.filter(asset => offlineAssetsInclude.some(pattern => pattern.test(asset.url)))
.filter(asset => !offlineAssetsExclude.some(pattern => pattern.test(asset.url)))
.map(asset => new Request(asset.url, { integrity: asset.hash }));
await caches.open(cacheName).then(cache => cache.addAll(assetsRequests));
}
async function onActivate(event) {
console.info('Service worker: Activate');
// Delete unused caches
const cacheKeys = await caches.keys();
await Promise.all(cacheKeys
.filter(key => key.startsWith(cacheNamePrefix) && key !== cacheName)
.map(key => caches.delete(key)));
}
async function onFetch(event) {
let cachedResponse = null;
if (event.request.method === 'GET') {
// For all navigation requests, try to serve index.html from cache
// If you need some URLs to be server-rendered, edit the following check to exclude those URLs
const shouldServeIndexHtml = event.request.mode === 'navigate';
const request = shouldServeIndexHtml ? 'index.html' : event.request;
const cache = await caches.open(cacheName);
cachedResponse = await cache.match(request);
}
return cachedResponse || fetch(event.request);
}
/* updated 2020-12-22 11:19 */
我尝试了以下方法来在浏览器中更新更改:
- 删除服务器上所有发布的文件,上传新文件,重启服务器
- 重新启动网站服务
- 每次我上传新版本的网站时,都会向 service-worker.published.js 添加时间戳。
- 使用清除存储功能删除了应用程序缓存,并确保选中所有复选框。
不确定如何让网站在发布后刷新我的更改。非常感谢任何帮助。
【问题讨论】:
-
更新并重新发布后,请尝试以下操作:关闭浏览器并重新打开。打开开发工具并选择网络选项卡。导航到该站点(这应该会下载您的新服务工作人员) - 检查服务工作人员是否转移。检查应用程序选项卡/服务工作者部分 - 它应该显示更新可用但未激活。选择“重新加载时更新”选项并重新加载页面 - 服务工作者应该更改为已激活并正在运行。检查 service worker 的内容 - 确保它已更新。如果其中任何一个失败,请检查网络流量是否有问题
-
@MisterMagoo 感谢您的回复。我已经尝试过你的建议,但是没有任何效果。我终于弄清楚那是什么了。事实证明,我只更新了由 Visual Studio 中的发布功能生成的 service worker 和 base dll。我还必须更新位于 _framework 文件夹中的文件。
标签: blazor blazor-server-side blazor-client-side