【问题标题】:Blazor client side UI updates are not reflected未反映 Blazor 客户端 UI 更新
【发布时间】: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 */

我尝试了以下方法来在浏览器中更新更改:

  1. 删除服务器上所有发布的文件,上传新文件,重启服务器
  2. 重新启动网站服务
  3. 每次我上传新版本的网站时,都会向 service-worker.published.js 添加时间戳。
  4. 使用清除存储功能删除了应用程序缓存,并确保选中所有复选框。

不确定如何让网站在发布后刷新我的更改。非常感谢任何帮助。

【问题讨论】:

  • 更新并重新发布后,请尝试以下操作:关闭浏览器并重新打开。打开开发工具并选择网络选项卡。导航到该站点(这应该会下载您的新服务工作人员) - 检查服务工作人员是否转移。检查应用程序选项卡/服务工作者部分 - 它应该显示更新可用但未激活。选择“重新加载时更新”选项并重新加载页面 - 服务工作者应该更改为已激活并正在运行。检查 service worker 的内容 - 确保它已更新。如果其中任何一个失败,请检查网络流量是否有问题
  • @MisterMagoo 感谢您的回复。我已经尝试过你的建议,但是没有任何效果。我终于弄清楚那是什么了。事实证明,我只更新了由 Visual Studio 中的发布功能生成的 service worker 和 base dll。我还必须更新位于 _framework 文件夹中的文件。

标签: blazor blazor-server-side blazor-client-side


【解决方案1】:

因此,在经历了很多挫折之后,事实证明 _framework 文件夹中的文件也必须更新,这些文件也是由 Visual Studio 中的发布功能生成的。我希望这可以帮助人们避免漫长而令人沮丧的一天。

【讨论】:

  • 当我们运行一个新的编译时,那些 _framework 文件没有更新?每次我编译时,我都必须清除缓存,这很麻烦,而且在发布时我会遇到问题。
  • 这是我在使用 Blazor 时的经验。我停止在我正在从事的项目中使用该技术,而是选择了其他东西。我希望在未来的版本中,事情会更加强大。除非有人有关于该问题的更新是固定的。
猜你喜欢
  • 2020-04-22
  • 2018-02-07
  • 2020-07-26
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 2020-10-27
  • 2012-03-20
  • 2020-11-06
相关资源
最近更新 更多