Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for static assets embedded in another ASP.NET app
- Loading branch information
1 parent
a436679
commit c9bb187
Showing
33 changed files
with
691 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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 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 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,31 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
cleanup() { | ||
kill %% | ||
} | ||
trap cleanup 0 | ||
|
||
npm install @azure/static-web-apps-cli@0.8.3 | ||
SWA_PATH=`pwd`/node_modules/.bin/swa | ||
|
||
export BOOTSTRAP_APP_PATH=$1 | ||
export BOOTSTRAP_TEST_RUNNER_PATH=$2 | ||
export BOOTSTRAP_TEST_RUNNER_URL=$3 | ||
|
||
echo "BOOTSTRAP_APP_PATH=$BOOTSTRAP_APP_PATH" | ||
echo "BOOTSTRAP_TEST_RUNNER_PATH=$BOOTSTRAP_TEST_RUNNER_PATH" | ||
echo "BOOTSTRAP_TEST_RUNNER_URL=$BOOTSTRAP_TEST_RUNNER_URL" | ||
|
||
cd $BOOTSTRAP_APP_PATH | ||
dotnet build -c Release | ||
|
||
# We're not running using the published build, so we need to set | ||
# environment first. | ||
export ASPNETCORE_ENVIRONMENT=development | ||
dotnet run -c Release --no-build --urls=http://localhost:8000/ & | ||
sleep 5 | ||
|
||
cd $BOOTSTRAP_TEST_RUNNER_PATH | ||
npm install | ||
node app |
This file contains 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 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 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 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 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 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 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 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,18 @@ | ||
{ | ||
"solution": { | ||
"path": "Uno.Wasm.Bootstrap.sln", | ||
"projects": [ | ||
"Uno.Wasm.AotProfiler\\Uno.Wasm.AotProfiler.csproj", | ||
"Uno.Wasm.Bootstrap.Cli\\Uno.Wasm.Bootstrap.Cli.csproj", | ||
"Uno.Wasm.Bootstrap.DevServer\\Uno.Wasm.Bootstrap.DevServer.csproj", | ||
"Uno.Wasm.Bootstrap.Server\\Uno.Wasm.Bootstrap.Server.csproj", | ||
"Uno.Wasm.Bootstrap.UnitTests\\Uno.Wasm.Bootstrap.UnitTests.csproj", | ||
"Uno.Wasm.Bootstrap\\Uno.Wasm.Bootstrap.csproj", | ||
"Uno.Wasm.LogProfiler\\Uno.Wasm.LogProfiler.csproj", | ||
"Uno.Wasm.MetadataUpdater\\Uno.Wasm.MetadataUpdater.csproj", | ||
"Uno.Wasm.Packager\\Uno.Wasm.Packager.csproj", | ||
"Uno.Wasm.TimezoneData\\Uno.Wasm.TimezoneData.csproj", | ||
"Uno.Wasm.Tuner\\Uno.Wasm.Tuner.csproj" | ||
] | ||
} | ||
} |
This file contains 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,138 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Mime; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.StaticFiles; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Uno.Wasm.Bootstrap.Server | ||
{ | ||
/// <summary> | ||
/// Extensions for mapping Uno Platform WebAssembly applications. | ||
/// </summary> | ||
public static class AspNetExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the application to serve Uno Platform WebAssembly framework files from the path <paramref name="pathPrefix"/>. This path must correspond to a referenced Uno Platform WebAssembly application project. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IApplicationBuilder"/>.</param> | ||
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Uno Platform WebAssembly application.</param> | ||
/// <returns>The <see cref="IApplicationBuilder"/></returns> | ||
public static IApplicationBuilder UseUnoFrameworkFiles(this IApplicationBuilder builder, PathString pathPrefix) | ||
{ | ||
if (builder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
var webHostEnvironment = builder.ApplicationServices.GetRequiredService<IWebHostEnvironment>(); | ||
|
||
var options = CreateStaticFilesOptions(webHostEnvironment.WebRootFileProvider); | ||
|
||
builder.UseWhen(ctx => ctx.Request.Path.StartsWithSegments(pathPrefix, out var rest), | ||
subBuilder => | ||
{ | ||
subBuilder.Use(async (context, next) => | ||
{ | ||
context.Response.Headers.Append("UnoPlatform-Environment", webHostEnvironment.EnvironmentName); | ||
if (webHostEnvironment.IsDevelopment()) | ||
{ | ||
// DOTNET_MODIFIABLE_ASSEMBLIES is used by the runtime to initialize hot-reload specific environment variables and is configured | ||
// by the launching process (dotnet-watch / Visual Studio). | ||
// In Development, we'll transmit the environment variable to WebAssembly as a HTTP header. The bootstrapping code will read the header | ||
// and configure it as env variable for the wasm app. | ||
if (Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES") is string dotnetModifiableAssemblies) | ||
{ | ||
context.Response.Headers.Append("DOTNET-MODIFIABLE-ASSEMBLIES", dotnetModifiableAssemblies); | ||
} | ||
// See https://github.com/dotnet/aspnetcore/issues/37357#issuecomment-941237000 | ||
// Translate the _ASPNETCORE_BROWSER_TOOLS environment configured by the browser tools agent in to a HTTP response header. | ||
if (Environment.GetEnvironmentVariable("__ASPNETCORE_BROWSER_TOOLS") is string dotnetWasmHotReload) | ||
{ | ||
context.Response.Headers.Append("ASPNETCORE-BROWSER-TOOLS", dotnetWasmHotReload); | ||
} | ||
} | ||
await next(context); | ||
}); | ||
subBuilder.UseStaticFiles(options); | ||
}); | ||
|
||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the application to serve Uno Platform WebAssembly framework files from the root path "/". | ||
/// </summary> | ||
/// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param> | ||
/// <returns>The <see cref="IApplicationBuilder"/></returns> | ||
public static IApplicationBuilder UseUnoFrameworkFiles(this IApplicationBuilder applicationBuilder) => | ||
UseUnoFrameworkFiles(applicationBuilder, default); | ||
|
||
private static StaticFileOptions CreateStaticFilesOptions(IFileProvider webRootFileProvider) | ||
{ | ||
var options = new StaticFileOptions | ||
{ | ||
FileProvider = webRootFileProvider | ||
}; | ||
|
||
var contentTypeProvider = new FileExtensionContentTypeProvider(); | ||
AddMapping(contentTypeProvider, ".dll", MediaTypeNames.Application.Octet); | ||
AddMapping(contentTypeProvider, ".clr", MediaTypeNames.Application.Octet); | ||
AddMapping(contentTypeProvider, ".pdb", MediaTypeNames.Application.Octet); | ||
AddMapping(contentTypeProvider, ".br", MediaTypeNames.Application.Octet); | ||
AddMapping(contentTypeProvider, ".dat", MediaTypeNames.Application.Octet); | ||
AddMapping(contentTypeProvider, ".blat", MediaTypeNames.Application.Octet); | ||
|
||
options.ContentTypeProvider = contentTypeProvider; | ||
|
||
// Static files middleware will try to use application/x-gzip as the content | ||
// type when serving a file with a gz extension. We need to correct that before | ||
// sending the file. | ||
options.OnPrepareResponse = fileContext => | ||
{ | ||
// At this point we mapped something from the /_framework | ||
fileContext.Context.Response.Headers.Append(HeaderNames.CacheControl, "no-cache"); | ||
var requestPath = fileContext.Context.Request.Path; | ||
var fileExtension = Path.GetExtension(requestPath.Value); | ||
if (string.Equals(fileExtension, ".gz") || string.Equals(fileExtension, ".br")) | ||
{ | ||
// When we are serving framework files (under _framework/ we perform content negotiation | ||
// on the accept encoding and replace the path with <<original>>.gz|br if we can serve gzip or brotli content | ||
// respectively. | ||
// Here we simply calculate the original content type by removing the extension and apply it | ||
// again. | ||
// When we revisit this, we should consider calculating the original content type and storing it | ||
// in the request along with the original target path so that we don't have to calculate it here. | ||
var originalPath = Path.GetFileNameWithoutExtension(requestPath.Value); | ||
if (originalPath != null && contentTypeProvider.TryGetContentType(originalPath, out var originalContentType)) | ||
{ | ||
fileContext.Context.Response.ContentType = originalContentType; | ||
} | ||
} | ||
}; | ||
|
||
return options; | ||
} | ||
|
||
private static void AddMapping(FileExtensionContentTypeProvider provider, string name, string mimeType) | ||
{ | ||
if (!provider.Mappings.ContainsKey(name)) | ||
{ | ||
provider.Mappings.Add(name, mimeType); | ||
} | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/Uno.Wasm.Bootstrap.Server/Uno.Wasm.Bootstrap.Server.csproj
This file contains 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,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<NoWarn>1701;1702;1705;649</NoWarn> | ||
<PackageId>Uno.Wasm.Bootstrap.Server</PackageId> | ||
<GeneratePackageOnBuild Condition="'$(Configuration)'=='Release'">true</GeneratePackageOnBuild> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Authors>nventive</Authors> | ||
<PackageProjectUrl>https://github.com/nventive/Uno.Wasm.Bootstrap</PackageProjectUrl> | ||
<PackageIconUrl>https://nv-assets.azurewebsites.net/logos/uno.png</PackageIconUrl> | ||
<RepositoryUrl>https://github.com/nventive/Uno.Wasm.Bootstrap</RepositoryUrl> | ||
<Description>Runtime server features for ASP.NET Core hosted Uno Platform applications.</Description> | ||
<Copyright>Copyright (C) 2015-$([System.DateTime]::Now.ToString(`yyyy`)) Uno Platform inc. - all rights reserved</Copyright> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="build\Uno.Wasm.Bootstrap.Server.targets"> | ||
<PackagePath>build</PackagePath> | ||
<Pack>true</Pack> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.App" Version="6.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
3 changes: 3 additions & 0 deletions
3
src/Uno.Wasm.Bootstrap.Server/build/Uno.Wasm.Bootstrap.Server.targets
This file contains 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,3 @@ | ||
<Project> | ||
|
||
</Project> |
Oops, something went wrong.