Skip to content

Commit 431069d

Browse files
Consider this alpha - SSB installable PWA
1 parent babddbf commit 431069d

File tree

6 files changed

+41
-14
lines changed

6 files changed

+41
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ I will add more network caching strategies, but for now it has just one - cache
6969
</ItemGroup>
7070

7171
<ItemGroup>
72+
<None Remove="Templates\ServiceWorker\sw_fetch-none.template.js" />
7273
<None Remove="Templates\ServiceWorker\sw_register-beforeinstallprompt.template.js" />
7374
<None Remove="Templates\ServiceWorker\sw_register-installable-banner.template.js" />
7475
<None Remove="Templates\ServiceWorker\sw_register-installable-blazor.template.js" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
self.addEventListener(networkFetchEvent, event => {
2+
return fetch(event.request);
3+
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@
7171

7272
<!-- Add manifest to index.html -->
7373
<PropertyGroup>
74-
<IndexFile>$(WWWRoot)$(ServiceWorkerIndexUrl)</IndexFile>
74+
<IndexFile Condition="'$(BlazorProjectType)' == 'CSB'">$(WWWRoot)$(ProjectIndexPage)</IndexFile>
75+
<IndexFile Condition="'$(BlazorProjectType)' == 'SSB'">$(ProjectHomePage)</IndexFile>
7576
<IndexLines>$([System.IO.File]::ReadAllText($(IndexFile)))</IndexLines>
7677
</PropertyGroup>
77-
78+
<Message Importance="high" Text="Blazor Project Type detected as $(BlazorProjectType)"/>
7879
<Message Importance="high" Text="Adding manifest to $(IndexFile)"
7980
Condition="'$(IndexLines.Contains(rel=&quot;manifest&quot;))'=='false'"/>
8081

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,26 @@
4848
</ServiceWorkerPreCacheGeneratedFiles>
4949

5050
<!-- Setup the declarations for the Service Worker -->
51-
<ServiceWorkerConstants Condition="'$(ServiceWorkerConstants)' == ''">
51+
<ServiceWorkerConstants Condition="'$(ServiceWorkerConstants)' == '' AND '$(BlazorProjectType)' == 'CSB'">
5252
const baseURL = '$(ServiceWorkerBaseURL)'%3B;
53-
const indexURL = '$(ServiceWorkerBaseUrl)$(ServiceWorkerIndexURL)'%3B;
53+
const indexURL = '$(ServiceWorkerBaseUrl)$(ProjectIndexPage)'%3B;
5454
const networkFetchEvent = '$(ServiceWorkerNetworkFetchEvent)'%3B;
5555
const swInstallEvent = '$(ServiceWorkerInstallEvent)'%3B;
5656
const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B;
5757
const swActivateEvent = '$(ServiceWorkerActivateEvent)'%3B;
5858
const staticCachePrefix = '$(ServiceWorkerCacheName)-v'%3B;
5959
const staticCacheName = '$(ServiceWorkerCacheName)-v$(ServiceWorkerCacheVersion)'%3B;
6060
</ServiceWorkerConstants>
61+
62+
<!-- Setup the declarations for the Service Worker -->
63+
<ServiceWorkerConstants Condition="'$(ServiceWorkerConstants)' == '' AND '$(BlazorProjectType)' == 'SSB'">
64+
const baseURL = '$(ServiceWorkerBaseURL)'%3B;
65+
const indexURL = '$(ServiceWorkerBaseUrl)$(ProjectHomePage)'%3B;
66+
const networkFetchEvent = '$(ServiceWorkerNetworkFetchEvent)'%3B;
67+
const swInstallEvent = '$(ServiceWorkerInstallEvent)'%3B;
68+
const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B;
69+
const swActivateEvent = '$(ServiceWorkerActivateEvent)'%3B;
70+
</ServiceWorkerConstants>
6171
</PropertyGroup>
6272

6373
<!-- The "Templates" properties are used to define where to find templates for the service worker sections -->
@@ -69,7 +79,7 @@
6979
<!-- The file that contains template code for the network fetch event in the service worker -->
7080
<ServiceWorkerTemplateNetwork Condition="'$(ServiceWorkerTemplateNetwork)' == ''">$(ServiceWorkerTemplatePath)sw_$(ServiceWorkerNetworkFetchEvent)-$(ServiceWorkerPattern).template.js</ServiceWorkerTemplateNetwork>
7181
</PropertyGroup>
72-
<Message Importance="high" Text="Exclude:;@(ServiceWorkerPreCacheExcludeFiles);@(ServiceWorkerPreCacheExcludeReleaseFiles);"/>
82+
7383
<ItemGroup>
7484
<!-- Build a list of files to pre-cache -->
7585
<DistFiles Include="$(ServiceWorkerPreCacheIncludeFiles);" />
@@ -92,10 +102,12 @@
92102
<WriteLinesToFile File="$(WWWRoot)$(ServiceWorkerFileName)"
93103
Overwrite="true"
94104
Lines="$(ServiceWorkerConstants)" />
95-
<WriteLinesToFile File="$(WWWRoot)$(ServiceWorkerFileName)"
105+
<WriteLinesToFile Condition="'$(BlazorProjectType)' == 'CSB'"
106+
File="$(WWWRoot)$(ServiceWorkerFileName)"
96107
Overwrite="false"
97108
Lines="const requiredFiles = [;@(DistFiles -> '&quot;/%(RecursiveDir)%(FileName)%(Extension)&quot;'->Replace('\','/'),',%0D%0A');]%3B;" />
98-
<WriteLinesToFile File="$(WWWRoot)$(ServiceWorkerFileName)"
109+
<WriteLinesToFile Condition="'$(BlazorProjectType)' == 'CSB'"
110+
File="$(WWWRoot)$(ServiceWorkerFileName)"
99111
Overwrite="false"
100112
Lines="@(InstallTemplateLines);" />
101113
<WriteLinesToFile File="$(WWWRoot)$(ServiceWorkerFileName)"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
<Target Name="_ServiceWorkerUpdateIndex">
9494
<!-- Add ServiceWorkerRegister.js to index.html -->
9595
<PropertyGroup>
96-
<IndexFile>$(WWWRoot)$(ServiceWorkerIndexUrl)</IndexFile>
96+
<IndexFile Condition="'$(BlazorProjectType)' == 'CSB'">$(WWWRoot)$(ProjectIndexPage)</IndexFile>
97+
<IndexFile Condition="'$(BlazorProjectType)' == 'SSB'">$(ProjectHomePage)</IndexFile>
9798
<IndexLines>$([System.IO.File]::ReadAllText($(IndexFile)))</IndexLines>
9899
</PropertyGroup>
99100

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44
<Import Project="..\build\BlazorPWA.MSBuild.ServiceWorkerRegister.targets" />
55
<Import Project="..\build\BlazorPWA.MSBuild.Manifest.targets" />
66

7+
<PropertyGroup Label="ProjectTypes">
8+
<!-- Project type -->
9+
<BlazorProjectType Condition="'$(BlazorProjectType)' == '' AND '$(TargetFramework)' == 'netcoreapp3.0'">SSB</BlazorProjectType>
10+
<BlazorProjectType Condition="'$(BlazorProjectType)' == '' AND '$(TargetFramework)' == 'netstandard2.0'">CSB</BlazorProjectType>
11+
</PropertyGroup>
12+
713
<PropertyGroup Label="ServiceWorker">
814
<!-- The service worker file names to generate -->
915
<ServiceWorkerFileName Condition="'$(ServiceWorkerFileName)' == ''">ServiceWorker.js</ServiceWorkerFileName>
1016
<ServiceWorkerRegisterFileName Condition="'$(ServiceWorkerRegisterFileName)' == ''">ServiceWorkerRegister.js</ServiceWorkerRegisterFileName>
1117

1218
<!-- The PWA strategy to implement in the service worker -->
13-
<!-- Currently only supports offline-first -->
19+
<!-- Currently only supports offline-first or none -->
20+
<ServiceWorkerPattern Condition="'$(BlazorProjectType)' == 'CSB' AND '$(ServiceWorkerPattern)' == ''">offline-first</ServiceWorkerPattern>
21+
<ServiceWorkerPattern Condition="'$(BlazorProjectType)' == 'SSB' AND '$(ServiceWorkerPattern)' == ''">none</ServiceWorkerPattern>
1422
<ServiceWorkerPattern Condition="'$(ServiceWorkerPattern)' == ''">offline-first</ServiceWorkerPattern>
15-
<!-- Remove this when we implement more patterns -->
16-
<ServiceWorkerPattern>offline-first</ServiceWorkerPattern>
1723

1824
<!-- The Cache name and version together form the name of the application browser cache e.g. blazor-cache-v1 -->
1925
<ServiceWorkerCacheName Condition="'$(ServiceWorkerCacheName)' == ''">blazor-cache</ServiceWorkerCacheName>
@@ -22,8 +28,11 @@
2228
<!-- The "home route" url for the application -->
2329
<ServiceWorkerBaseUrl Condition="'$(ServiceWorkerBaseUrl)' == ''">/</ServiceWorkerBaseUrl>
2430

25-
<!-- The actual home page served up for the base URL - so we have a way to cache it -->
26-
<ServiceWorkerIndexUrl Condition="'$(ServiceWorkerIndexUrl)' == ''">index.html</ServiceWorkerIndexUrl>
31+
<!-- The CSB home page served up for the base URL -->
32+
<ProjectIndexPage Condition="'$(ProjectIndexPage)' == ''">index.html</ProjectIndexPage>
33+
34+
<!-- The SSB home page served up for the base URL -->
35+
<ProjectHomePage Condition="'$(ProjectHomePage)' == ''">Pages/_Host.cshtml</ProjectHomePage>
2736

2837
</PropertyGroup>
2938

@@ -54,7 +63,7 @@
5463
BeforeTargets="$(CreatePWABeforeTargets)"
5564
Condition="'$(ServiceWorkerPattern)'!='' AND '$(ServiceWorkerCacheName)'!='' AND '$(ServiceWorkerCacheVersion)'!='' AND (!Exists('$(WWWRoot)$(ServiceWorkerFileName)') OR '$(ServiceWorkerForce)'=='true')">
5665

57-
<Message Importance="high" Text="Building Your PWA: Type: $(ServiceWorkerPattern) CacheName: $(ServiceWorkerCacheName) CacheVersion: $(ServiceWorkerCacheVersion)"/>
66+
<Message Importance="high" Text="Building Your $(BlazorProjectType) PWA: Type: $(ServiceWorkerPattern) CacheName: $(ServiceWorkerCacheName) CacheVersion: $(ServiceWorkerCacheVersion)"/>
5867
<CallTarget Targets="BuildServiceWorkerInstaller"/>
5968
<CallTarget Targets="BuildServiceWorkerManifest"/>
6069
<CallTarget Targets="BuildServiceWorker"/>

0 commit comments

Comments
 (0)