Skip to content

Commit 578699c

Browse files
Added ServiceWorkerIgnoreHosts - for not registering service worker on specific hosts.
Bumped Version to 1.0.1 Updated changelog and readme
1 parent 4aaa76e commit 578699c

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

Blazor.PWA.MSBuild.Tasks/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>1.0.0</VersionPrefix>
3+
<VersionPrefix>1.0.1</VersionPrefix>
44
<VersionSuffix>beta$([System.DateTime]::Now.ToString("yyyyMMdd-HHmmss"))</VersionSuffix>
55
<VersionSuffix Condition="'$(Configuration)' == 'Release'"></VersionSuffix>
66
</PropertyGroup>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
window.updateAvailable = new Promise(function (resolve, reject) {
2+
var { hostname } = window.location;
3+
if (typeof ignoreHosts !== 'undefined') {
4+
if (ignoreHosts.includes(hostname)) {
5+
return;
6+
}
7+
}
28
if ('serviceWorker' in navigator) {
39
navigator.serviceWorker.register(serviceWorkerFileName)
410
.then(function (registration) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
const staticCachePrefix = '$(ServiceWorkerCacheName)-v'%3B;
4040
const updateAlertMessage = '$(ServiceWorkerUpdateAlertText)'%3B;
4141
</ServiceWorkerRegisterConstants>
42+
<ServiceWorkerRegisterConstants Condition="'$(ServiceWorkerIgnoreHosts)' != ''">
43+
$(ServiceWorkerRegisterConstants);
44+
const ignoreHosts = [$(ServiceWorkerIgnoreHosts)]%3B;
45+
</ServiceWorkerRegisterConstants>
4246
<ServiceWorkerRegisterConstants Condition="'$(ServiceWorkerRegisterInstallableType)' == 'installable-blazor'">
4347
$(ServiceWorkerRegisterConstants);
4448
const blazorAssembly = '$(ServiceWorkerBlazorAssembly)'%3B;

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#### 11/11/2019
2+
3+
- Added new **Property** **`ServiceWorkerIgnoreHosts`** - used to prevent service worker installation on specific hosts e.g. localhost
4+
15
#### 16/10/2019
26

37
- Added new **Property** **`BlazorProjectType`** - used to select the type of Blazor project (SSB/CSB). This property will auto-configure but can be overridden in your csproj file in cases where auto-configure is wrong.

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,30 @@ The `Name` can be anything you like, but is required by `MSBuild`.
155155

156156
So, this example is excluding all files under the `_redist` folder from the Service Worker Cache.
157157

158+
### Prevent PWA installation / caching on localhost (or any host)
158159

160+
If you want to exclude *localhost* (or any host) so that the developer inner loop is not affected, you can now use the **`ServiceWorkerIgnoreHosts`** property.
161+
162+
Add it to you `csproj` file under `PropertyGroup` and list the hosts you want to exclude.
163+
164+
``` XML
165+
<PropertyGroup>
166+
<ServiceWorkerIgnoreHosts>'localhost','127.0.0.1','::1','KarensPC'</ServiceWorkerIgnoreHosts>
167+
</PropertyGroup>
168+
```
169+
170+
The service worker will not register itself when the `hostname` matches anything in the list.
171+
172+
*Note: the single quotes around each hostname are required for now*
159173

160174
## Roadmap
161175

162176
- [ ] At the moment, there is only one choice for caching strategy - Cache First/Network Fallback - I will add more (https://developers.google.com/web/ilt/pwa/introduction-to-progressive-web-app-architectures#caching_strategies_supported_by_sw-toolbox)
163177
- [x] The current method for alerting the user that the app is installable is semi-hard coded (you can adjust it manually after generation) - this will change to allow hooks/callbacks into Blazor via project properties
164-
- [ ] The current method for alerting the user when an update is available is semi-hard coded (you can adjust it manually after generation) - this will change to allow hooks/callbacks into Blazor via project properties
178+
- [x] The current method for alerting the user when an update is available is semi-hard coded (you can adjust it manually after generation) - this will change to allow hooks/callbacks into Blazor via project properties
165179
- [ ] Document all of the configuration Properties (they all have comments in the code - so you are able to understand their purpose without documentation...)
166180
- [ ] Bug fixes
181+
- [x] Allow hosts exclusions - so working on localhost can avoid PWA caching
167182

168183
## Contribute
169184

0 commit comments

Comments
 (0)