-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from khalidabuhakmeh/htmx-boost-token
Support updating the antiforgery token when a boost occurs
- Loading branch information
Showing
6 changed files
with
140 additions
and
39 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,37 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace Htmx.TagHelpers; | ||
|
||
public static class HtmxAntiforgeryScriptEndpoints | ||
{ | ||
/// <summary> | ||
/// The path to the antiforgery script that is used from HTML | ||
/// </summary> | ||
public static string Path { get; private set; } = "_htmx/antiforgery.js"; | ||
|
||
/// <summary> | ||
/// Register an endpoint that responds with the HTMX antiforgery script.<br/> | ||
/// IMPORTANT: Remember to add the following script tag to your _Layout.cshtml or Razor view: | ||
/// <![CDATA[ | ||
/// <script src="@HtmxAntiforgeryScriptEndpoints.Path" defer></script> | ||
/// ]]> | ||
/// </summary> | ||
/// <param name="builder">Endpoint builder</param> | ||
/// <param name="path">The path to the antiforgery script</param> | ||
/// <returns>The registered endpoint (Use <seealso cref="Path"/> to reference endpoint)</returns> | ||
public static IEndpointConventionBuilder MapHtmxAntiforgeryScript( | ||
this IEndpointRouteBuilder builder, | ||
string? path = null) | ||
{ | ||
// set Path globally for access | ||
Path = path ?? Path; | ||
|
||
return builder.MapGet(Path, async ctx => | ||
{ | ||
ctx.Response.ContentType = "text/javascript"; | ||
await ctx.Response.WriteAsync(HtmxSnippets.AntiforgeryJavaScript); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,26 +3,31 @@ | |
<head> | ||
<meta charset="utf-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<meta name="htmx-config" | ||
historyCacheSize="20" | ||
indicatorClass="htmx-indicator" | ||
includeAspNetAntiforgeryToken="true" | ||
/> | ||
<meta | ||
name="htmx-config" | ||
historyCacheSize="20" | ||
indicatorClass="htmx-indicator" | ||
includeAspNetAntiforgeryToken="true"/> | ||
<title>@ViewData["Title"] - Htmx.Sample</title> | ||
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css"/> | ||
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/> | ||
<script src="~/lib/jquery/dist/jquery.min.js" defer></script> | ||
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js" defer></script> | ||
<script src="https://unpkg.com/htmx.org@@1.9.2" defer></script> | ||
<script src="@HtmxAntiforgeryScriptEndpoints.Path" defer></script> | ||
</head> | ||
<body hx-boost="true"> | ||
<body> | ||
<header> | ||
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> | ||
<nav | ||
class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> | ||
<div class="container"> | ||
<a class="navbar-brand" asp-area="" asp-page="/Index">Htmx.Sample</a> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent" | ||
aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> | ||
<ul class="navbar-nav flex-grow-1"> | ||
<ul hx-boost="true" class="navbar-nav flex-grow-1"> | ||
<li class="nav-item"> | ||
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a> | ||
</li> | ||
|
@@ -46,11 +51,6 @@ | |
</div> | ||
</footer> | ||
|
||
<script src="~/lib/jquery/dist/jquery.min.js"></script> | ||
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> | ||
<script src="https://unpkg.com/[email protected]"></script> | ||
@Html.HtmxAntiforgeryScript() | ||
|
||
@await RenderSectionAsync("Scripts", required: false) | ||
</body> | ||
</html> |
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