Blazor-由于完整性检查失败,服务工作者无法安装
创始人
2024-12-20 22:30:55
0

这个问题通常是由于时间戳不同步导致的。您可以尝试使用以下方法来解决此问题:

在您的Blazor项目中,找到 wwwroot/service-worker.published.js 文件并删除它。然后,重新生成该文件。

Startup.cs 文件中添加以下代码片段:

public void ConfigureServices(IServiceCollection services)
{
    services.AddServerSideBlazor()
        .AddCircuitOptions(options => { options.DetailedErrors = true; });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseStaticFiles();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();

    app.UseBlazorFrameworkFiles();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
    });
}

然后,在您的 wwwroot 文件夹中创建一个 service-worker.js 文件。在该文件中添加以下代码:

self.addEventListener('activate', function (event) {
    event.waitUntil(
        caches.keys().then(function (cacheNames) {
            return Promise.all(
                cacheNames.filter(function (cacheName) {
                    // Return true if you want to remove this cache,
                    // but remember that caches are shared across
                    // the whole origin
                    return true;
                }).map(function (cacheName) {
                    return caches.delete(cacheName);
                })
            );
        })
    );
});

最后,您需要更新您的 index.html 文件。在 head 元素中添加以下代码: