Skip to content

Commit

Permalink
feat: Improve cacheability of the app payload in CDNs
Browse files Browse the repository at this point in the history
This feature moves all the root files, except index.html to a versioned folder, avoiding different caching policies and expirations based on content.
  • Loading branch information
jeromelaban committed Jun 3, 2020
1 parent 009da4c commit fdc7d1a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ The documentation for this file [can be found here](https://github.com/mono/link
## Publishing the build results
The easiest way to publish the build results is to use the Visual Studio publish menu on your project. This will allow to use all the features provided by the standard experience, as described in the [Deploy to Azure App Service](https://docs.microsoft.com/en-us/visualstudio/deployment/quickstart-deploy-to-azure?view=vs-2017).

For deeper integration in the publishing pipeline, the `WasmShellOutputPackagePath` property is defined by the bootstrapper after the `BuildDist` target, which contains the path to the generated `package_XXX` content.

## Serve the Wasm app through Windows Linux Subsystem
Using Windows 10, serving the app through a small Web Server is done through WSL.

Expand Down
54 changes: 43 additions & 11 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public partial class ShellTask_v0 : Microsoft.Build.Utilities.Task

public bool GenerateAOTProfile { get; set; } = false;

[Output]
public string? OutputPackagePath { get; private set; }

public override bool Execute()
{
try
Expand All @@ -179,10 +182,10 @@ public override bool Execute()
ExtractAdditionalJS();
ExtractAdditionalCSS();
CleanupDist();
MergeConfig();
TouchServiceWorker();
PrepareFinalDist();
GenerateConfig();
MergeConfig();
GenerateHtml();
TryCompressDist();

Expand Down Expand Up @@ -345,10 +348,10 @@ private void CompressFiles(string[] filesToCompress, string method, Action<strin
.AsParallel()
.Select(fileName =>
{
var compressedPathBase = Path.Combine(_workDistPath, "_compressed_" + method);
var compressedPathBase = Path.Combine(_distPath, "_compressed_" + method);
var compressedFileName = fileName;
compressedFileName = compressedFileName.Replace(_workDistPath, compressedPathBase);
compressedFileName = compressedFileName.Replace(_distPath, compressedPathBase);
DirectoryCreateDirectory(Path.GetDirectoryName(compressedFileName));
Expand Down Expand Up @@ -910,11 +913,10 @@ select AssemblyDefinition.ReadAssembly(asmPath)

private void PrepareFinalDist()
{

IEnumerable<byte> ComputeHash(string file)
{
using var hashFunction = SHA1.Create();
using var s = File.OpenRead(file);
using var s = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
return hashFunction.ComputeHash(s);
}

Expand All @@ -928,8 +930,22 @@ IEnumerable<byte> ComputeHash(string file)
using var hashFunction = SHA1.Create();
var hash = string.Join("", hashFunction.ComputeHash(allBytes).Select(b => b.ToString("x2")));

_remoteBasePackagePath = $"package_{hash}";
_finalPackagePath = TryConvertLongPath(Path.Combine(_distPath, _remoteBasePackagePath));
if (_shellMode == ShellMode.Node)
{
_remoteBasePackagePath = "";
_finalPackagePath = _distPath;
OutputPackagePath = _distPath;
}
else
{
_remoteBasePackagePath = $"package_{hash}";
_finalPackagePath = TryConvertLongPath(Path.Combine(_distPath, _remoteBasePackagePath));
OutputPackagePath = _finalPackagePath.Replace(@"\\?\", "");
}

// Create the path if it does not exist (particularly if the path is
// not in a set of folder that exists)
Directory.CreateDirectory(_finalPackagePath);

if (Directory.Exists(_finalPackagePath))
{
Expand All @@ -938,9 +954,25 @@ IEnumerable<byte> ComputeHash(string file)

Directory.Move(_workDistPath, _finalPackagePath);

MoveFileSafe(Path.Combine(_finalPackagePath, "web.config"), Path.Combine(_distPath, "web.config"));
MoveFileSafe(Path.Combine(_finalPackagePath, "server.py"), Path.Combine(_distPath, "server.py"));

RenameFiles(_finalPackagePath, "dll");
}

private static void MoveFileSafe(string source, string target)
{
if (File.Exists(source) && source != target)
{
if (File.Exists(target))
{
File.Delete(target);
}

File.Move(source, target);
}
}


/// <summary>
/// Renames the files to avoid quarantine by antivirus software such as Symantec,
Expand Down Expand Up @@ -1200,13 +1232,13 @@ private void MergeConfig()
var tempFile = Path.GetTempFileName();
try
{
var monoJsPath = Path.Combine(_workDistPath, "dotnet.js");
var monoJsPath = Path.Combine(_finalPackagePath, "dotnet.js");

using (var fs = new StreamWriter(tempFile))
{
fs.Write(File.ReadAllText(Path.Combine(_workDistPath, "mono-config.js")));
fs.Write(File.ReadAllText(Path.Combine(_workDistPath, "uno-config.js")));
fs.Write(File.ReadAllText(Path.Combine(_workDistPath, "uno-bootstrap.js")));
fs.Write(File.ReadAllText(Path.Combine(_finalPackagePath, "mono-config.js")));
fs.Write(File.ReadAllText(Path.Combine(_finalPackagePath, "uno-config.js")));
fs.Write(File.ReadAllText(Path.Combine(_finalPackagePath, "uno-bootstrap.js")));
fs.Write(File.ReadAllText(monoJsPath));
}

Expand Down
6 changes: 4 additions & 2 deletions src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<Output TaskParameter="PackagerProjectFile" PropertyName="_UnoMonoPackagerProjectFile" />
</UnoInstallSDKTask_v0>

<ShellTask_v0
<ShellTask_v0
CurrentProjectPath="$(MSBuildProjectDirectory)"
BuildTaskBasePath="$(MSBuildThisFileDirectory)"
Assembly="$(IntermediateOutputPath)$(TargetFileName)"
Expand Down Expand Up @@ -158,7 +158,9 @@
GenerateAOTProfile="$(WasmShellGenerateAOTProfile)"
AotProfile="@(WasmShellEnableAotProfile)"
Assets="@(Content)"
ReferencePath="@(_AssembliesForReferenceCopyLocalPaths)" />
ReferencePath="@(_AssembliesForReferenceCopyLocalPaths)">
<Output TaskParameter="OutputPackagePath" PropertyName="WasmShellOutputPackagePath" />
</ShellTask_v0>
</Target>

<Target Name="_CleanDist" BeforeTargets="Clean">
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.Wasm.Sample/Uno.Wasm.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<!-- Add linked content first to validate directory creation from links -->
<Content Include="..\.editorconfig" Link="AdditionalContent\%(FileName)%(Extension)"/>
<Content Include="../nuget.config" Link="AdditionalContent\%(FileName)%(Extension)"/>
<Content Include="AdditionalContent\SomeContent01.txt" />
<Content Include="AdditionalContent\SomeContent01.txt" />
<Content Include="AdditionalContent/SomeContent02.txt" />
</ItemGroup>

Expand All @@ -49,7 +49,7 @@
<_AdditionalFile Include="nuget.config" />
</ItemGroup>

<Error Condition="!exists('bin/$(Configuration)/$(TargetFramework)/dist/AdditionalContent/%(_AdditionalFile.Identity)')" Text="%(_AdditionalFile.Identity) does not exist in dist"/>
<Error Condition="!exists('$(WasmShellOutputPackagePath)/AdditionalContent/%(_AdditionalFile.Identity)')" Text="%(_AdditionalFile.Identity) does not exist in $(WasmShellOutputPackagePath)"/>
<Message Importance="high" Text="Output dist validated" />
</Target>

Expand Down

0 comments on commit fdc7d1a

Please sign in to comment.