Skip to content

Commit 3cdd855

Browse files
Move update event handler to templated code to prepare for extra templates.
1 parent 5418ece commit 3cdd855

File tree

7 files changed

+33
-6
lines changed

7 files changed

+33
-6
lines changed

Blazor.PWA.MSBuild.Tasks/Blazor.PWA.MSBuild.Tasks.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ I will add more network caching strategies, but for now it has just one - cache
6868

6969
</ItemGroup>
7070

71+
<ItemGroup>
72+
<None Remove="Templates\ServiceWorker\sw_register-update-alert.template.js" />
73+
</ItemGroup>
74+
7175
<ItemGroup>
7276
<Folder Include="Templates\Images\" />
7377
</ItemGroup>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+

2+
window['updateAvailable']
3+
.then(isAvailable => {
4+
if (isAvailable) {
5+
alert(updateAlertMessage);
6+
}
7+
});

Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_register.template.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
console.log('Service worker registration failed, error:', error));
2424
}
2525
});
26-
window['updateAvailable']
27-
.then(isAvailable => {
28-
if (isAvailable) {
29-
alert("Update available. Reload the page when convenient.");
30-
}
31-
});
3226

3327
window.addEventListener('beforeinstallprompt', function (e) {
3428
// Prevent Chrome 67 and earlier from automatically showing the prompt

Blazor.PWA.MSBuild.Tasks/build/BlazorPWA.MSBuild.ServiceWorkerRegister.targets

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,28 @@
2626
<ServiceWorkerRegisterTemplatePath Condition="'$(ServiceWorkerRegisterTemplatePath)' == ''">$(MSBuildThisFileDirectory)..\Templates\ServiceWorker\</ServiceWorkerRegisterTemplatePath>
2727
<!-- The file that contains template code for the service worker register file -->
2828
<ServiceWorkerRegisterTemplate Condition="'$(ServiceWorkerRegisterTemplate)' == ''">$(ServiceWorkerRegisterTemplatePath)sw_register.template.js</ServiceWorkerRegisterTemplate>
29+
<!-- The file that contains template code for the service worker "update available" -->
30+
<ServiceWorkerRegisterUpdateNotificationTemplate Condition="'$(ServiceWorkerRegisterUpdateNotificationTemplate)' == ''">$(ServiceWorkerRegisterTemplatePath)sw_register-update-alert.template.js</ServiceWorkerRegisterUpdateNotificationTemplate>
2931
</PropertyGroup>
3032

3133
<PropertyGroup Label="Implementation">
3234
<!-- event fired by browser when the service worker has installed ** probably never change **-->
3335
<ServiceWorkerInstalledEvent Condition="'$(ServiceWorkerInstalledEvent)'==''">installed</ServiceWorkerInstalledEvent>
36+
<!-- Text to display when an update is available -->
37+
<ServiceWorkerUpdateAlertText Condition="'$(ServiceWorkerUpdateAlertText)'==''">Update available. Reload the page when convenient.</ServiceWorkerUpdateAlertText>
38+
<!-- Update available alert type -->
39+
<ServiceWorkerUpdateNotificationType Condition="'$(ServiceWorkerUpdateNotificationType)'==''">update-alert</ServiceWorkerUpdateNotificationType>
3440
<!-- Setup the declarations for the Service Worker Register -->
3541
<ServiceWorkerRegisterConstants Condition="'$(ServiceWorkerConstants)' == ''">
3642
const serviceWorkerFileName = '$(ServiceWorkerBaseURL)$(ServiceWorkerFileName)'%3B;
3743
const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B;
3844
const staticCachePrefix = '$(ServiceWorkerCacheName)-v'%3B;
45+
const updateAlertMessage = '$(ServiceWorkerUpdateAlertText)'%3B;
3946
</ServiceWorkerRegisterConstants>
4047
</PropertyGroup>
4148
<ItemGroup>
4249
<ServiceWorkerRegisterTemplateLines Include="$([System.IO.File]::ReadAllText($(ServiceWorkerRegisterTemplate)))"/>
50+
<ServiceWorkerRegisterTemplateLines Include="$([System.IO.File]::ReadAllText($(ServiceWorkerRegisterUpdateNotificationTemplate)))"/>
4351
</ItemGroup>
4452
<WriteLinesToFile
4553
File="$(WWWRoot)$(ServiceWorkerRegisterFileName)"

Blazor.PWA.MSBuild.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.PWA.MSBuild.Tasks",
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BCE600C8-35D7-4AA4-9FFB-156E4526097E}"
99
ProjectSection(SolutionItems) = preProject
10+
CHANGELOG.md = CHANGELOG.md
1011
LICENSE.txt = LICENSE.txt
1112
README.md = README.md
1213
EndProjectSection

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#### 09/08/2019
2+
3+
- Added new **Property** **`ServiceWorkerUpdateAlertText`** - used to change the default text in the "Update available alert".
4+
- Added new **Property** **`ServiceWorkerRegisterUpdateNotificationTemplate`** - The name of the template file for the "update available" event.
5+
- Moved the "Updated available alert" to it's own template so we can have alternates
6+
7+
#### 08/08/2019 Initial Release

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ It currently includes a simple banner to notify the user that your application c
1010

1111
This is to be considered a Beta release - I am looking for feedback/issues/requests.
1212

13+
### Browser compatibility
14+
15+
Note that chromium browsers will prompt you to install a PWA on a desktop computer.
16+
Firefox does not currently provide this experience on desktop.
17+
I don't have a Mac, but I believe Safari does not prompt on desktop either.
18+
1319
## Installation
1420

1521
Install the nuget BlazorPWA.MsBuild

0 commit comments

Comments
 (0)