Skip to content

Commit

Permalink
Consider this alpha - SSB installable PWA
Browse files Browse the repository at this point in the history
  • Loading branch information
SQL-MisterMagoo committed Oct 14, 2019
1 parent babddbf commit 431069d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions Blazor.PWA.MSBuild.Tasks/Blazor.PWA.MSBuild.Tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ I will add more network caching strategies, but for now it has just one - cache
</ItemGroup>

<ItemGroup>
<None Remove="Templates\ServiceWorker\sw_fetch-none.template.js" />
<None Remove="Templates\ServiceWorker\sw_register-beforeinstallprompt.template.js" />
<None Remove="Templates\ServiceWorker\sw_register-installable-banner.template.js" />
<None Remove="Templates\ServiceWorker\sw_register-installable-blazor.template.js" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
self.addEventListener(networkFetchEvent, event => {
return fetch(event.request);
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@

<!-- Add manifest to index.html -->
<PropertyGroup>
<IndexFile>$(WWWRoot)$(ServiceWorkerIndexUrl)</IndexFile>
<IndexFile Condition="'$(BlazorProjectType)' == 'CSB'">$(WWWRoot)$(ProjectIndexPage)</IndexFile>
<IndexFile Condition="'$(BlazorProjectType)' == 'SSB'">$(ProjectHomePage)</IndexFile>
<IndexLines>$([System.IO.File]::ReadAllText($(IndexFile)))</IndexLines>
</PropertyGroup>

<Message Importance="high" Text="Blazor Project Type detected as $(BlazorProjectType)"/>
<Message Importance="high" Text="Adding manifest to $(IndexFile)"
Condition="'$(IndexLines.Contains(rel=&quot;manifest&quot;))'=='false'"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,26 @@
</ServiceWorkerPreCacheGeneratedFiles>

<!-- Setup the declarations for the Service Worker -->
<ServiceWorkerConstants Condition="'$(ServiceWorkerConstants)' == ''">
<ServiceWorkerConstants Condition="'$(ServiceWorkerConstants)' == '' AND '$(BlazorProjectType)' == 'CSB'">
const baseURL = '$(ServiceWorkerBaseURL)'%3B;
const indexURL = '$(ServiceWorkerBaseUrl)$(ServiceWorkerIndexURL)'%3B;
const indexURL = '$(ServiceWorkerBaseUrl)$(ProjectIndexPage)'%3B;
const networkFetchEvent = '$(ServiceWorkerNetworkFetchEvent)'%3B;
const swInstallEvent = '$(ServiceWorkerInstallEvent)'%3B;
const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B;
const swActivateEvent = '$(ServiceWorkerActivateEvent)'%3B;
const staticCachePrefix = '$(ServiceWorkerCacheName)-v'%3B;
const staticCacheName = '$(ServiceWorkerCacheName)-v$(ServiceWorkerCacheVersion)'%3B;
</ServiceWorkerConstants>

<!-- Setup the declarations for the Service Worker -->
<ServiceWorkerConstants Condition="'$(ServiceWorkerConstants)' == '' AND '$(BlazorProjectType)' == 'SSB'">
const baseURL = '$(ServiceWorkerBaseURL)'%3B;
const indexURL = '$(ServiceWorkerBaseUrl)$(ProjectHomePage)'%3B;
const networkFetchEvent = '$(ServiceWorkerNetworkFetchEvent)'%3B;
const swInstallEvent = '$(ServiceWorkerInstallEvent)'%3B;
const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B;
const swActivateEvent = '$(ServiceWorkerActivateEvent)'%3B;
</ServiceWorkerConstants>
</PropertyGroup>

<!-- The "Templates" properties are used to define where to find templates for the service worker sections -->
Expand All @@ -69,7 +79,7 @@
<!-- The file that contains template code for the network fetch event in the service worker -->
<ServiceWorkerTemplateNetwork Condition="'$(ServiceWorkerTemplateNetwork)' == ''">$(ServiceWorkerTemplatePath)sw_$(ServiceWorkerNetworkFetchEvent)-$(ServiceWorkerPattern).template.js</ServiceWorkerTemplateNetwork>
</PropertyGroup>
<Message Importance="high" Text="Exclude:;@(ServiceWorkerPreCacheExcludeFiles);@(ServiceWorkerPreCacheExcludeReleaseFiles);"/>

<ItemGroup>
<!-- Build a list of files to pre-cache -->
<DistFiles Include="$(ServiceWorkerPreCacheIncludeFiles);" />
Expand All @@ -92,10 +102,12 @@
<WriteLinesToFile File="$(WWWRoot)$(ServiceWorkerFileName)"
Overwrite="true"
Lines="$(ServiceWorkerConstants)" />
<WriteLinesToFile File="$(WWWRoot)$(ServiceWorkerFileName)"
<WriteLinesToFile Condition="'$(BlazorProjectType)' == 'CSB'"
File="$(WWWRoot)$(ServiceWorkerFileName)"
Overwrite="false"
Lines="const requiredFiles = [;@(DistFiles -> '&quot;/%(RecursiveDir)%(FileName)%(Extension)&quot;'->Replace('\','/'),',%0D%0A');]%3B;" />
<WriteLinesToFile File="$(WWWRoot)$(ServiceWorkerFileName)"
<WriteLinesToFile Condition="'$(BlazorProjectType)' == 'CSB'"
File="$(WWWRoot)$(ServiceWorkerFileName)"
Overwrite="false"
Lines="@(InstallTemplateLines);" />
<WriteLinesToFile File="$(WWWRoot)$(ServiceWorkerFileName)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
<Target Name="_ServiceWorkerUpdateIndex">
<!-- Add ServiceWorkerRegister.js to index.html -->
<PropertyGroup>
<IndexFile>$(WWWRoot)$(ServiceWorkerIndexUrl)</IndexFile>
<IndexFile Condition="'$(BlazorProjectType)' == 'CSB'">$(WWWRoot)$(ProjectIndexPage)</IndexFile>
<IndexFile Condition="'$(BlazorProjectType)' == 'SSB'">$(ProjectHomePage)</IndexFile>
<IndexLines>$([System.IO.File]::ReadAllText($(IndexFile)))</IndexLines>
</PropertyGroup>

Expand Down
21 changes: 15 additions & 6 deletions Blazor.PWA.MSBuild.Tasks/build/BlazorPWA.MSBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
<Import Project="..\build\BlazorPWA.MSBuild.ServiceWorkerRegister.targets" />
<Import Project="..\build\BlazorPWA.MSBuild.Manifest.targets" />

<PropertyGroup Label="ProjectTypes">
<!-- Project type -->
<BlazorProjectType Condition="'$(BlazorProjectType)' == '' AND '$(TargetFramework)' == 'netcoreapp3.0'">SSB</BlazorProjectType>
<BlazorProjectType Condition="'$(BlazorProjectType)' == '' AND '$(TargetFramework)' == 'netstandard2.0'">CSB</BlazorProjectType>
</PropertyGroup>

<PropertyGroup Label="ServiceWorker">
<!-- The service worker file names to generate -->
<ServiceWorkerFileName Condition="'$(ServiceWorkerFileName)' == ''">ServiceWorker.js</ServiceWorkerFileName>
<ServiceWorkerRegisterFileName Condition="'$(ServiceWorkerRegisterFileName)' == ''">ServiceWorkerRegister.js</ServiceWorkerRegisterFileName>

<!-- The PWA strategy to implement in the service worker -->
<!-- Currently only supports offline-first -->
<!-- Currently only supports offline-first or none -->
<ServiceWorkerPattern Condition="'$(BlazorProjectType)' == 'CSB' AND '$(ServiceWorkerPattern)' == ''">offline-first</ServiceWorkerPattern>
<ServiceWorkerPattern Condition="'$(BlazorProjectType)' == 'SSB' AND '$(ServiceWorkerPattern)' == ''">none</ServiceWorkerPattern>
<ServiceWorkerPattern Condition="'$(ServiceWorkerPattern)' == ''">offline-first</ServiceWorkerPattern>
<!-- Remove this when we implement more patterns -->
<ServiceWorkerPattern>offline-first</ServiceWorkerPattern>

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

<!-- The actual home page served up for the base URL - so we have a way to cache it -->
<ServiceWorkerIndexUrl Condition="'$(ServiceWorkerIndexUrl)' == ''">index.html</ServiceWorkerIndexUrl>
<!-- The CSB home page served up for the base URL -->
<ProjectIndexPage Condition="'$(ProjectIndexPage)' == ''">index.html</ProjectIndexPage>

<!-- The SSB home page served up for the base URL -->
<ProjectHomePage Condition="'$(ProjectHomePage)' == ''">Pages/_Host.cshtml</ProjectHomePage>

</PropertyGroup>

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

<Message Importance="high" Text="Building Your PWA: Type: $(ServiceWorkerPattern) CacheName: $(ServiceWorkerCacheName) CacheVersion: $(ServiceWorkerCacheVersion)"/>
<Message Importance="high" Text="Building Your $(BlazorProjectType) PWA: Type: $(ServiceWorkerPattern) CacheName: $(ServiceWorkerCacheName) CacheVersion: $(ServiceWorkerCacheVersion)"/>
<CallTarget Targets="BuildServiceWorkerInstaller"/>
<CallTarget Targets="BuildServiceWorkerManifest"/>
<CallTarget Targets="BuildServiceWorker"/>
Expand Down

0 comments on commit 431069d

Please sign in to comment.