Giter VIP home page Giter VIP logo

Comments (6)

DreamwareDevelopment avatar DreamwareDevelopment commented on September 17, 2024

Actually, nevermind. JSON doesn't work either when there is some delay added to the server response.

Edit: Just checked, no response sent from the server besides the 500 code is still not triggering the htmx:responseError or htmx:afterRequest events when there is delay introduced via the server. Am I doing something wrong?

document.body.addEventListener('htmx:beforeRequest', function(event) {
    // Find all buttons that have a loading span inside them
    any('button')?.forEach(button => {
      if (button.querySelector('.loading-span')) {
        console.log(`Disabling button from beforeRequest`);
        console.log(button);
        button.disabled = true;
      }
    });
  });

  document.body.addEventListener('htmx:responseError', function(event) {
    console.log(`htmx:responseError`);
    // Re-enable all buttons that have a loading span inside them
    any('button')?.forEach(button => {
      if (button.querySelector('.loading-span')) {
        console.log(`Re-enabling button from responseError`);
        console.log(button);
        button.disabled = false;
      }
    });
  });

  document.body.addEventListener('htmx:afterRequest', function(event) {
    console.log(`htmx:afterRequest`);
    // Re-enable all buttons that have a loading span inside them
    any('button')?.forEach(button => {
      if (button.querySelector('.loading-span')) {
        console.log(`Re-enabling button from afterRequest`);
        console.log(button);
        button.disabled = false;
      }
    });
  });
<form 
        id="bulkUploadForm" 
        class="w-full flex flex-col gap-4" 
        hx-encoding="multipart/form-data"
        hx-post="/s/monitoring/upload"
        hx-target="body"
        hx-swap="outerHTML"
        hx-indicator=".loading-span"
      >

from htmx.

Telroshan avatar Telroshan commented on September 17, 2024

Hey, could you provide a reproducible example (either a JSFiddle/Codepen, or a repo)?
A quick test on this JSFiddle seems to behave as expected, am I missing something?
Note that this code gets 404 errors and not 5xx, though 4xx and 5xx error code should get the same treatment, as they are both part of the same responseHandling default config.

htmx/src/htmx.js

Lines 264 to 268 in bd2dc65

responseHandling: [
{ code: '204', swap: false },
{ code: '[23]..', swap: true },
{ code: '[45]..', swap: false, error: true }
],

from htmx.

DreamwareDevelopment avatar DreamwareDevelopment commented on September 17, 2024

Dang it. My minimal example works fine. I'll have to keep digging into my code to find out where things are going wrong. I'll close this for now.

from htmx.

DreamwareDevelopment avatar DreamwareDevelopment commented on September 17, 2024

Reopening, I reproduced the issue in my minimal example and now I understand what's going wrong. Essentially, I have a form in a modal. That modal has it's own onclick handler on the submit button that starts an animation to fade out the modal. At this same time, the post request is made. However, there is a listener on animationend that removes the modal from the DOM. So, when the request comes back later, the event is processed by HTMX, however because the original node that issued the request is no longer in the DOM, a JS exception is thrown and JS stops working. Could this be handled more gracefully?

from htmx.

DreamwareDevelopment avatar DreamwareDevelopment commented on September 17, 2024

I made a workaround (not in the example repo) that waits to remove the element that triggered the request until there is no htmx-request class present. That unblocks me, but there may still be some improvements that could be made here, so leaving this open.

from htmx.

Telroshan avatar Telroshan commented on September 17, 2024

On a UX aspect, I would suggest waiting for the request to complete before dismissing the modal anyway; what if, for any reason, the request fails, wouldn't you want your modal to stay open with potentially an error message instead of having it dismissed, giving the user the illusion that everything went well even though it didn't?

from htmx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.