Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support updating the antiforgery token when a boost occurs #16

Merged
merged 3 commits into from May 31, 2023

Conversation

khalidabuhakmeh
Copy link
Owner

@khalidabuhakmeh khalidabuhakmeh commented May 24, 2023

It can be problematic if a token has been used previously.

Note: This updates to HTMX 1.9.2 (the latest as of this commit). #15

… problematic if a token has been used previously.

Note: This updates to HTMX 1.9.5 (latest as of this commit).
@khalidabuhakmeh
Copy link
Owner Author

👋 @AlexZeitler @juchom Could I get a quick review for this PR?

This uses HTMX 1.9.2 to check if a request is boosted. That said, the JS should be written in a way that is a no-op for previous versions.

@juchom
Copy link

juchom commented May 24, 2023

I've made some quick tests, and it works well.

Thanks for this quick fix.

@khalidabuhakmeh
Copy link
Owner Author

I'm not sure I had to replace the meta tag in the DOM, since htmx will use the entity in memory, but I did for the sanity of devs looking at the HTML markup.

We could revisit this, but this approach is decent for now. Waiting for feedback on the PR, but will try and get this out sooner than later.

@AlexZeitler
Copy link
Contributor

@khalidabuhakmeh Sorry, I won't be able to review it before Friday in a way to give reasonable feedback.

@khalidabuhakmeh
Copy link
Owner Author

@AlexZeitler no worries, I believe @juchom can still use the JavaScript here to work around his issue. There's no rush. I just like a second set of eyes on this since folks are actually using it. 😱

@juchom
Copy link

juchom commented May 24, 2023

@AlexZeitler, @khalidabuhakmeh thanks a lot, it will be done, when it's done !

This is very nice from you to fix it that fast 👍

@khalidabuhakmeh
Copy link
Owner Author

khalidabuhakmeh commented May 26, 2023

👋 Hey folks, I've added a new endpoint for the anti-forgery script, which allows for referencing the script in a <script src="" defer></script> tag. This is the better option using modern approaches for JavaScript.

I still left the old approach but left a message about the newer approach.

As a bonus 🎁: If you use this approach, you won't have to worry about registering event handlers multiple times on boost, since the script will only be loaded once per page. I left the JavaScript checks in place to account for the old way, but they are unnecessary using the endpoint approach.

@AlexZeitler
Copy link
Contributor

LGTM

@khalidabuhakmeh
Copy link
Owner Author

It's going in boys and girls!

@khalidabuhakmeh khalidabuhakmeh merged commit d3d6e96 into main May 31, 2023
1 check passed
@khalidabuhakmeh khalidabuhakmeh deleted the htmx-boost-token branch June 2, 2023 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants