hotwired / stimulus-rails

Use Stimulus in your Ruby on Rails app
https://stimulus.hotwired.dev
MIT License
637 stars 91 forks source link

Stimulus broken in Firefox 115+ with importmap-rails #121

Closed brucew closed 1 year ago

brucew commented 1 year ago

I've been using stimulus-rails with importmap-rails in a Rails 6.1 project for months. No recent changes in our Stimulus implementation or controllers. We are selectively importing the Stimulus related modules using javascript_import_module_tag.

Recently our support team noticed that the Stimulus features were not working in Firefox (Chrome/Edge/Safari all fine). There is a console error message in Firefox 115.0.2 (latest release) that is not present in other browsers: Uncaught TypeError: The specifier “@hotwired/stimulus” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “../” or “/”. This error message is not present and Stimulus features work in Firefox 114.0.2. We are also importing turbo-rails using javascript_import_module_tag and the Turbo features work in Firefox 115.0.2 with no error message in the console related to turbo-rails.

The error is thrown in the stimulus-loading module at: https://github.com/hotwired/stimulus-rails/blob/70998f6e4bd239a17461e049a9aa81ba73ca73bf/app/assets/javascripts/stimulus-loading.js#L2 Commenting out this line fixes Stimulus features and the console does not show the error. Also Chrome appears unaffected by this change.

In addition I tested Firefox 107.0.1, the latest Firefox that does not support import maps and so should use es-module-shim. With or without the above fix, the console shows the similar error: Uncaught TypeError: Error resolving module specifier “@hotwired/stimulus”. Relative module specifiers must start with “./”, “../” or “/”. However Stimulus features work fine either way.

marcoroth commented 1 year ago

Hey @brucew, is this something different then the regular error you get when using Importmaps in a browser which doesn't support it natively?

Usually the shim can't get rid of that error message, see: https://github.com/guybedford/es-module-shims#polyfill-explainer

Technically, if everything works fine either way, you should be able to just ignore that error message.

brucew commented 1 year ago

@marcoroth Yes. In Firefox 115 (and 116 beta) the Stimulus features of our website are not functional.

marcoroth commented 1 year ago

@brucew I think this is an issue in firefox itself, see: https://github.com/hotwired/turbo-rails/issues/484

A fix for this will ship in Firefox 116, which is currently scheduled to be released on August 1st. The fix is already included in the current Firefox Beta (116.0b6) and Firefox Nightly.

brucew commented 1 year ago

@marcoroth Confirmed Firefox 116.0b6 does not have this issue. Thanks!

Since my Turbo (turbo-rails) did not break and the bug was present in Firefox 116.0b4, I figured it was a stimulus-rails issue.