I've upgraded a legacy rails app to Rails 6.1.3.2 where I'm also using Hotwire and Stimulus. Because this was a legacy app, I've decided to go straight from asset pipeline to including the importmap-rails gem.
The issue I'm running into is that when including <div data-controller="hello"></div> via hello_controller.js to test that Stimulus is working, I find that on page refresh, the controller is not always loading (I'm not seeing Hello World on the page occasionally). Here's my setup -
Gemfile
gem 'importmap-rails', "~> 1.1.5"
gem 'stimulus-rails', "~> 1.1.0"
gem 'turbo-rails'
importmap.rb
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
javascript/application.js
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
javascript/controllers/application.js
import { Application } from "@hotwired/stimulus"
const application = Application.start()
// Configure Stimulus development experience
application.debug = false
window.Stimulus = application
export { application }
javascript/controllers/index.js
// This file is auto-generated by ./bin/rails stimulus:manifest:update
// Run that command whenever you add a new controller or create them with
// ./bin/rails generate stimulus controllerName
import { application } from "controllers/application"
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)
manifest.js (some of these are legacy before I intro'd import map)
//= link_tree ../images
//= link_tree ../fonts
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
//= link_tree ../javascripts/admin
//= link_tree ../javascripts
//= link_tree ../javascripts/devise/ .js
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
layouts/application.html.erb
<head>
<%= render :partial => 'shared/favicon' %>
<%# stimulus_include_tags %>
<%= javascript_importmap_tags %>
<%= javascript_include_tag "turbo", type: "module-shim" %>
<%= yield :head %>
</head>
<body>
<div id="wrapper">
<div data-controller="hello"></div>
</div>
</body>
Running ./bin/importmap json returns the following;
./bin/importmap json
{
"imports": {
"application": "/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js",
"@hotwired/turbo-rails": "/assets/turbo.min-305f0d205866ac9fc3667580728220ae0c3b499e5f15df7c4daaeee4d03b5ac1.js",
"@hotwired/stimulus": "/assets/stimulus.min-b8a9738499c7a8362910cd545375417370d72a9776fb4e766df7671484e2beb7.js",
"@hotwired/stimulus-loading": "/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js",
"controllers/dropdown_controller": "/assets/controllers/dropdown_controller-fd4f1bf22de2dbf743c9051ba338d61a1fb13147b55fe838504b78a260521b9e.js",
"controllers/hello_controller": "/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js",
"controllers/modal_controller": "/assets/controllers/modal_controller-ef43a7d5af349eeeb3a040a49503341ef56515c53b447a9f75b3c8b9049b77de.js",
"controllers": "/assets/controllers/index-2a439010cf426766c110f1d1d2cce7a57524980516bcc71253f244766cff1e75.js",
"controllers/tabs_controller": "/assets/controllers/tabs_controller-62b5446ded99b1c85952eadd043447a7569da4adcfe381745113a9f7e356e4e3.js",
"controllers/application": "/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js"
}
}
Any ideas what might be happening? Prior to introducing import map, I was already running Stimulus + Hotwire where I didn't have this issue, controllers would load fine on page load and refresh.