Skip to content

Commit 5632f4d

Browse files
Moved index.html manipulation to its own target, conditional on serviceworkerregister.js existing.
Added some conditions to reading the templates -- they have to exist!
1 parent 05c81e9 commit 5632f4d

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

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

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,6 @@
66
<Message Importance="high" Text="Building Service Worker Register $(ServiceWorkerRegisterFileName)" Condition="!Exists('$(WWWRoot)$(ServiceWorkerRegisterFileName)')"/>
77
<Message Importance="high" Text="Re-Building Service Worker Register $(ServiceWorkerRegisterFileName)" Condition="Exists('$(WWWRoot)$(ServiceWorkerRegisterFileName)')"/>
88

9-
<!-- Add ServiceWorkerRegister.js to index.html -->
10-
<PropertyGroup Label="HomePage">
11-
<IndexFile>$(WWWRoot)$(ServiceWorkerIndexUrl)</IndexFile>
12-
<IndexLines>$([System.IO.File]::ReadAllText($(IndexFile)))</IndexLines>
13-
</PropertyGroup>
14-
15-
<Message Importance="high" Text="Adding $(ServiceWorkerRegisterFileName) to $(IndexFile)"
16-
Condition="'$(IndexLines.Contains($(ServiceWorkerRegisterFileName)))'=='false'"/>
17-
<WriteLinesToFile
18-
File="$(IndexFile)"
19-
Overwrite="true"
20-
Lines="$(IndexLines.Replace('&lt;/head&gt;',' &lt;script src=&quot;$(ServiceWorkerRegisterFileName)&quot;&gt;&lt;/script&gt;%0D%0A&lt;/head&gt;'))"
21-
Condition="'$(IndexLines.Contains($(ServiceWorkerRegisterFileName)))'=='false'"/>
22-
239
<!-- The "Templates" properties are used to define where to find templates for the service worker register -->
2410
<PropertyGroup Label="Templates">
2511
<!-- The template folder location ** probably never change ** -->
@@ -34,9 +20,6 @@
3420
<ServiceWorkerRegisterInstallableType Condition="'$(ServiceWorkerRegisterInstallableType)'==''">installable-banner</ServiceWorkerRegisterInstallableType>
3521
<!-- The file that contains template code for the service worker "update available" -->
3622
<ServiceWorkerRegisterInstallableTemplate Condition="'$(ServiceWorkerRegisterInstallableTemplate)' == ''">$(ServiceWorkerRegisterTemplatePath)sw_register-$(ServiceWorkerRegisterInstallableType).template.js</ServiceWorkerRegisterInstallableTemplate>
37-
</PropertyGroup>
38-
39-
<PropertyGroup Label="Implementation">
4023
<!-- event fired by browser when the service worker has installed ** probably never change **-->
4124
<ServiceWorkerInstalledEvent Condition="'$(ServiceWorkerInstalledEvent)'==''">installed</ServiceWorkerInstalledEvent>
4225
<!-- Text to display when an update is available -->
@@ -46,19 +29,55 @@
4629
const serviceWorkerFileName = '$(ServiceWorkerBaseURL)$(ServiceWorkerFileName)'%3B;
4730
const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B;
4831
const staticCachePrefix = '$(ServiceWorkerCacheName)-v'%3B;
49-
const updateAlertMessage = '$(ServiceWorkerUpdateAlertText)'%3B;
32+
const updateAlertMessage = '$(ServiceWorkerUpdateAlertText)'%3B;
5033
</ServiceWorkerRegisterConstants>
5134
</PropertyGroup>
5235
<ItemGroup>
53-
<ServiceWorkerRegisterTemplateLines Include="$([System.IO.File]::ReadAllText($(ServiceWorkerRegisterTemplate)))"/>
54-
<ServiceWorkerRegisterTemplateLines Include="$([System.IO.File]::ReadAllText($(ServiceWorkerRegisterUpdateTemplate)))"/>
55-
<ServiceWorkerRegisterTemplateLines Include="$([System.IO.File]::ReadAllText($(ServiceWorkerRegisterInstallableTemplate)))"/>
36+
<!-- Read the Service Worker Register template-->
37+
<ServiceWorkerRegisterTemplateLines
38+
Condition="Exists('$(ServiceWorkerRegisterTemplate)')"
39+
Include="$([System.IO.File]::ReadAllText($(ServiceWorkerRegisterTemplate)))"/>
40+
<!-- Read the Service Worker Update template-->
41+
<ServiceWorkerRegisterTemplateLines
42+
Condition="Exists('$(ServiceWorkerRegisterUpdateTemplate)')"
43+
Include="$([System.IO.File]::ReadAllText($(ServiceWorkerRegisterUpdateTemplate)))"/>
44+
<!-- Read the Service Worker Installable template-->
45+
<ServiceWorkerRegisterTemplateLines
46+
Condition="Exists('$(ServiceWorkerRegisterInstallableTemplate)')"
47+
Include="$([System.IO.File]::ReadAllText($(ServiceWorkerRegisterInstallableTemplate)))"/>
5648
</ItemGroup>
49+
<!-- (Re)Create the ServiceWorkerRegister.js file -->
5750
<WriteLinesToFile
51+
Condition="'$(ServiceWorkerRegisterConstants)@(ServiceWorkerRegisterTemplateLines)' != ''"
5852
File="$(WWWRoot)$(ServiceWorkerRegisterFileName)"
5953
Overwrite="true"
6054
Lines="$(ServiceWorkerRegisterConstants);@(ServiceWorkerRegisterTemplateLines);"
6155
/>
56+
<!-- Call the target for adding the ServiceWorkerRegister.js file to index.html -->
57+
<CallTarget
58+
Condition="Exists('$(WWWRoot)$(ServiceWorkerRegisterFileName)')"
59+
Targets="_ServiceWorkerUpdateIndex"
60+
/>
6261
</Target>
6362

63+
<Target Name="_ServiceWorkerUpdateIndex">
64+
<!-- Add ServiceWorkerRegister.js to index.html -->
65+
<PropertyGroup>
66+
<IndexFile>$(WWWRoot)$(ServiceWorkerIndexUrl)</IndexFile>
67+
<IndexLines>$([System.IO.File]::ReadAllText($(IndexFile)))</IndexLines>
68+
</PropertyGroup>
69+
70+
<Message
71+
Importance="high"
72+
Text="Adding $(ServiceWorkerRegisterFileName) to $(IndexFile)"
73+
Condition="'$(IndexLines.Contains($(ServiceWorkerRegisterFileName)))'=='false'"
74+
/>
75+
<WriteLinesToFile
76+
File="$(IndexFile)"
77+
Overwrite="true"
78+
Lines="$(IndexLines.Replace('&lt;/head&gt;',' &lt;script src=&quot;$(ServiceWorkerRegisterFileName)&quot;&gt;&lt;/script&gt;%0D%0A&lt;/head&gt;'))"
79+
Condition="'$(IndexLines.Contains($(ServiceWorkerRegisterFileName)))'=='false'"
80+
/>
81+
82+
</Target>
6483
</Project>

0 commit comments

Comments
 (0)