Blazor WASM PWA 无法更改图标,无法更改名称

Wal*_*thi 5 asp.net-core manifest.json blazor blazor-webassembly blazor-pwa

我正在努力解决一个案例,我已经对如何解决这个问题做了很好的研究。它对每个人都有效,但对我不起作用!

环境:

  • 微软 Windows 10 专业版
  • Visual Studio 社区版 2019

因此,我创建了一个项目,并从一开始就选择了渐进式 Web 应用程序 (PWA),以自动生成我需要的所有内容。还使应用程序 ASP.NET Core 托管。

启动项目是独立模式下的 Rihal.Server(不是 IIS 或 Docker)。

当我启动应用程序时,我在 Chrome 中收到安装应用程序的默认提示。当我单击“+”图标时,我会看到默认的 Blazor 图标和应用程序的名称“Rihal”。到目前为止,一切都很好。当我更改图标和名称时,我仍然保留默认值! 在此输入图像描述

  1. 我已经尝试一一清理和重建所有项目,没有错误。
  2. 我尝试过重新启动 Visual Studio。
  3. 我尝试更改清单中的其他内容(例如背景颜色)以查看任何变化,但没有任何变化。
  4. 我已经检查了构建操作(内容)。默认复制到输出目录为(不复制),更改为(始终复制)无效。

就像清单被完全忽略一样。

我开发了这个应用程序的其他部分,然后更改了图标和名称:

  1. 替换了默认图标(与较小分辨率下的 192 使用的图标相同) 在此输入图像描述

  2. 更新了我的manifest.json以获取192尺寸和名称/短名称:(我也尝试添加多个尺寸)。

{
  "name": "Rihal Timesheet",
  "short_name": "Timesheet",
  "start_url": "./",
  "display": "standalone",
  "background_color": "#ffffff",
  "theme_color": "#03173d",
  "icons": [
    {
      "src": "icon-512.png",
      "type": "image/png",
      "sizes": "512x512"
    },
    {
      "src": "icon-192.png",
      "type": "image/png",
      "sizes": "192x192"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

其他相关文件:

Rihal.Client 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', () => { });
Run Code Online (Sandbox Code Playgroud)

Rihal.Client 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);
}
Run Code Online (Sandbox Code Playgroud)

Rihal.Client csproj 文件

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
    <ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>embedded</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.2.0-preview3.20168.3" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="3.2.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.5" />
    <PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Shared\Rihal.Shared.csproj" />
  </ItemGroup>

  <ItemGroup>
    <ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

Bri*_*ker 6

打开浏览器调试窗口。Chrome 或新的 Edge。当它打开时,右键单击页面上的刷新按钮,选择=>清空缓存并硬重新加载。

我发现您可以在客户端 csproj 文件中执行此操作。在 Service Worker 中强制并更新。


<Target Name="PublishServiceWorker" AfterTargets="CopyFilesToPublishDirectory">
        <WriteLinesToFile File="$(PublishDir)wwwroot\service-worker.js" Lines="/* $([System.Guid]::NewGuid()) */" />
</Target>
Run Code Online (Sandbox Code Playgroud)