mirror of
https://github.com/svemagie/blog-eleventy-indiekit.git
synced 2026-04-02 16:44:56 +02:00
fix: use Alpine.data() for comments component registration
Convert commentsSection from a global function to Alpine.data() registration via the alpine:init event. This is the proper Alpine.js pattern for reusable components — the component is registered in Alpine's internal registry before DOM processing begins, eliminating script loading order issues. Reverts the hacky approach of moving the script tag to <head>.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
/**
|
||||
* Client-side comments component (Alpine.js)
|
||||
* Handles IndieAuth flow, comment submission, and display
|
||||
*
|
||||
* Registered via Alpine.data() so the component is available
|
||||
* regardless of script loading order.
|
||||
*/
|
||||
|
||||
function commentsSection(targetUrl) {
|
||||
return {
|
||||
document.addEventListener("alpine:init", () => {
|
||||
Alpine.data("commentsSection", (targetUrl) => ({
|
||||
targetUrl,
|
||||
user: null,
|
||||
meUrl: "",
|
||||
@@ -42,7 +45,11 @@ function commentsSection(targetUrl) {
|
||||
const authError = params.get("auth_error");
|
||||
if (authError) {
|
||||
this.showStatus(`Authentication failed: ${authError}`, "error");
|
||||
window.history.replaceState({}, "", window.location.pathname + "#comments");
|
||||
window.history.replaceState(
|
||||
{},
|
||||
"",
|
||||
window.location.pathname + "#comments",
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -139,5 +146,5 @@ function commentsSection(targetUrl) {
|
||||
this.statusMessage = "";
|
||||
}, 5000);
|
||||
},
|
||||
};
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user