leotaku / tower-livereload

Tower middleware to automatically reload your web browser during development
Apache License 2.0
62 stars 7 forks source link

Reload breaks with local dev proxy which returns 502s #12

Closed liamdawson closed 3 months ago

liamdawson commented 4 months ago

I'm using https://github.com/peterldowns/localias for local development. If I attempt to load the app while cargo watch is recompiling and rerunning the app, Caddy (as part of localias) immediately responds with a 502 error.

In the snippet:

    fetch(url, {{ cache: "no-store", signal: controller.signal }})
      .then(() => console.log("[tower-livereload] reload..."))
      .then(() => window.location.reload())
      .catch(() => setTimeout(() => retry(url), {reload_interval}))

the .catch doesn't fire if a HTTP response is received, even though it's a HTTP error 502. This causes the snippet to immediately reload into the 502 error page instead.

I hacked this together as a manually injected snippet which works in my scenario:

        fetch(url, {{ cache: "no-store", signal: controller.signal }})
          .then((resp) => {
            if (!resp.ok) {
              throw new Error("Failed to load endpoint");
            }

            console.log("[tower-livereload] reload...");
            window.location.reload();
          })
          .catch((err) => setTimeout(() => retry(url), {reload_interval}));
leotaku commented 4 months ago

Hi! Thanks for raising this issue, I think this is a good idea. I will implement something like your snippet in the near future.