Skip to content

Commit cdc8f5c

Browse files
committed
Plug-in PWA support through static web assets
1 parent 4a6cd5b commit cdc8f5c

File tree

3 files changed

+22
-41
lines changed

3 files changed

+22
-41
lines changed

src/Components/Blazor/Build/src/targets/ServiceWorkerAssetsManifest.targets

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,26 @@
11
<Project>
2-
2+
33
<PropertyGroup>
44
<_BlazorCopyFilesToOutputDirectoryDependsOn>
55
$(_BlazorCopyFilesToOutputDirectoryDependsOn);
6-
_ComputeServiceWorkerAssetsManifestInputs;
76
_WriteServiceWorkerAssetsManifest;
7+
ResolveStaticWebAssetsInputs;
88
</_BlazorCopyFilesToOutputDirectoryDependsOn>
99
</PropertyGroup>
1010

11+
1112
<Target Name="_ComputeServiceWorkerAssetsManifestInputs"
1213
Condition="'$(ServiceWorkerAssetsManifest)' != ''"
13-
DependsOnTargets="PrepareBlazorOutputs">
14+
BeforeTargets="_ResolveBlazorOutputs">
1415

1516
<PropertyGroup>
1617
<_ServiceWorkerAssetsManifestIntermediateOutputPath>$(BlazorIntermediateOutputPath)serviceworkerassets.js</_ServiceWorkerAssetsManifestIntermediateOutputPath>
1718
</PropertyGroup>
1819

1920
<ItemGroup>
20-
<!-- Include _framework/* content -->
21-
<ServiceWorkerAssetsManifestItem
22-
Include="@(BlazorOutputWithTargetPath)"
23-
Condition="$([System.String]::Copy('%(BlazorOutputWithTargetPath.TargetOutputPath)').Replace('\','/').StartsWith('dist/'))">
24-
<AssetUrl>$([System.String]::Copy('%(BlazorOutputWithTargetPath.TargetOutputPath)').Replace('\','/').Substring(5))</AssetUrl>
25-
</ServiceWorkerAssetsManifestItem>
26-
27-
<!-- Include content from wwwroot -->
28-
<ServiceWorkerAssetsManifestItem
29-
Include="@(ContentWithTargetPath)"
30-
Condition="
31-
('%(ContentWithTargetPath.CopyToPublishDirectory)' == 'Always' OR '%(ContentWithTargetPath.CopyToPublishDirectory)' == 'PreserveNewest')
32-
AND $([System.String]::Copy('%(ContentWithTargetPath.TargetPath)').Replace('\','/').StartsWith('wwwroot/'))">
33-
<AssetUrl>$([System.String]::Copy('%(ContentWithTargetPath.TargetPath)').Replace('\','/').Substring(8))</AssetUrl>
34-
</ServiceWorkerAssetsManifestItem>
35-
36-
<!-- Include SWA from references -->
37-
<ServiceWorkerAssetsManifestItem
38-
Include="@(StaticWebAsset)"
39-
Condition="'%(StaticWebAsset.SourceType)' != ''">
40-
<AssetUrl>%(StaticWebAsset.BasePath)/%(StaticWebAsset.RelativePath)</AssetUrl>
41-
</ServiceWorkerAssetsManifestItem>
21+
<BlazorOutputWithTargetPath Condition="'$(ServiceWorkerAssetsManifest)' != ''"
22+
Include="$(_ServiceWorkerAssetsManifestIntermediateOutputPath)"
23+
TargetOutputPath="$(BaseBlazorDistPath)$(ServiceWorkerAssetsManifest)" />
4224
</ItemGroup>
4325

4426
</Target>
@@ -56,16 +38,19 @@
5638
OutputPath="$(_ServiceWorkerAssetsManifestIntermediateOutputPath)" />
5739

5840
<ItemGroup>
59-
<BlazorOutputWithTargetPath
60-
Include="$(_ServiceWorkerAssetsManifestIntermediateOutputPath)"
61-
TargetOutputPath="$(BaseBlazorDistPath)$(ServiceWorkerAssetsManifest)" />
62-
6341
<FileWrites Include="$(_ServiceWorkerAssetsManifestIntermediateOutputPath)" />
6442
</ItemGroup>
6543

6644
</Target>
6745

6846
<Target Name="_ComputeServiceWorkerAssetsManifestFileHashes">
47+
48+
<ItemGroup>
49+
<ServiceWorkerAssetsManifestItem Include="%(StaticWebAsset.Identity)" Condition="'%(RelativePath)' != '$(ServiceWorkerAssetsManifest)'">
50+
<AssetUrl>$([System.String]::Copy('$([System.String]::Copy('%(StaticWebAsset.BasePath)').TrimEnd('/'))/%(StaticWebAsset.RelativePath)').Replace('\','/').TrimStart('/'))</AssetUrl>
51+
</ServiceWorkerAssetsManifestItem>
52+
</ItemGroup>
53+
6954
<GetFileHash Files="@(ServiceWorkerAssetsManifestItem)" Algorithm="SHA256" HashEncoding="base64">
7055
<Output TaskParameter="Items" ItemName="_ServiceWorkerAssetsManifestItemWithHash" />
7156
</GetFileHash>
@@ -94,5 +79,5 @@
9479
<ServiceWorkerAssetsManifestVersion>$([System.String]::Copy('%(_ServiceWorkerAssetsManifestCombinedHash.FileHash)').Substring(0, 8))</ServiceWorkerAssetsManifestVersion>
9580
</PropertyGroup>
9681
</Target>
97-
82+
9883
</Project>

src/Components/Blazor/Build/src/targets/StaticWebAssets.targets

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<Target Name="_ResolveBlazorGeneratedAssets" DependsOnTargets="PrepareBlazorOutputs">
44
<ItemGroup>
5-
<StaticWebAsset Include="@(BlazorOutputWithTargetPath->'$(TargetDir)%(TargetOutputPath)')" RemoveMetadata="TargetOutputPath">
5+
<StaticWebAsset Include="@(BlazorOutputWithTargetPath->'%(FullPath)')" RemoveMetadata="TargetOutputPath">
66
<SourceType></SourceType>
77
<SourceId>$(PackageId)</SourceId>
88
<ContentRoot>$([MSBuild]::NormalizeDirectory('$(TargetDir)dist\'))</ContentRoot>
@@ -14,7 +14,7 @@
1414
These files are not "external" in the "traditional" sense but it is fine for now as this is an implementation detail.
1515
We only need to do this for the standalone case, for hosted scenarios this works just fine as the assets are considered
1616
external. -->
17-
<_ExternalStaticWebAsset Include="@(BlazorOutputWithTargetPath->'$(TargetDir)%(TargetOutputPath)')">
17+
<_ExternalStaticWebAsset Include="@(BlazorOutputWithTargetPath->'%(FullPath)')">
1818
<SourceId>$(PackageId)</SourceId>
1919
<!-- We just do this to keep the existing implementation happy. We will update this in the next release. -->
2020
<SourceType>Generated</SourceType>
@@ -100,7 +100,7 @@
100100
<RelativePath>$([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)', '$([MSBuild]::NormalizePath('$([System.Text.RegularExpressions.Regex]::Replace('%(RelativePath)','^wwwroot\\?\/?(.*)','$(BlazorPublishDistDir)$1'))'))'))</RelativePath>
101101
</_StandaloneExternalPublishStaticWebAsset>
102102

103-
<_CurrentProjectWwwrootStaticWebAsset Include="@(StaticWebAsset)" Exclude="@(BlazorOutputWithTargetPath->'$(TargetDir)%(TargetOutputPath)')" Condition="'%(StaticWebAsset.SourceType)' == ''" />
103+
<_CurrentProjectWwwrootStaticWebAsset Include="@(StaticWebAsset)" Exclude="@(BlazorOutputWithTargetPath->'%(FullPath)')" Condition="'%(StaticWebAsset.SourceType)' == ''" />
104104

105105
<_StandaloneWwwrootCurrentProjectPublishStaticWebAsset Include="%(_CurrentProjectWwwrootStaticWebAsset.Identity)">
106106
<RelativePath>$(BlazorPublishDistDir)%(RelativePath)</RelativePath>

src/ProjectTemplates/BlazorWasm.ProjectTemplates/BlazorWasm-CSharp.Client.csproj.in

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@
2020

2121
<!--#endif -->
2222
<!--#if PWA -->
23-
<ItemGroup>
24-
<!-- When publishing, swap service-worker.published.js in place of service-worker.js -->
25-
<Content Update="wwwroot\service-worker*.js" CopyToPublishDirectory="false" />
26-
<ContentWithTargetPath Include="wwwroot\service-worker.published.js">
27-
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
28-
<TargetPath>wwwroot\service-worker.js</TargetPath>
29-
</ContentWithTargetPath>
23+
<!-- When publishing, swap service-worker.published.js in place of service-worker.js -->
24+
<ItemGroup Condition="'$(DesignTimeBuild)' != 'true'">
25+
<Content Remove="wwwroot\service-worker.js" />
26+
<Content Update="wwwroot\service-worker.published.js" Link="wwwroot\service-worker.js" />
3027
</ItemGroup>
31-
3228
<!--#endif -->
3329
</Project>

0 commit comments

Comments
 (0)