|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using Microsoft.AspNetCore.Builder; |
| 5 | +using Microsoft.AspNetCore.SpaServices.StaticFiles; |
| 6 | +using Microsoft.Extensions.FileProviders; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | +using System; |
| 9 | + |
| 10 | +namespace Microsoft.Extensions.DependencyInjection |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Extension methods for configuring an application to serve static files for a |
| 14 | + /// Single Page Application (SPA). |
| 15 | + /// </summary> |
| 16 | + public static class SpaStaticFilesExtensions |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Registers an <see cref="ISpaStaticFileProvider"/> service that can provide static |
| 20 | + /// files to be served for a Single Page Application (SPA). |
| 21 | + /// </summary> |
| 22 | + /// <param name="services">The <see cref="IServiceCollection"/>.</param> |
| 23 | + /// <param name="configuration">If specified, this callback will be invoked to set additional configuration options.</param> |
| 24 | + public static void AddSpaStaticFiles( |
| 25 | + this IServiceCollection services, |
| 26 | + Action<SpaStaticFilesOptions> configuration = null) |
| 27 | + { |
| 28 | + services.AddSingleton<ISpaStaticFileProvider>(serviceProvider => |
| 29 | + { |
| 30 | + // Use the options configured in DI (or blank if none was configured) |
| 31 | + var optionsProvider = serviceProvider.GetService<IOptions<SpaStaticFilesOptions>>(); |
| 32 | + var options = optionsProvider.Value; |
| 33 | + |
| 34 | + // Allow the developer to perform further configuration |
| 35 | + configuration?.Invoke(options); |
| 36 | + |
| 37 | + if (string.IsNullOrEmpty(options.RootPath)) |
| 38 | + { |
| 39 | + throw new InvalidOperationException($"No {nameof(SpaStaticFilesOptions.RootPath)} " + |
| 40 | + $"was set on the {nameof(SpaStaticFilesOptions)}."); |
| 41 | + } |
| 42 | + |
| 43 | + return new DefaultSpaStaticFileProvider(serviceProvider, options); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Configures the application to serve static files for a Single Page Application (SPA). |
| 49 | + /// The files will be located using the registered <see cref="ISpaStaticFileProvider"/> service. |
| 50 | + /// </summary> |
| 51 | + /// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param> |
| 52 | + public static void UseSpaStaticFiles(this IApplicationBuilder applicationBuilder) |
| 53 | + { |
| 54 | + if (applicationBuilder == null) |
| 55 | + { |
| 56 | + throw new ArgumentNullException(nameof(applicationBuilder)); |
| 57 | + } |
| 58 | + |
| 59 | + UseSpaStaticFilesInternal(applicationBuilder, |
| 60 | + overrideFileProvider: null, |
| 61 | + allowFallbackOnServingWebRootFiles: false); |
| 62 | + } |
| 63 | + |
| 64 | + internal static void UseSpaStaticFilesInternal( |
| 65 | + this IApplicationBuilder app, |
| 66 | + IFileProvider overrideFileProvider, |
| 67 | + bool allowFallbackOnServingWebRootFiles) |
| 68 | + { |
| 69 | + var shouldServeStaticFiles = ShouldServeStaticFiles( |
| 70 | + app, |
| 71 | + overrideFileProvider, |
| 72 | + allowFallbackOnServingWebRootFiles, |
| 73 | + out var fileProviderOrDefault); |
| 74 | + |
| 75 | + if (shouldServeStaticFiles) |
| 76 | + { |
| 77 | + app.UseStaticFiles(new StaticFileOptions |
| 78 | + { |
| 79 | + FileProvider = fileProviderOrDefault |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static bool ShouldServeStaticFiles( |
| 85 | + IApplicationBuilder app, |
| 86 | + IFileProvider overrideFileProvider, |
| 87 | + bool allowFallbackOnServingWebRootFiles, |
| 88 | + out IFileProvider fileProviderOrDefault) |
| 89 | + { |
| 90 | + if (overrideFileProvider != null) |
| 91 | + { |
| 92 | + // If the file provider was explicitly supplied, that takes precedence over any other |
| 93 | + // configured file provider. This is most useful if the application hosts multiple SPAs |
| 94 | + // (via multiple calls to UseSpa()), so each needs to serve its own separate static files |
| 95 | + // instead of using AddSpaStaticFiles/UseSpaStaticFiles. |
| 96 | + fileProviderOrDefault = overrideFileProvider; |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + var spaStaticFilesService = app.ApplicationServices.GetService<ISpaStaticFileProvider>(); |
| 101 | + if (spaStaticFilesService != null) |
| 102 | + { |
| 103 | + // If an ISpaStaticFileProvider was configured but it says no IFileProvider is available |
| 104 | + // (i.e., it supplies 'null'), this implies we should not serve any static files. This |
| 105 | + // is typically the case in development when SPA static files are being served from a |
| 106 | + // SPA development server (e.g., Angular CLI or create-react-app), in which case no |
| 107 | + // directory of prebuilt files will exist on disk. |
| 108 | + fileProviderOrDefault = spaStaticFilesService.FileProvider; |
| 109 | + return fileProviderOrDefault != null; |
| 110 | + } |
| 111 | + else if (!allowFallbackOnServingWebRootFiles) |
| 112 | + { |
| 113 | + throw new InvalidOperationException($"To use {nameof(UseSpaStaticFiles)}, you must " + |
| 114 | + $"first register an {nameof(ISpaStaticFileProvider)} in the service provider, typically " + |
| 115 | + $"by calling services.{nameof(AddSpaStaticFiles)}."); |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + // Fall back on serving wwwroot |
| 120 | + fileProviderOrDefault = null; |
| 121 | + return true; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments