Skip to content

Commit

Permalink
Merge pull request #190 from jet1992/NotFoundPage
Browse files Browse the repository at this point in the history
Not found page
  • Loading branch information
Shazwazza committed Apr 2, 2024
2 parents 62fe08c + f0765d5 commit d19268a
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 27 deletions.
15 changes: 7 additions & 8 deletions src/Smidge.Nuglify/NuglifySourceMapController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
Expand All @@ -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();
}


}
}
}
5 changes: 5 additions & 0 deletions src/Smidge.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/Smidge.Web/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" />
<link rel="stylesheet" href="inline-css-bundle" />
<link rel="stylesheet" href="test-bundle-4" onload="window.alert('hello')" />
@await SmidgeHelper.CssHereAsync("notfound-map-css-bundle", debug: false)
</head>
<body>

Expand Down
2 changes: 2 additions & 0 deletions src/Smidge.Web/wwwroot/Css/notFoundMap.min.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@charset "UTF-8";
/*# sourceMappingURL=notFound.min.css.map */
6 changes: 3 additions & 3 deletions src/Smidge/Controllers/AddCompressionHeaderAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -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;

Expand All @@ -72,4 +72,4 @@ public void OnActionExecuted(ActionExecutedContext context)
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/Smidge/Controllers/AddExpiryHeadersAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Smidge/Controllers/CheckNotModifiedAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Smidge.Models;
using System;
Expand Down Expand Up @@ -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)
Expand All @@ -74,4 +74,4 @@ private static void ReturnNotModified(ActionExecutedContext context)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Smidge/Controllers/SmidgeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class SmidgeController : Controller
public async Task<IActionResult> Bundle(
[FromServices] BundleRequestModel bundleModel)
{
if (!_bundleManager.TryGetValue(bundleModel.FileKey, out Bundle foundBundle))
if (!bundleModel.IsBundleFound || !_bundleManager.TryGetValue(bundleModel.FileKey, out Bundle foundBundle))
{
return NotFound();
}
Expand Down Expand Up @@ -160,7 +160,7 @@ public class SmidgeController : Controller
public async Task<IActionResult> Composite(
[FromServices] CompositeFileModel file)
{
if (!file.ParsedPath.Names.Any())
if (!file.IsBundleFound || !file.ParsedPath.Names.Any())
{
return NotFound();
}
Expand Down
16 changes: 12 additions & 4 deletions src/Smidge/Models/BundleRequestModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Smidge.CompositeFiles;
using Smidge.CompositeFiles;
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Infrastructure;
Expand All @@ -18,22 +18,30 @@ 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;
}

public Bundle Bundle { get; }
public override string FileKey { get; }
}
}
}
8 changes: 6 additions & 2 deletions src/Smidge/Models/CompositeFileModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Smidge.CompositeFiles;
using Smidge.CompositeFiles;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Smidge.Hashing;

Expand All @@ -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; }
}
}
}
11 changes: 8 additions & 3 deletions src/Smidge/Models/RequestModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Smidge.CompositeFiles;
using Smidge.CompositeFiles;
using System;
using Microsoft.AspNetCore.Mvc.Infrastructure;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
}
}
12 changes: 12 additions & 0 deletions src/Smidge/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"Smidge": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:62980;http://localhost:62981"
}
}
}

0 comments on commit d19268a

Please sign in to comment.