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
[Question] Option to override xhr request with call to my SDK #2536
Comments
Second alternative, I considered using a service worker which implements SDK interactions |
Third alternative, I tried to override XHR with Proxy
|
Hey, if you're making a call to a black box SDK, that apparently doesn't return an HTML response, will probably not use any of the htmx features such as response headers, you might be better off simply not using htmx in this case and process the response yourself, from a standard form's |
I believe htmx allows me to write more clean code Look at this example of using my SDK (backend) with lit framework:
With htmx it can be more concise just like this:
|
Well, looking at those exact snippets, it would seem that the htmx version is cleaner & more concise. Plus, you oppose it with a framework's syntax that has a lot of boilerplate code compared to the actual logic, so it feels like an unfair comparison here. With vanilla JS, it would be like this <main>
<form id="myForm">
<label for="name">Enter your name: </label>
<input id="name" alt="Name" type="text" />
<button type="submit">Click Me!</button>
</form>
<section id="greeting"></section>
</main> Note that this HTML didn't change much from your second more-concise approach, I just removed the hx attributes as we won't use it (not use htmx at all) and added an ID to the form to retrieve it easily document.getElementById("myForm").addEventListener("submit", async function(event) {
event.preventDefault();
const name = document.getElementById('name').value;
const greeting = await backend.greet(name);
document.getElementById("greeting").innerHTML = greeting
}) Now, would that still be a "less concise" approach than what you would have to do to mock this SDK behavior and make it act as it was a XHR request? I would really doubt so. |
Actually I succeed on this using Service Worker as in example of POST request in htmx form below: It's being intercepted by SW here which handles the request and responds with HTML as a proper backend would do But in this scenario, since we are building dapp, we try to avoid using any centralized server, yet by moving the backend with SSR to the edge in SW, we can still enjoy all benefits of proper hypertext with HTMX. |
I understand htmx works best with REST but in my case there is black box SDK in between, therefore I am trying to override beforeRequest to handle form submission with my SDK, wrap it with HTML response and then proceed as normal, can it work?
The text was updated successfully, but these errors were encountered: