From 0cc06f20d7da762080c0fec88a32193c75ae8f2b Mon Sep 17 00:00:00 2001 From: Michael Bond Date: Tue, 19 Dec 2023 19:04:32 -0500 Subject: [PATCH] Revert back to WASM --- ...otnet-8-release-new-template_pkmds-web.yml | 57 ------------------ .github/workflows/main.yml | 49 +++++++++++++++ Pkmds.Web/App.razor | 16 +++++ Pkmds.Web/Components/App.razor | 26 -------- Pkmds.Web/Components/Pages/Error.razor | 32 ---------- Pkmds.Web/Components/Pages/Error.razor.cs | 14 ----- Pkmds.Web/Components/Routes.razor | 8 --- Pkmds.Web/GlobalUsings.cs | 5 +- .../{Components => }/Layout/MainLayout.razor | 0 .../Layout/MainLayout.razor.cs | 2 +- .../Layout/MainLayout.razor.css | 0 Pkmds.Web/{Components => }/Pages/Home.razor | 0 .../{Components => }/Pages/Home.razor.cs | 2 +- Pkmds.Web/Pkmds.Web.csproj | 15 ++++- Pkmds.Web/Program.cs | 28 ++------- Pkmds.Web/Properties/launchSettings.json | 11 ++-- Pkmds.Web/_Imports.razor | 1 + Pkmds.Web/appsettings.Development.json | 8 --- Pkmds.Web/appsettings.json | 9 --- Pkmds.Web/wwwroot/app.css | 24 -------- Pkmds.Web/wwwroot/css/app.css | 57 ++++++++++++++++++ Pkmds.Web/wwwroot/icon-192.png | Bin 0 -> 2626 bytes Pkmds.Web/wwwroot/index.html | 38 ++++++++++++ Pkmds.Web/wwwroot/manifest.webmanifest | 22 +++++++ Pkmds.Web/wwwroot/service-worker.js | 4 ++ Pkmds.Web/wwwroot/service-worker.published.js | 55 +++++++++++++++++ Pkmds.sln | 10 +-- 27 files changed, 279 insertions(+), 214 deletions(-) delete mode 100644 .github/workflows/dotnet-8-release-new-template_pkmds-web.yml create mode 100644 .github/workflows/main.yml create mode 100644 Pkmds.Web/App.razor delete mode 100644 Pkmds.Web/Components/App.razor delete mode 100644 Pkmds.Web/Components/Pages/Error.razor delete mode 100644 Pkmds.Web/Components/Pages/Error.razor.cs delete mode 100644 Pkmds.Web/Components/Routes.razor rename Pkmds.Web/{Components => }/Layout/MainLayout.razor (100%) rename Pkmds.Web/{Components => }/Layout/MainLayout.razor.cs (99%) rename Pkmds.Web/{Components => }/Layout/MainLayout.razor.css (100%) rename Pkmds.Web/{Components => }/Pages/Home.razor (100%) rename Pkmds.Web/{Components => }/Pages/Home.razor.cs (85%) delete mode 100644 Pkmds.Web/appsettings.Development.json delete mode 100644 Pkmds.Web/appsettings.json delete mode 100644 Pkmds.Web/wwwroot/app.css create mode 100644 Pkmds.Web/wwwroot/css/app.css create mode 100644 Pkmds.Web/wwwroot/icon-192.png create mode 100644 Pkmds.Web/wwwroot/index.html create mode 100644 Pkmds.Web/wwwroot/manifest.webmanifest create mode 100644 Pkmds.Web/wwwroot/service-worker.js create mode 100644 Pkmds.Web/wwwroot/service-worker.published.js diff --git a/.github/workflows/dotnet-8-release-new-template_pkmds-web.yml b/.github/workflows/dotnet-8-release-new-template_pkmds-web.yml deleted file mode 100644 index 7aa44993..00000000 --- a/.github/workflows/dotnet-8-release-new-template_pkmds-web.yml +++ /dev/null @@ -1,57 +0,0 @@ -# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy -# More GitHub Actions for Azure: https://github.com/Azure/actions - -name: Build and deploy ASP.Net Core app to Azure Web App - pkmds-web - -on: - push: - branches: - - main - workflow_dispatch: - -jobs: - build: - runs-on: windows-latest - - steps: - - uses: actions/checkout@v4 - - - name: Set up .NET Core - uses: actions/setup-dotnet@v1 - with: - dotnet-version: '8.x' - include-prerelease: true - - - name: Build with dotnet - run: dotnet build --configuration Release - - - name: dotnet publish - run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp - - - name: Upload artifact for deployment job - uses: actions/upload-artifact@v3 - with: - name: .net-app - path: ${{env.DOTNET_ROOT}}/myapp - - deploy: - runs-on: windows-latest - needs: build - environment: - name: 'Production' - url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} - - steps: - - name: Download artifact from build job - uses: actions/download-artifact@v3 - with: - name: .net-app - - - name: Deploy to Azure Web App - id: deploy-to-webapp - uses: azure/webapps-deploy@v2 - with: - app-name: 'pkmds-web' - slot-name: 'Production' - publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_10C919944D7C4D1A96A97BE4D2BC955F }} - package: . diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..55ddffb3 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,49 @@ +name: Deploy to GitHub Pages + +# Run workflow on every push to the main branch +on: + push: + branches: [main] + +jobs: + deploy-to-github-pages: + # use ubuntu-latest image to run steps on + runs-on: ubuntu-latest + steps: + # uses GitHub's checkout action to checkout code form the main branch + - uses: actions/checkout@v4.1.1 + + # sets up .NET SDK + - name: Setup .NET Core SDK + uses: actions/setup-dotnet@v4.0.0 + with: + dotnet-version: "8.x" + + - name: Install WASM workload + run: dotnet workload install wasm-tools + + - name: Test + run: dotnet test + + # publishes Blazor project to the release-folder + - name: Publish .NET Core Project + run: dotnet publish PKMDS-Blazor/Wasm/PKMDS-Blazor.Wasm.csproj -c Release -o release --nologo + + # changes the base-tag in index.html from '/' to 'PKMDS-Blazor' to match GitHub Pages repository subdirectory + - name: Change base-tag in index.html from / to PKMDS-Blazor + run: sed -i 's///g' release/wwwroot/index.html + + # copy index.html to 404.html to serve the same file when a file is not found + - name: copy index.html to 404.html + run: cp release/wwwroot/index.html release/wwwroot/404.html + + # add .nojekyll file to tell GitHub pages to not treat this as a Jekyll project. (Allow files and folders starting with an underscore) + - name: Add .nojekyll file + run: touch release/wwwroot/.nojekyll + + - name: Commit wwwroot to GitHub Pages + uses: JamesIves/github-pages-deploy-action@v4.5.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} + branch: gh-pages + folder: release/wwwroot diff --git a/Pkmds.Web/App.razor b/Pkmds.Web/App.razor new file mode 100644 index 00000000..5b725949 --- /dev/null +++ b/Pkmds.Web/App.razor @@ -0,0 +1,16 @@ + + + + + + + Not found + +

+ Sorry, there's nothing at this address. +

+
+
+
diff --git a/Pkmds.Web/Components/App.razor b/Pkmds.Web/Components/App.razor deleted file mode 100644 index d1c9e6ec..00000000 --- a/Pkmds.Web/Components/App.razor +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - -@code { - IComponentRenderMode globalRenderMode = InteractiveServer; -} diff --git a/Pkmds.Web/Components/Pages/Error.razor b/Pkmds.Web/Components/Pages/Error.razor deleted file mode 100644 index e13955e0..00000000 --- a/Pkmds.Web/Components/Pages/Error.razor +++ /dev/null @@ -1,32 +0,0 @@ -@page "/Error" - - - Error - - -

- Error. -

-

- An error occurred while processing your request. -

- -@if (ShowRequestId) -{ -

- Request ID: @RequestId -

-} - -

- Development Mode -

-

- Swapping to Development environment will display more detailed information about the error that occurred. -

-

- The Development environment shouldn't be enabled for deployed applications. - It can result in displaying sensitive information from exceptions to end users. - For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development - and restarting the app. -

diff --git a/Pkmds.Web/Components/Pages/Error.razor.cs b/Pkmds.Web/Components/Pages/Error.razor.cs deleted file mode 100644 index 26c3ed1e..00000000 --- a/Pkmds.Web/Components/Pages/Error.razor.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Pkmds.Web.Components.Pages; - -public partial class Error -{ - [CascadingParameter] - private HttpContext? HttpContext { get; set; } - - private string? RequestId { get; set; } - - private bool ShowRequestId => !string.IsNullOrEmpty(RequestId); - - protected override void OnInitialized() => - RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; -} diff --git a/Pkmds.Web/Components/Routes.razor b/Pkmds.Web/Components/Routes.razor deleted file mode 100644 index 472e0fab..00000000 --- a/Pkmds.Web/Components/Routes.razor +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - diff --git a/Pkmds.Web/GlobalUsings.cs b/Pkmds.Web/GlobalUsings.cs index ccb488ed..aee4ad22 100644 --- a/Pkmds.Web/GlobalUsings.cs +++ b/Pkmds.Web/GlobalUsings.cs @@ -1,9 +1,10 @@ -global using System.Diagnostics; -global using System.Reflection; +global using System.Reflection; global using System.Text; global using KristofferStrube.Blazor.FileSystemAccess; global using Microsoft.AspNetCore.Components; global using Microsoft.AspNetCore.Components.Forms; +global using Microsoft.AspNetCore.Components.Web; +global using Microsoft.AspNetCore.Components.WebAssembly.Hosting; global using Microsoft.JSInterop; global using MudBlazor; global using MudBlazor.Services; diff --git a/Pkmds.Web/Components/Layout/MainLayout.razor b/Pkmds.Web/Layout/MainLayout.razor similarity index 100% rename from Pkmds.Web/Components/Layout/MainLayout.razor rename to Pkmds.Web/Layout/MainLayout.razor diff --git a/Pkmds.Web/Components/Layout/MainLayout.razor.cs b/Pkmds.Web/Layout/MainLayout.razor.cs similarity index 99% rename from Pkmds.Web/Components/Layout/MainLayout.razor.cs rename to Pkmds.Web/Layout/MainLayout.razor.cs index 4f6a57fc..3ff716d5 100644 --- a/Pkmds.Web/Components/Layout/MainLayout.razor.cs +++ b/Pkmds.Web/Layout/MainLayout.razor.cs @@ -1,4 +1,4 @@ -namespace Pkmds.Web.Components.Layout; +namespace Pkmds.Web.Layout; public partial class MainLayout { diff --git a/Pkmds.Web/Components/Layout/MainLayout.razor.css b/Pkmds.Web/Layout/MainLayout.razor.css similarity index 100% rename from Pkmds.Web/Components/Layout/MainLayout.razor.css rename to Pkmds.Web/Layout/MainLayout.razor.css diff --git a/Pkmds.Web/Components/Pages/Home.razor b/Pkmds.Web/Pages/Home.razor similarity index 100% rename from Pkmds.Web/Components/Pages/Home.razor rename to Pkmds.Web/Pages/Home.razor diff --git a/Pkmds.Web/Components/Pages/Home.razor.cs b/Pkmds.Web/Pages/Home.razor.cs similarity index 85% rename from Pkmds.Web/Components/Pages/Home.razor.cs rename to Pkmds.Web/Pages/Home.razor.cs index df11d732..f6791827 100644 --- a/Pkmds.Web/Components/Pages/Home.razor.cs +++ b/Pkmds.Web/Pages/Home.razor.cs @@ -1,4 +1,4 @@ -namespace Pkmds.Web.Components.Pages; +namespace Pkmds.Web.Pages; public partial class Home : IDisposable { diff --git a/Pkmds.Web/Pkmds.Web.csproj b/Pkmds.Web/Pkmds.Web.csproj index 24e4e445..36ddb383 100644 --- a/Pkmds.Web/Pkmds.Web.csproj +++ b/Pkmds.Web/Pkmds.Web.csproj @@ -1,15 +1,28 @@ - + net8.0 enable enable + service-worker-assets.js + + + + + + + + + + + + diff --git a/Pkmds.Web/Program.cs b/Pkmds.Web/Program.cs index 60cd0d92..98031969 100644 --- a/Pkmds.Web/Program.cs +++ b/Pkmds.Web/Program.cs @@ -1,9 +1,9 @@ -var builder = WebApplication.CreateBuilder(args); -var services = builder.Services; +var builder = WebAssemblyHostBuilder.CreateDefault(args); -services - .AddRazorComponents() - .AddInteractiveServerComponents(); +builder.RootComponents.Add("#app"); +builder.RootComponents.Add("head::after"); + +var services = builder.Services; services .AddMudServices() @@ -14,20 +14,4 @@ var app = builder.Build(); -if (!app.Environment.IsDevelopment()) -{ - app.UseExceptionHandler("/Error", createScopeForErrors: true); - app.UseHsts(); -} - -app.UseHttpsRedirection(); - -app - .UseStaticFiles() - .UseAntiforgery(); - -app - .MapRazorComponents() - .AddInteractiveServerRenderMode(); - -app.Run(); +await app.RunAsync(); diff --git a/Pkmds.Web/Properties/launchSettings.json b/Pkmds.Web/Properties/launchSettings.json index 48a0cbd9..6cb158e6 100644 --- a/Pkmds.Web/Properties/launchSettings.json +++ b/Pkmds.Web/Properties/launchSettings.json @@ -4,8 +4,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:43934", - "sslPort": 44339 + "applicationUrl": "http://localhost:64000", + "sslPort": 44359 } }, "profiles": { @@ -13,7 +13,8 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:5266", + "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", + "applicationUrl": "http://localhost:5079", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -22,7 +23,8 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:7182;http://localhost:5266", + "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", + "applicationUrl": "https://localhost:7051;http://localhost:5079", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -30,6 +32,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, + "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/Pkmds.Web/_Imports.razor b/Pkmds.Web/_Imports.razor index 2f6e9ed8..5c764d60 100644 --- a/Pkmds.Web/_Imports.razor +++ b/Pkmds.Web/_Imports.razor @@ -12,6 +12,7 @@ @using Pkmds.Web.Components @using Pkmds.Web.Components.EditForms @using Pkmds.Web.Components.EditForms.Tabs +@using Pkmds.Web.Layout @using Pkmds.Web.Services @using PKHeX.Core @using System.Diagnostics diff --git a/Pkmds.Web/appsettings.Development.json b/Pkmds.Web/appsettings.Development.json deleted file mode 100644 index 0c208ae9..00000000 --- a/Pkmds.Web/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/Pkmds.Web/appsettings.json b/Pkmds.Web/appsettings.json deleted file mode 100644 index 10f68b8c..00000000 --- a/Pkmds.Web/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -} diff --git a/Pkmds.Web/wwwroot/app.css b/Pkmds.Web/wwwroot/app.css deleted file mode 100644 index 40ce010d..00000000 --- a/Pkmds.Web/wwwroot/app.css +++ /dev/null @@ -1,24 +0,0 @@ -pkm-sprite { - max-width: 68px; - max-height: 56px; - align-self: center; -} - -.slot-fill :hover { - transform-origin: center; - animation: bounce 500ms linear infinite; -} - -.party-grid { - width: 462px; /* (68 + 4 + 5) * 30 */ -} - -@keyframes bounce { - 0%, 100% { - transform: translateY(0); - } - - 50% { - transform: translateY(-5px); - } -} diff --git a/Pkmds.Web/wwwroot/css/app.css b/Pkmds.Web/wwwroot/css/app.css new file mode 100644 index 00000000..9a118ad0 --- /dev/null +++ b/Pkmds.Web/wwwroot/css/app.css @@ -0,0 +1,57 @@ +pkm-sprite { + max-width: 68px; + max-height: 56px; + align-self: center; +} + +.slot-fill :hover { + transform-origin: center; + animation: bounce 500ms linear infinite; +} + +.party-grid { + width: 462px; /* (68 + 4 + 5) * 30 */ +} + +@keyframes bounce { + 0%, 100% { + transform: translateY(0); + } + + 50% { + transform: translateY(-5px); + } +} + +.loading-progress { + position: relative; + display: block; + width: 8rem; + height: 8rem; + margin: 20vh auto 1rem auto; +} + + .loading-progress circle { + fill: none; + stroke: #e0e0e0; + stroke-width: 0.6rem; + transform-origin: 50% 50%; + transform: rotate(-90deg); + } + + .loading-progress circle:last-child { + stroke: #1b6ec2; + stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * 0.8), 500%; + transition: stroke-dasharray 0.05s ease-in-out; + } + +.loading-progress-text { + position: absolute; + text-align: center; + font-weight: bold; + inset: calc(20vh + 3.25rem) 0 auto 0.2rem; +} + + .loading-progress-text:after { + content: var(--blazor-load-percentage-text, "Loading"); + } diff --git a/Pkmds.Web/wwwroot/icon-192.png b/Pkmds.Web/wwwroot/icon-192.png new file mode 100644 index 0000000000000000000000000000000000000000..166f56da7612ea74df6a297154c8d281a4f28a14 GIT binary patch literal 2626 zcmV-I3cdA-P)v0A9xRwxP|bki~~&uFk>U z#P+PQh zyZ;-jwXKqnKbb6)@RaxQz@vm={%t~VbaZrdbaZrdbaeEeXj>~BG?&`J0XrqR#sSlO zg~N5iUk*15JibvlR1f^^1czzNKWvoJtc!Sj*G37QXbZ8LeD{Fzxgdv#Q{x}ytfZ5q z+^k#NaEp>zX_8~aSaZ`O%B9C&YLHb(mNtgGD&Kezd5S@&C=n~Uy1NWHM`t07VQP^MopUXki{2^#ryd94>UJMYW|(#4qV`kb7eD)Q=~NN zaVIRi@|TJ!Rni8J=5DOutQ#bEyMVr8*;HU|)MEKmVC+IOiDi9y)vz=rdtAUHW$yjt zrj3B7v(>exU=IrzC<+?AE=2vI;%fafM}#ShGDZx=0Nus5QHKdyb9pw&4>4XCpa-o?P(Gnco1CGX|U> z$f+_tA3+V~<{MU^A%eP!8R*-sD9y<>Jc7A(;aC5hVbs;kX9&Sa$JMG!W_BLFQa*hM zri__C@0i0U1X#?)Y=)>JpvTnY6^s;fu#I}K9u>OldV}m!Ch`d1Vs@v9 zb}w(!TvOmSzmMBa9gYvD4xocL2r0ds6%Hs>Z& z#7#o9PGHDmfG%JQq`O5~dt|MAQN@2wyJw_@``7Giyy(yyk(m8U*kk5$X1^;3$a3}N^Lp6hE5!#8l z#~NYHmKAs6IAe&A;bvM8OochRmXN>`D`{N$%#dZCRxp4-dJ?*3P}}T`tYa3?zz5BA zTu7uE#GsDpZ$~j9q=Zq!LYjLbZPXFILZK4?S)C-zE1(dC2d<7nO4-nSCbV#9E|E1MM|V<9>i4h?WX*r*ul1 z5#k6;po8z=fdMiVVz*h+iaTlz#WOYmU^SX5#97H~B32s-#4wk<1NTN#g?LrYieCu> zF7pbOLR;q2D#Q`^t%QcY06*X-jM+ei7%ZuanUTH#9Y%FBi*Z#22({_}3^=BboIsbg zR0#jJ>9QR8SnmtSS6x($?$}6$x+q)697#m${Z@G6Ujf=6iO^S}7P`q8DkH!IHd4lB zDzwxt3BHsPAcXFFY^Fj}(073>NL_$A%v2sUW(CRutd%{G`5ow?L`XYSO*Qu?x+Gzv zBtR}Y6`XF4xX7)Z04D+fH;TMapdQFFameUuHL34NN)r@aF4RO%x&NApeWGtr#mG~M z6sEIZS;Uj1HB1*0hh=O@0q1=Ia@L>-tETu-3n(op+97E z#&~2xggrl(LA|giII;RwBlX2^Q`B{_t}gxNL;iB11gEPC>v` zb4SJ;;BFOB!{chn>?cCeGDKuqI0+!skyWTn*k!WiPNBf=8rn;@y%( znhq%8fj2eAe?`A5mP;TE&iLEmQ^xV%-kmC-8mWao&EUK_^=GW-Y3z ksi~={si~={skwfB0gq6itke#r1ONa407*qoM6N<$g11Kq@c;k- literal 0 HcmV?d00001 diff --git a/Pkmds.Web/wwwroot/index.html b/Pkmds.Web/wwwroot/index.html new file mode 100644 index 00000000..13703bb6 --- /dev/null +++ b/Pkmds.Web/wwwroot/index.html @@ -0,0 +1,38 @@ + + + + + + + PKMDS Save Editor + + + + + + + + + + + + +
+ + + + +
+
+ +
+ An unhandled error has occurred. + Reload + 🗙 +
+ + + + + + diff --git a/Pkmds.Web/wwwroot/manifest.webmanifest b/Pkmds.Web/wwwroot/manifest.webmanifest new file mode 100644 index 00000000..23a7ad3a --- /dev/null +++ b/Pkmds.Web/wwwroot/manifest.webmanifest @@ -0,0 +1,22 @@ +{ + "name": "Pkmds.Web", + "short_name": "Pkmds.Web", + "id": "./", + "start_url": "./", + "display": "standalone", + "background_color": "#ffffff", + "theme_color": "#03173d", + "prefer_related_applications": false, + "icons": [ + { + "src": "icon-512.png", + "type": "image/png", + "sizes": "512x512" + }, + { + "src": "icon-192.png", + "type": "image/png", + "sizes": "192x192" + } + ] +} diff --git a/Pkmds.Web/wwwroot/service-worker.js b/Pkmds.Web/wwwroot/service-worker.js new file mode 100644 index 00000000..fe614dae --- /dev/null +++ b/Pkmds.Web/wwwroot/service-worker.js @@ -0,0 +1,4 @@ +// 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', () => { }); diff --git a/Pkmds.Web/wwwroot/service-worker.published.js b/Pkmds.Web/wwwroot/service-worker.published.js new file mode 100644 index 00000000..fc5ce381 --- /dev/null +++ b/Pkmds.Web/wwwroot/service-worker.published.js @@ -0,0 +1,55 @@ +// 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$/, /\.blat$/, /\.dat$/]; +const offlineAssetsExclude = [/^service-worker\.js$/]; + +// Replace with your base path if you are hosting on a subfolder. Ensure there is a trailing '/'. +const base = "/"; +const baseUrl = new URL(base, self.origin); +const manifestUrlList = self.assetsManifest.assets.map(asset => new URL(asset.url, baseUrl).href); + +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, cache: 'no-cache' })); + 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, + // unless that request is for an offline resource. + // If you need some URLs to be server-rendered, edit the following check to exclude those URLs + const shouldServeIndexHtml = event.request.mode === 'navigate' + && !manifestUrlList.some(url => url === event.request.url); + + const request = shouldServeIndexHtml ? 'index.html' : event.request; + const cache = await caches.open(cacheName); + cachedResponse = await cache.match(request); + } + + return cachedResponse || fetch(event.request); +} diff --git a/Pkmds.sln b/Pkmds.sln index 7268bf0f..83b1ed8a 100644 --- a/Pkmds.sln +++ b/Pkmds.sln @@ -9,7 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution global.json = global.json EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pkmds.Web", "Pkmds.Web\Pkmds.Web.csproj", "{3131089A-973A-48AD-975F-7A024E4F9A98}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pkmds.Web", "Pkmds.Web\Pkmds.Web.csproj", "{F457F3DE-E45B-48D2-AFC9-BF9DAD07DE90}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +17,10 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {3131089A-973A-48AD-975F-7A024E4F9A98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3131089A-973A-48AD-975F-7A024E4F9A98}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3131089A-973A-48AD-975F-7A024E4F9A98}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3131089A-973A-48AD-975F-7A024E4F9A98}.Release|Any CPU.Build.0 = Release|Any CPU + {F457F3DE-E45B-48D2-AFC9-BF9DAD07DE90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F457F3DE-E45B-48D2-AFC9-BF9DAD07DE90}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F457F3DE-E45B-48D2-AFC9-BF9DAD07DE90}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F457F3DE-E45B-48D2-AFC9-BF9DAD07DE90}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE