Add support for static web assets in blazor client-side (#11042)
* Add support for static web assets in blazor client-side * Blazor serve use MSBuild data instead of heuristics to run the app * Remove blazor-client-side app Static Web Assets when hosted in an ASP.NET Core
This commit is contained in:
parent
6ce8a879ae
commit
0a4f42a7e2
|
|
@ -28,7 +28,7 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<RunCommand>dotnet</RunCommand>
|
<RunCommand>dotnet</RunCommand>
|
||||||
<_BlazorCliLocation>$(MSBuildThisFileDirectory)../../DevServer/src/bin/$(Configuration)/netcoreapp3.0/blazor-devserver.dll</_BlazorCliLocation>
|
<_BlazorCliLocation>$(MSBuildThisFileDirectory)../../DevServer/src/bin/$(Configuration)/netcoreapp3.0/blazor-devserver.dll</_BlazorCliLocation>
|
||||||
<RunArguments>exec "$(_BlazorCliLocation)" serve $(AdditionalRunArguments)</RunArguments>
|
<RunArguments>exec "$(_BlazorCliLocation)" serve "$(MSBuildProjectDirectory)/$(OutputPath)$(TargetFileName)" $(AdditionalRunArguments)</RunArguments>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -29,4 +29,18 @@
|
||||||
<ContentWithTargetPath Include="$(BlazorMetadataFilePath)" TargetPath="$(BlazorMetadataFileName)" CopyToOutputDirectory="PreserveNewest" />
|
<ContentWithTargetPath Include="$(BlazorMetadataFilePath)" TargetPath="$(BlazorMetadataFileName)" CopyToOutputDirectory="PreserveNewest" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Target>
|
</Target>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<GetCurrentProjectStaticWebAssetsDependsOn>
|
||||||
|
$(GetCurrentProjectStaticWebAssetsDependsOn);
|
||||||
|
_ClearCurrentStaticWebAssetsForReferenceDiscovery
|
||||||
|
</GetCurrentProjectStaticWebAssetsDependsOn>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<Target Name="_ClearCurrentStaticWebAssetsForReferenceDiscovery">
|
||||||
|
<ItemGroup>
|
||||||
|
<StaticWebAsset Remove="@(StaticWebAsset)" Condition="'%(SourceType)' == ''" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Target>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.Versioning;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.CommandLineUtils;
|
using Microsoft.Extensions.CommandLineUtils;
|
||||||
|
|
||||||
|
|
@ -20,11 +23,28 @@ namespace Microsoft.AspNetCore.Blazor.DevServer.Commands
|
||||||
|
|
||||||
HelpOption("-?|-h|--help");
|
HelpOption("-?|-h|--help");
|
||||||
|
|
||||||
|
ApplicationPath = new CommandArgument()
|
||||||
|
{
|
||||||
|
Description = "Path to the client application dll",
|
||||||
|
MultipleValues = false,
|
||||||
|
Name = "<PATH>",
|
||||||
|
ShowInHelpText = true
|
||||||
|
};
|
||||||
|
Arguments.Add(ApplicationPath);
|
||||||
|
|
||||||
OnExecute(Execute);
|
OnExecute(Execute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CommandArgument ApplicationPath { get; private set; }
|
||||||
|
|
||||||
private int Execute()
|
private int Execute()
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(ApplicationPath.Value))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Invalid value for parameter '{nameof(ApplicationPath)}'. Value supplied: '{ApplicationPath.Value}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
Server.Startup.ApplicationAssembly = ApplicationPath.Value;
|
||||||
Server.Program.BuildWebHost(RemainingArguments.ToArray()).Run();
|
Server.Program.BuildWebHost(RemainingArguments.ToArray()).Run();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,24 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Components.Server;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Mime;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Components.Server;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.ResponseCompression;
|
using Microsoft.AspNetCore.ResponseCompression;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Mime;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Blazor.DevServer.Server
|
namespace Microsoft.AspNetCore.Blazor.DevServer.Server
|
||||||
{
|
{
|
||||||
internal class Startup
|
internal class Startup
|
||||||
{
|
{
|
||||||
|
public static string ApplicationAssembly { get; set; }
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddRouting();
|
services.AddRouting();
|
||||||
|
|
@ -33,22 +35,36 @@ namespace Microsoft.AspNetCore.Blazor.DevServer.Server
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment environment, IConfiguration configuration)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment environment, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
|
var applicationAssemblyFullPath = ResolveApplicationAssemblyFullPath(environment);
|
||||||
|
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
app.UseResponseCompression();
|
app.UseResponseCompression();
|
||||||
EnableConfiguredPathbase(app, configuration);
|
EnableConfiguredPathbase(app, configuration);
|
||||||
|
|
||||||
app.UseBlazorDebugging();
|
app.UseBlazorDebugging();
|
||||||
|
|
||||||
app.UseClientSideBlazorFiles(FindClientAssemblyPath(environment));
|
app.UseStaticFiles();
|
||||||
|
app.UseClientSideBlazorFiles(applicationAssemblyFullPath);
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
endpoints.MapFallbackToClientSideBlazor(FindClientAssemblyPath(environment), "index.html");
|
endpoints.MapFallbackToClientSideBlazor(applicationAssemblyFullPath, "index.html");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string ResolveApplicationAssemblyFullPath(IWebHostEnvironment environment)
|
||||||
|
{
|
||||||
|
var applicationAssemblyFullPath = Path.Combine(environment.ContentRootPath, ApplicationAssembly);
|
||||||
|
if (!File.Exists(applicationAssemblyFullPath))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Application assembly not found at {applicationAssemblyFullPath}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return applicationAssemblyFullPath;
|
||||||
|
}
|
||||||
|
|
||||||
private static void EnableConfiguredPathbase(IApplicationBuilder app, IConfiguration configuration)
|
private static void EnableConfiguredPathbase(IApplicationBuilder app, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
var pathBase = configuration.GetValue<string>("pathbase");
|
var pathBase = configuration.GetValue<string>("pathbase");
|
||||||
|
|
@ -73,53 +89,5 @@ namespace Microsoft.AspNetCore.Blazor.DevServer.Server
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string FindClientAssemblyPath(IWebHostEnvironment environment)
|
|
||||||
{
|
|
||||||
var contentRoot = environment.ContentRootPath;
|
|
||||||
var binDir = FindClientBinDir(contentRoot);
|
|
||||||
var appName = Path.GetFileName(contentRoot); // TODO: Allow for the possibility that the assembly name has been overridden
|
|
||||||
var assemblyPath = Path.Combine(binDir, $"{appName}.dll");
|
|
||||||
if (!File.Exists(assemblyPath))
|
|
||||||
{
|
|
||||||
throw new FileNotFoundException($"Could not locate application assembly at expected location {assemblyPath}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return assemblyPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string FindClientBinDir(string clientAppSourceRoot)
|
|
||||||
{
|
|
||||||
// As a temporary workaround for https://github.com/aspnet/Blazor/issues/261,
|
|
||||||
// disallow the scenario where there is both a Debug *and* a Release dir.
|
|
||||||
// Only allow there to be one, and that's the one we pick.
|
|
||||||
var debugDirPath = Path.Combine(clientAppSourceRoot, "bin", "Debug");
|
|
||||||
var releaseDirPath = Path.Combine(clientAppSourceRoot, "bin", "Release");
|
|
||||||
var debugDirExists = Directory.Exists(debugDirPath);
|
|
||||||
var releaseDirExists = Directory.Exists(releaseDirPath);
|
|
||||||
if (debugDirExists && releaseDirExists)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"Cannot identify unique bin directory for Blazor app. " +
|
|
||||||
$"Found both '{debugDirPath}' and '{releaseDirPath}'. Ensure that only one is present on " +
|
|
||||||
$"disk. This is a temporary limitation (see https://github.com/aspnet/Blazor/issues/261).");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(debugDirExists || releaseDirExists))
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"Cannot find bin directory for Blazor app. " +
|
|
||||||
$"Neither '{debugDirPath}' nor '{releaseDirPath}' exists on disk. Make sure the project has been built.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var binDir = debugDirExists ? debugDirPath : releaseDirPath;
|
|
||||||
|
|
||||||
var subdirectories = Directory.GetDirectories(binDir);
|
|
||||||
if (subdirectories.Length != 1)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"Could not locate bin directory for Blazor app. " +
|
|
||||||
$"Expected to find exactly 1 subdirectory in '{binDir}', but found {subdirectories.Length}.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return Path.Combine(binDir, subdirectories[0]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<Project>
|
<Project>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<_BlazorDevServerDll>$(MSBuildThisFileDirectory)../tools/blazor-devserver.dll</_BlazorDevServerDll>
|
<_BlazorDevServerDll>$(MSBuildThisFileDirectory)../tools/blazor-devserver.dll</_BlazorDevServerDll>
|
||||||
<RunCommand>dotnet</RunCommand>
|
<RunCommand>dotnet</RunCommand>
|
||||||
<RunArguments>"$(_BlazorDevServerDll)" serve</RunArguments>
|
<RunArguments>"$(_BlazorDevServerDll)" serve "$(MSBuildProjectDirectory)/$(OutputPath)$(TargetFileName)"</RunArguments>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ namespace BlazorHosted_CSharp.Server
|
||||||
app.UseBlazorDebugging();
|
app.UseBlazorDebugging();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.UseStaticFiles();
|
||||||
app.UseClientSideBlazorFiles<Client.Startup>();
|
app.UseClientSideBlazorFiles<Client.Startup>();
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
|
||||||
|
|
@ -11,4 +11,17 @@
|
||||||
<Reference Include="Microsoft.AspNetCore.Blazor" />
|
<Reference Include="Microsoft.AspNetCore.Blazor" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<GetCurrentProjectStaticWebAssetsDependsOn>
|
||||||
|
$(GetCurrentProjectStaticWebAssetsDependsOn);
|
||||||
|
_ClearCurrentStaticWebAssetsForReferenceDiscovery
|
||||||
|
</GetCurrentProjectStaticWebAssetsDependsOn>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<Target Name="_ClearCurrentStaticWebAssetsForReferenceDiscovery">
|
||||||
|
<ItemGroup>
|
||||||
|
<StaticWebAsset Remove="@(StaticWebAsset)" Condition="'%(SourceType)' == ''" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Target>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -45,11 +45,6 @@ async function boot(userOptions?: Partial<BlazorOptions>): Promise<void> {
|
||||||
const circuitHandlers: CircuitHandler[] = [new AutoReconnectCircuitHandler(logger)];
|
const circuitHandlers: CircuitHandler[] = [new AutoReconnectCircuitHandler(logger)];
|
||||||
window['Blazor'].circuitHandlers = circuitHandlers;
|
window['Blazor'].circuitHandlers = circuitHandlers;
|
||||||
|
|
||||||
// In the background, start loading the boot config and any embedded resources
|
|
||||||
const embeddedResourcesPromise = fetchBootConfigAsync().then(bootConfig => {
|
|
||||||
return loadEmbeddedResourcesAsync(bootConfig);
|
|
||||||
});
|
|
||||||
|
|
||||||
// pass options.configureSignalR to configure the signalR.HubConnectionBuilder
|
// pass options.configureSignalR to configure the signalR.HubConnectionBuilder
|
||||||
const initialConnection = await initializeConnection(options, circuitHandlers, logger);
|
const initialConnection = await initializeConnection(options, circuitHandlers, logger);
|
||||||
|
|
||||||
|
|
@ -62,9 +57,6 @@ async function boot(userOptions?: Partial<BlazorOptions>): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure any embedded resources have been loaded before starting the app
|
|
||||||
await embeddedResourcesPromise;
|
|
||||||
|
|
||||||
const circuit = await startCircuit(initialConnection);
|
const circuit = await startCircuit(initialConnection);
|
||||||
|
|
||||||
if (!circuit) {
|
if (!circuit) {
|
||||||
|
|
|
||||||
|
|
@ -60,56 +60,7 @@ namespace Microsoft.AspNetCore.Components.Server
|
||||||
// Add our provider
|
// Add our provider
|
||||||
var provider = new ManifestEmbeddedFileProvider(typeof(ConfigureStaticFilesOptions).Assembly);
|
var provider = new ManifestEmbeddedFileProvider(typeof(ConfigureStaticFilesOptions).Assembly);
|
||||||
|
|
||||||
options.FileProvider = new CompositeFileProvider(provider, new ContentReferencesFileProvider(), options.FileProvider);
|
options.FileProvider = new CompositeFileProvider(provider, options.FileProvider);
|
||||||
}
|
|
||||||
|
|
||||||
private class ContentReferencesFileProvider : IFileProvider
|
|
||||||
{
|
|
||||||
byte[] _data = Encoding.UTF8.GetBytes(@"{ ""cssReferences"": [], ""jsReferences"": [] }");
|
|
||||||
|
|
||||||
public IDirectoryContents GetDirectoryContents(string subpath)
|
|
||||||
{
|
|
||||||
return new NotFoundDirectoryContents();
|
|
||||||
}
|
|
||||||
|
|
||||||
public IFileInfo GetFileInfo(string subpath)
|
|
||||||
{
|
|
||||||
if (subpath.Equals("/_framework/blazor.boot.json", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return new MemoryFileInfo(_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new NotFoundFileInfo(subpath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IChangeToken Watch(string filter) => NullChangeToken.Singleton;
|
|
||||||
|
|
||||||
private class MemoryFileInfo : IFileInfo
|
|
||||||
{
|
|
||||||
private readonly byte[] _data;
|
|
||||||
|
|
||||||
public MemoryFileInfo(byte[] data)
|
|
||||||
{
|
|
||||||
_data = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Exists => true;
|
|
||||||
|
|
||||||
public long Length => _data.Length;
|
|
||||||
|
|
||||||
public string PhysicalPath => null;
|
|
||||||
|
|
||||||
public string Name => "blazor.boot.json";
|
|
||||||
|
|
||||||
public DateTimeOffset LastModified => DateTimeOffset.FromUnixTimeSeconds(0);
|
|
||||||
|
|
||||||
public bool IsDirectory => false;
|
|
||||||
|
|
||||||
public Stream CreateReadStream()
|
|
||||||
{
|
|
||||||
return new MemoryStream(_data, writable: false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue