Skip to content

Commit

Permalink
#179 - Integration of BlazorPWA.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Dec 25, 2019
1 parent 82d9df5 commit 9724e62
Show file tree
Hide file tree
Showing 5 changed files with 1,853 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/Money.UI.Blazor/Money.UI.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,31 @@
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
</PropertyGroup>

<PropertyGroup>
<ManifestShortName>Money</ManifestShortName>
<ManifestLongName>Money by Neptuo</ManifestLongName>

<ServiceWorkerFileName>service-worker.js</ServiceWorkerFileName>
<ServiceWorkerRegisterFileName>service-worker-register.js</ServiceWorkerRegisterFileName>
<ServiceWorkerForce>true</ServiceWorkerForce>
<ServiceWorkerCacheVersion Condition="'$(Configuration)' == 'Debug'">0.0.$([System.DateTime]::Now.Ticks)</ServiceWorkerCacheVersion>
<ServiceWorkerCacheVersion Condition="'$(Configuration)' == 'Release'">$(Version)</ServiceWorkerCacheVersion>
<ServiceWorkerRegisterInstallableType>installable-blazor</ServiceWorkerRegisterInstallableType>
<ServiceWorkerBlazorInstallMethod>Pwa.Installable</ServiceWorkerBlazorInstallMethod>
<ServiceWorkerRegisterUpdateType>update-blazor</ServiceWorkerRegisterUpdateType>
</PropertyGroup>

<Target Name="PWACustomise" BeforeTargets="CreatePWA">
<PropertyGroup>
<ServiceWorkerPreCacheExcludeFiles>
$(WWWRoot)**\*.scss;
</ServiceWorkerPreCacheExcludeFiles>
</PropertyGroup>
<Message Importance="high" Text="Removing: $(ServiceWorkerPreCacheExcludeFiles)" />
</Target>

<ItemGroup>
<PackageReference Include="BlazorPWA.MSBuild" Version="1.0.2" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.1.0-preview4.19579.2" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.1.0-preview4.19579.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.1.0-preview4.19579.2" />
Expand All @@ -31,9 +55,7 @@
</ItemGroup>

<ItemGroup>
<Content Update="wwwroot\index.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="wwwroot\index.html" CopyToOutputDirectory="PreserveNewest" />
<Compile Update="**\*.razor.cs" DependentUpon="%(Filename)" />
<Watch Include="**\*.razor" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/Money.UI.Blazor/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Money</title>
<base href="/" />
<link href="manifest.json" rel="manifest" />
<link href="lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="css/site.min.css" rel="stylesheet" />
<link href="lib/fontawesome/css/all.min.css" rel="stylesheet" />
Expand Down Expand Up @@ -37,5 +38,6 @@ <h2>Neptuo</h2>
<script src="lib/signalr/signalr.min.js"></script>
<script src="_framework/blazor.webassembly.js"></script>
<script src="js/site.js"></script>
<script src="/service-worker-register.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions src/Money.UI.Blazor/wwwroot/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"short_name": "Money",
"name": "Money by Neptuo",
"start_url": "/",
"display": "standalone",
"icons": [
{
"src":"/images/icon-w-192x192.png",
"type":"image/png",
"sizes":"192x192"
},
{
"src":"/images/icon-w-512x512.png",
"type":"image/png",
"sizes":"512x512"
}
]
}
62 changes: 62 additions & 0 deletions src/Money.UI.Blazor/wwwroot/service-worker-register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const serviceWorkerFileName = '/service-worker.js';
const swInstalledEvent = 'installed';
const staticCachePrefix = 'blazor-cache-v';
const updateAlertMessage = 'Update available. Reload the page when convenient.';
const blazorAssembly = 'Money.UI.Blazor';
const blazorInstallMethod = 'Pwa.Installable';
window.updateAvailable = new Promise(function (resolve, reject) {
var { hostname } = window.location;
if (typeof ignoreHosts !== 'undefined') {
if (ignoreHosts.includes(hostname)) {
return;
}
}
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(serviceWorkerFileName)
.then(function (registration) {
console.log('Registration successful, scope is:', registration.scope);
registration.onupdatefound = () => {
const installingWorker = registration.installing;
installingWorker.onstatechange = () => {
switch (installingWorker.state) {
case swInstalledEvent:
if (navigator.serviceWorker.controller) {
resolve(true);
} else {
resolve(false);
}
break;
default:
}
};
};
})
.catch(error =>
console.log('Service worker registration failed, error:', error));
}
});
function showAddToHomeScreen() {
DotNet.invokeMethodAsync(blazorAssembly, blazorInstallMethod)
.then(function () { }, function (er) { setTimeout(showAddToHomeScreen, 1000); });
}

window.BlazorPWA = {
installPWA: function () {
if (window.PWADeferredPrompt) {
window.PWADeferredPrompt.prompt();
window.PWADeferredPrompt.userChoice
.then(function (choiceResult) {
window.PWADeferredPrompt = null;
});
}
}
};
window.addEventListener('beforeinstallprompt', function (e) {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
window.PWADeferredPrompt = e;

showAddToHomeScreen();

});
Loading

0 comments on commit 9724e62

Please sign in to comment.