diff --git a/src/Smidge.Nuglify/NuglifySourceMapController.cs b/src/Smidge.Nuglify/NuglifySourceMapController.cs index 9d10932..aaba097 100644 --- a/src/Smidge.Nuglify/NuglifySourceMapController.cs +++ b/src/Smidge.Nuglify/NuglifySourceMapController.cs @@ -1,7 +1,8 @@ -using System; +using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; using Smidge.Cache; using Smidge.Models; using Smidge.Options; @@ -19,12 +20,11 @@ public NuglifySourceMapController(ISmidgeFileSystem fileSystem, IBundleManager b _bundleManager = bundleManager; } - public FileResult SourceMap([FromServices] BundleRequestModel bundle) + public ActionResult SourceMap([FromServices] BundleRequestModel bundle) { - if (!_bundleManager.TryGetValue(bundle.FileKey, out _)) + if (!bundle.IsBundleFound) { - //TODO: Throw an exception, this will result in an exception anyways - return null; + return NotFound(); } var sourceMapFile = _fileSystem.CacheFileSystem.GetRequiredFileInfo(bundle.GetSourceMapFilePath()); @@ -43,10 +43,9 @@ public FileResult SourceMap([FromServices] BundleRequestModel bundle) } } - //TODO: Throw an exception, this will result in an exception anyways - return null; + return NotFound(); } } -} \ No newline at end of file +} diff --git a/src/Smidge.Web/Startup.cs b/src/Smidge.Web/Startup.cs index 0ab919a..bf120d4 100644 --- a/src/Smidge.Web/Startup.cs +++ b/src/Smidge.Web/Startup.cs @@ -198,6 +198,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { RequestPath = "/smidge-static" }); + + bundles + .CreateCss("notfound-map-css-bundle", + "~/Css/notFoundMap.min.css" + ); }); app.UseSmidgeNuglify(); diff --git a/src/Smidge.Web/Views/Home/Index.cshtml b/src/Smidge.Web/Views/Home/Index.cshtml index 393d56b..b63f708 100644 --- a/src/Smidge.Web/Views/Home/Index.cshtml +++ b/src/Smidge.Web/Views/Home/Index.cshtml @@ -34,6 +34,7 @@ + @await SmidgeHelper.CssHereAsync("notfound-map-css-bundle", debug: false) diff --git a/src/Smidge.Web/wwwroot/Css/notFoundMap.min.css b/src/Smidge.Web/wwwroot/Css/notFoundMap.min.css new file mode 100644 index 0000000..7133bb1 --- /dev/null +++ b/src/Smidge.Web/wwwroot/Css/notFoundMap.min.css @@ -0,0 +1,2 @@ +@charset "UTF-8"; + /*# sourceMappingURL=notFound.min.css.map */ diff --git a/src/Smidge/Controllers/AddCompressionHeaderAttribute.cs b/src/Smidge/Controllers/AddCompressionHeaderAttribute.cs index f2ca3fb..f69f65d 100644 --- a/src/Smidge/Controllers/AddCompressionHeaderAttribute.cs +++ b/src/Smidge/Controllers/AddCompressionHeaderAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.DependencyInjection; @@ -55,7 +55,7 @@ public void OnActionExecuted(ActionExecutedContext context) if (context.Exception != null) return; //get the model from the items - if (context.HttpContext.Items.TryGetValue(nameof(AddCompressionHeaderAttribute), out var requestModel) && requestModel is RequestModel file) + if (context.HttpContext.Items.TryGetValue(nameof(AddCompressionHeaderAttribute), out var requestModel) && requestModel is RequestModel file && file.IsBundleFound) { var enableCompression = true; @@ -72,4 +72,4 @@ public void OnActionExecuted(ActionExecutedContext context) } } } -} \ No newline at end of file +} diff --git a/src/Smidge/Controllers/AddExpiryHeadersAttribute.cs b/src/Smidge/Controllers/AddExpiryHeadersAttribute.cs index 2929d58..cf08dae 100644 --- a/src/Smidge/Controllers/AddExpiryHeadersAttribute.cs +++ b/src/Smidge/Controllers/AddExpiryHeadersAttribute.cs @@ -50,7 +50,7 @@ public void OnActionExecuted(ActionExecutedContext context) return; //get the model from the items - if (!context.HttpContext.Items.TryGetValue(nameof(AddExpiryHeadersAttribute), out object fileObject) || fileObject is not RequestModel file) + if (!context.HttpContext.Items.TryGetValue(nameof(AddExpiryHeadersAttribute), out object fileObject) || fileObject is not RequestModel file || !file.IsBundleFound) return; var enableETag = true; diff --git a/src/Smidge/Controllers/CheckNotModifiedAttribute.cs b/src/Smidge/Controllers/CheckNotModifiedAttribute.cs index 8229ea5..8fb90eb 100644 --- a/src/Smidge/Controllers/CheckNotModifiedAttribute.cs +++ b/src/Smidge/Controllers/CheckNotModifiedAttribute.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Smidge.Models; using System; @@ -51,7 +51,7 @@ public void OnActionExecuted(ActionExecutedContext context) if (context.Exception != null) return; //get the model from the items - if (context.HttpContext.Items.TryGetValue(nameof(CheckNotModifiedAttribute), out var requestModel) && requestModel is RequestModel file) + if (context.HttpContext.Items.TryGetValue(nameof(CheckNotModifiedAttribute), out var requestModel) && requestModel is RequestModel file && file.IsBundleFound) { //Don't execute when the request is in Debug if (file.Debug) @@ -74,4 +74,4 @@ private static void ReturnNotModified(ActionExecutedContext context) } } } -} \ No newline at end of file +} diff --git a/src/Smidge/Controllers/CompositeFileCacheFilterAttribute.cs b/src/Smidge/Controllers/CompositeFileCacheFilterAttribute.cs index 674f7d0..2d3e5ec 100644 --- a/src/Smidge/Controllers/CompositeFileCacheFilterAttribute.cs +++ b/src/Smidge/Controllers/CompositeFileCacheFilterAttribute.cs @@ -68,7 +68,7 @@ public void OnActionExecuting(ActionExecutingContext context) if (context.ActionArguments.Count == 0) return; var firstArg = context.ActionArguments.First().Value; - if (firstArg is RequestModel file) + if (firstArg is RequestModel file && file.IsBundleFound) { var cacheBusterValue = file.ParsedPath.CacheBusterValue; diff --git a/src/Smidge/Controllers/SmidgeController.cs b/src/Smidge/Controllers/SmidgeController.cs index 3df51b0..2f76f03 100644 --- a/src/Smidge/Controllers/SmidgeController.cs +++ b/src/Smidge/Controllers/SmidgeController.cs @@ -67,7 +67,7 @@ public class SmidgeController : Controller public async Task Bundle( [FromServices] BundleRequestModel bundleModel) { - if (!_bundleManager.TryGetValue(bundleModel.FileKey, out Bundle foundBundle)) + if (!bundleModel.IsBundleFound || !_bundleManager.TryGetValue(bundleModel.FileKey, out Bundle foundBundle)) { return NotFound(); } @@ -160,7 +160,7 @@ public class SmidgeController : Controller public async Task Composite( [FromServices] CompositeFileModel file) { - if (!file.ParsedPath.Names.Any()) + if (!file.IsBundleFound || !file.ParsedPath.Names.Any()) { return NotFound(); } diff --git a/src/Smidge/Models/BundleRequestModel.cs b/src/Smidge/Models/BundleRequestModel.cs index 337144f..c5f3772 100644 --- a/src/Smidge/Models/BundleRequestModel.cs +++ b/src/Smidge/Models/BundleRequestModel.cs @@ -1,4 +1,4 @@ -using Smidge.CompositeFiles; +using Smidge.CompositeFiles; using System; using System.Linq; using Microsoft.AspNetCore.Mvc.Infrastructure; @@ -18,17 +18,25 @@ public BundleRequestModel(IUrlManager urlManager, IActionContextAccessor accesso // In reality we'll need to do that anyways if we want to support load balancing! // https://github.com/Shazwazza/Smidge/issues/17 + if (!IsBundleFound) + { + return; + } if (!ParsedPath.Names.Any()) { - throw new InvalidOperationException("The bundle route value does not contain a bundle name"); + IsBundleFound = false; + + return; } FileKey = ParsedPath.Names.Single(); if (!bundleManager.TryGetValue(FileKey, out Bundle bundle)) { - throw new InvalidOperationException("No bundle found with key " + FileKey); + IsBundleFound = false; + + return; } Bundle = bundle; } @@ -36,4 +44,4 @@ public BundleRequestModel(IUrlManager urlManager, IActionContextAccessor accesso public Bundle Bundle { get; } public override string FileKey { get; } } -} \ No newline at end of file +} diff --git a/src/Smidge/Models/CompositeFileModel.cs b/src/Smidge/Models/CompositeFileModel.cs index d800fc9..1d850fe 100644 --- a/src/Smidge/Models/CompositeFileModel.cs +++ b/src/Smidge/Models/CompositeFileModel.cs @@ -1,4 +1,4 @@ -using Smidge.CompositeFiles; +using Smidge.CompositeFiles; using Microsoft.AspNetCore.Mvc.Infrastructure; using Smidge.Hashing; @@ -10,10 +10,14 @@ public class CompositeFileModel : RequestModel public CompositeFileModel(IHasher hasher, IUrlManager urlManager, IActionContextAccessor accessor, IRequestHelper requestHelper) : base("file", urlManager, accessor, requestHelper) { + if (!IsBundleFound) + { + return; + } //Creates a single hash of the full url (which can include many files) FileKey = hasher.Hash(string.Join(".", ParsedPath.Names)); } public override string FileKey { get; } } -} \ No newline at end of file +} diff --git a/src/Smidge/Models/RequestModel.cs b/src/Smidge/Models/RequestModel.cs index 9704611..d439783 100644 --- a/src/Smidge/Models/RequestModel.cs +++ b/src/Smidge/Models/RequestModel.cs @@ -1,4 +1,4 @@ -using Smidge.CompositeFiles; +using Smidge.CompositeFiles; using System; using Microsoft.AspNetCore.Mvc.Infrastructure; @@ -25,7 +25,10 @@ protected RequestModel(string valueName, IUrlManager urlManager, IActionContextA ParsedPath = urlManager.ParsePath(bundleId); if (ParsedPath == null) - throw new InvalidOperationException($"Could not parse {bundleId} as a valid smidge path"); + { + IsBundleFound = false; + return; + } Debug = ParsedPath.Debug; @@ -61,5 +64,7 @@ protected RequestModel(string valueName, IUrlManager urlManager, IActionContextA public string Mime { get; private set; } public DateTime LastFileWriteTime { get; set; } + + public bool IsBundleFound { get; set; } = true; } -} \ No newline at end of file +} diff --git a/src/Smidge/Properties/launchSettings.json b/src/Smidge/Properties/launchSettings.json new file mode 100644 index 0000000..ec82ba1 --- /dev/null +++ b/src/Smidge/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "Smidge": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:62980;http://localhost:62981" + } + } +} \ No newline at end of file