digitallyinduced / ihp

🔥 The fastest way to build type safe web apps. IHP is a new batteries-included web framework optimized for longterm productivity and programmer happiness
https://ihp.digitallyinduced.com/
MIT License
4.9k stars 194 forks source link

Detect JS failures on asset version changes #1177

Open neongreen opened 2 years ago

neongreen commented 2 years ago

Scenario:

In this scenario, JS asset versions will change and Turbolinks will attempt to load the new versions of all assets without unloading the old versions:

image

This will lead to silent errors like those:

image

Any behavior relying on custom JS will silently break.

I propose detecting this with window.onerror and doing a full page reload or.. something. Not sure what exactly should be done.

neongreen commented 2 years ago

My workaround has been to ask the user to reload the page manually:

// app.js

$(window).on("error", function(evt) {
    var e = evt.originalEvent; // get the javascript event
    if (e.message) { 
        alert("JavaScript error:\n    " + e.message + "\n" +
              "Line:\n    " + e.lineno + "\n" +
              "File:\n    " + e.filename + "\n\n" +
              "Please reload the page!");
    } else {
        alert("JavaScript error:\n    " + e.type + "\n" +
              "Element:\n    " + (e.srcElement || e.target) + "\n\n" +
              "Please reload the page!");
    }
});