Skip to content

Commit

Permalink
Merge pull request #4 from SQL-MisterMagoo/disable-localhost
Browse files Browse the repository at this point in the history
Added ServiceWorkerIgnoreHosts - for not registering service worker...
  • Loading branch information
SQL-MisterMagoo authored Nov 11, 2019
2 parents 4aaa76e + 578699c commit fbc0e57
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Blazor.PWA.MSBuild.Tasks/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionPrefix>1.0.1</VersionPrefix>
<VersionSuffix>beta$([System.DateTime]::Now.ToString("yyyyMMdd-HHmmss"))</VersionSuffix>
<VersionSuffix Condition="'$(Configuration)' == 'Release'"></VersionSuffix>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
const staticCachePrefix = '$(ServiceWorkerCacheName)-v'%3B;
const updateAlertMessage = '$(ServiceWorkerUpdateAlertText)'%3B;
</ServiceWorkerRegisterConstants>
<ServiceWorkerRegisterConstants Condition="'$(ServiceWorkerIgnoreHosts)' != ''">
$(ServiceWorkerRegisterConstants);
const ignoreHosts = [$(ServiceWorkerIgnoreHosts)]%3B;
</ServiceWorkerRegisterConstants>
<ServiceWorkerRegisterConstants Condition="'$(ServiceWorkerRegisterInstallableType)' == 'installable-blazor'">
$(ServiceWorkerRegisterConstants);
const blazorAssembly = '$(ServiceWorkerBlazorAssembly)'%3B;
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 11/11/2019

- Added new **Property** **`ServiceWorkerIgnoreHosts`** - used to prevent service worker installation on specific hosts e.g. localhost

#### 16/10/2019

- 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.
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,30 @@ The `Name` can be anything you like, but is required by `MSBuild`.

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

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

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.

Add it to you `csproj` file under `PropertyGroup` and list the hosts you want to exclude.

``` XML
<PropertyGroup>
<ServiceWorkerIgnoreHosts>'localhost','127.0.0.1','::1','KarensPC'</ServiceWorkerIgnoreHosts>
</PropertyGroup>
```

The service worker will not register itself when the `hostname` matches anything in the list.

*Note: the single quotes around each hostname are required for now*

## Roadmap

- [ ] 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)
- [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
- [ ] 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
- [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
- [ ] Document all of the configuration Properties (they all have comments in the code - so you are able to understand their purpose without documentation...)
- [ ] Bug fixes
- [x] Allow hosts exclusions - so working on localhost can avoid PWA caching

## Contribute

Expand Down

0 comments on commit fbc0e57

Please sign in to comment.