-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Blazor] Move Service worker to static web assets in preparation for compressing and fingerprinting everything #39177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/StaticWebAssetsSdk/Tasks/ServiceWorker/AssetsManifestFile.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks | ||
{ | ||
#pragma warning disable IDE1006 // Naming Styles | ||
public class AssetsManifestFile | ||
{ | ||
/// <summary> | ||
/// Gets or sets a version string. | ||
/// </summary> | ||
public string version { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the assets. Keys are URLs; values are base-64-formatted SHA256 content hashes. | ||
/// </summary> | ||
public AssetsManifestFileEntry[] assets { get; set; } | ||
} | ||
|
||
public class AssetsManifestFileEntry | ||
{ | ||
/// <summary> | ||
/// Gets or sets the asset URL. Normally this will be relative to the application's base href. | ||
/// </summary> | ||
public string url { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the file content hash. This should be the base-64-formatted SHA256 value. | ||
/// </summary> | ||
public string hash { get; set; } | ||
} | ||
#pragma warning restore IDE1006 // Naming Styles | ||
} | ||
92 changes: 92 additions & 0 deletions
92
src/StaticWebAssetsSdk/Tasks/ServiceWorker/GenerateServiceWorkerAssetsManifest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.Serialization.Json; | ||
using System.Security.Cryptography; | ||
using Microsoft.Build.Framework; | ||
|
||
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks | ||
{ | ||
public partial class GenerateServiceWorkerAssetsManifest : Task | ||
{ | ||
[Required] | ||
public ITaskItem[] Assets { get; set; } | ||
|
||
public string Version { get; set; } | ||
|
||
[Required] | ||
public string OutputPath { get; set; } | ||
|
||
[Output] | ||
public string CalculatedVersion { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
using var fileStream = File.Create(OutputPath); | ||
CalculatedVersion = GenerateAssetManifest(fileStream); | ||
|
||
return true; | ||
} | ||
|
||
internal string GenerateAssetManifest(Stream stream) | ||
{ | ||
var assets = new AssetsManifestFileEntry[Assets.Length]; | ||
Parallel.For(0, assets.Length, i => | ||
{ | ||
var item = Assets[i]; | ||
var hash = item.GetMetadata("FileHash"); | ||
var url = item.GetMetadata("AssetUrl"); | ||
|
||
if (string.IsNullOrEmpty(hash)) | ||
{ | ||
// Some files that are part of the service worker manifest may not have their hashes previously | ||
// calcualted. Calculate them at this time. | ||
using var sha = SHA256.Create(); | ||
using var file = File.OpenRead(item.ItemSpec); | ||
var bytes = sha.ComputeHash(file); | ||
|
||
hash = Convert.ToBase64String(bytes); | ||
} | ||
|
||
assets[i] = new AssetsManifestFileEntry | ||
{ | ||
hash = "sha256-" + hash, | ||
url = url, | ||
}; | ||
}); | ||
|
||
var version = Version; | ||
if (string.IsNullOrEmpty(version)) | ||
{ | ||
// If a version isn't specified (which is likely the most common case), construct a Version by combining | ||
// the file names + hashes of all the inputs. | ||
|
||
var combinedHash = string.Join( | ||
Environment.NewLine, | ||
assets.OrderBy(f => f.url, StringComparer.Ordinal).Select(f => f.hash)); | ||
|
||
using var sha = SHA256.Create(); | ||
var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(combinedHash)); | ||
version = Convert.ToBase64String(bytes).Substring(0, 8); | ||
} | ||
|
||
var data = new AssetsManifestFile | ||
{ | ||
version = version, | ||
assets = assets, | ||
}; | ||
|
||
using var streamWriter = new StreamWriter(stream, Encoding.UTF8, bufferSize: 50, leaveOpen: true); | ||
streamWriter.Write("self.assetsManifest = "); | ||
streamWriter.Flush(); | ||
|
||
using var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8, ownsStream: false, indent: true); | ||
new DataContractJsonSerializer(typeof(AssetsManifestFile)).WriteObject(jsonWriter, data); | ||
jsonWriter.Flush(); | ||
|
||
streamWriter.WriteLine(";"); | ||
|
||
return version; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.