Blazor 中的缓存清除

Pan*_*wat 5 blazor blazor-client-side

Blazor 中有缓存清除的解决方案吗?我在 Blazor WebAssembly 中转换了我的 asp.net core 应用程序,我在 razor 页面中使用它asp-append-version=true来进行客户端缓存更新。

发现DLL 存在同样的问题

小智 5

我和我的同事Nurzhan AitbayevYerassyl Shalabayev开发了以下解决方案:

\n
    \n
  1. 将版本存储在 web.client\'s wwwroot/appsettings.json 中
  2. \n
\n
"ApplicationSettings": {\n    ...\n    "Version": "Release-0"\n  }\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 将版本存储在 web.server appsettings.json 中
  2. \n
\n
"ApplicationSettings": {\n    ...\n    "Version": "Release-0"\n  }\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. Web 服务器始终在标头中返回实际版本:
  2. \n
\n
app.Use(async (context, next) =>\n            {\n                context.Response.Headers.Add("Application-Version", Configuration["ApplicationSettings:Version"]);\n                await next.Invoke();\n            });\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 客户端检查每个响应的版本
  2. \n
\n
private void CheckAppVersion(HttpResponseMessage response)\n        {\n            if (response == null)\n            {\n                throw new ArgumentNullException(nameof(response));\n            }\n\n            response.Headers.TryGetValues("Application-Version", out var headers);\n            if (headers == null || !headers.Any())\n            {\n                return;\n            }\n\n            var appVersion = headers.First();\n            if (!string.IsNullOrWhiteSpace(appVersion))\n            {\n                var localVersion = _applicationSettings.Version;\n                var isActual = string.Compare(localVersion, appVersion, StringComparison.OrdinalIgnoreCase);\n\n                if (isActual != 0)\n                {\n                    throw new OldVersionException("\xd0\x92\xd0\xb5\xd1\x80\xd1\x81\xd0\xb8\xd1\x8f \xd0\xbf\xd1\x80\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xb6\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f \xd0\xbe\xd0\xb1\xd0\xbd\xd0\xbe\xd0\xb2\xd0\xb8\xd0\xbb\xd0\xb0\xd1\x81\xd1\x8c");\n                }\n            }\n        }\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 如果用户出现异常,则重新加载页面:\nC#
  2. \n
\n
protected async Task RefreshPageAsync()\n        {\n            await JSRuntime.InvokeVoidAsync("reloadPage");\n        }\n
Run Code Online (Sandbox Code Playgroud)\n

JS

\n
<script>\n        function reloadPage() {\n            location.reload(true);\n        }\n    </script>\n
Run Code Online (Sandbox Code Playgroud)\n


Pan*_*wat 2

根据@Mister Comment添加我的答案。

BlazorPWA.MSBuildnuget包是生成PWA所需的文件。

对于更新缓存,您每次要发布代码时都需要更改 proj 文件。

<ServiceWorkerCacheVersion>1</ServiceWorkerCacheVersion>
<ServiceWorkerForce>true</ServiceWorkerForce>
Run Code Online (Sandbox Code Playgroud)

如果你想实现这个 nuget 包,请检查此项