我正在努力解决一个我已经对如何做到这一点进行了很好的研究的案例。它适用于所有人,但不适用于我!
环境:
- 微软视窗 10 专业版
- Visual Studio 社区版 2019
因此,我创建了一个项目并从一开始就选择了渐进式 Web 应用程序 (PWA) 来自动生成我需要的一切。还托管了应用程序 ASP.NET Core。
启动项目是独立模式下的 Rihal.Server(不是 IIS 或 Docker)。
当我启动应用程序时,我会在 Chrome 中收到默认提示以安装应用程序。当我单击“+”图标时,我会看到默认的 Blazor 图标和应用程序的名称“Rihal”。到现在为止还挺好。当我更改图标和名称时,我仍然保持默认设置!
- 我已经尝试一一清理和重建所有项目,没有错误。
- 我试过重新启动 Visual Studio。
- 我尝试更改清单中的其他内容,例如背景颜色以查看任何更改,没有任何更改。
- 我已经检查了构建操作(内容)。默认复制到输出目录是(不复制),更改为(始终复制)无效。
就像清单被完全忽略一样。
我已经开发了这个应用程序的其他部分,然后来改变图标和名称:
{ "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" } ] }
其他相关文件:
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', () => { });
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); }
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>