When using HTMX boosts with ASP.NET Core, anti-forgery tokens stored in the <head> element don't automatically refresh after a boosted page navigation, and event handlers can be re-registered unintentionally. The post walks through two approaches to fix this: wrapping body scripts with a guard check to prevent duplicate event handler registration, and using a deferred script served from a dedicated ASP.NET Core endpoint placed in the <head> to keep anti-forgery tokens current after each boost. The deferred approach is preferred because HTMX boosts skip reloading <head> elements, avoiding the re-registration problem entirely while enabling response caching on the script endpoint.

4m read timeFrom khalidabuhakmeh.com
Post cover image
Table of contents
What is HTMX and What’s a Boost?Scripts Within The Body TagAdd Script to Head and DeferConclusion