I m having an issue now with a custom
# avo-2
n
I'm having an issue now with a custom stimulus controller.. works locally and not in production, I get a 404 on loading the asset. ๐Ÿงต
importmap.rb
Copy code
ruby
# Pin npm packages by running ./bin/importmap

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"

# Avo custom JS entrypoint
pin "avo.custom", preload: true
avo.custom.js
Copy code
js
import ProductResourceController from './controllers/product_resource_controller'

// Hook into the stimulus instance provided by Avo
const application = window.Stimulus
application.register('product-resource', ProductResourceController)

// eslint-disable-next-line no-console
console.log('Hi from Avo custom JS ๐Ÿ‘‹')
avo.custom-7fd8cfcdfff885e9d874f43f614549257254a1fc419bf90a92a0790beb76f174.js:1 GET https://my.domain.com/assets/controllers/product_resource_controller net::ERR_ABORTED 404
also the ๐Ÿ‘‹ does not print
The file does appear in
./bin/importmap json
results locally
l
Hello @narrow-magazine-18722, what kind of license are you using? Community or pro?
n
Trial of Pro
l
Are you experiencing other errors with assets or only this one?
n
So I ran
bin/importmap json
on production as well, and the two missing items compared to my local run are
avo.custom
and
controllers/product_resource_controller
Copy code
W, [2023-07-04T02:03:06.133880 #59546]  WARN -- : Importmap skipped missing path: avo.custom.js
W, [2023-07-04T02:03:06.133985 #59546]  WARN -- : Importmap skipped missing path: controllers/product_resource_controller.js
I should mention this is avo
2.35.0
importmap-rails (1.1.5)
rails (7.0.4.3)
l
n
Yep I ran the
avo:js:install
generator and added my controller as above
l
For what i see on the logs I think there are 2 possibilities: 1 -
avo.custom.js
is on the wrong dir 2 -
avo.custom.js
is on the right dir but the configured path is wrong I'm not the best person to help you with this issue, maybe @lemon-wall-20836 can understand this better
n
I appreciate it! My first time working with importmaps... I heard this might change in avo 3, and that maybe everyone hates importmaps
l
hey dan, the importmap integration should just work
let me check it out
n
I can see in the network tab that it does grab the precompiled
avo.custom
asset correctly (which makes sense because that is the only ref to the controller) I tried bumping
Rails.application.config.assets.version
for fun to no effect
l
so the file grabbed you see in the network tab is the right one?
you can see your JS there?
n
Yeah, the
avo.custom
file is correct
l
and where do you get the 404?
n
When it loads the custom controller from the
import
statement -- it's trying to load just
assets/controllers/product_resource_controller
and not the version with the precompiled hash in the filename
That file with the hash in the filename does exist, I can load it manually
l
are you using the
eagerLoadControllersFrom
method?

https://cdn.discordapp.com/attachments/1125712633320980551/1125721785787240550/CleanShot_2023-07-04_at_12.36.23.pngโ–พ

n
In
index.js
Copy code
javascript
// Import and register all your controllers from the importmap under controllers/*

import { application } from "controllers/application"

// Eager load all controllers defined in the import map under controllers/**/*_controller
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)

// Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
// import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
// lazyLoadControllersFrom("controllers", application)
The controller is inside
./controllers
l
how does your
importmap.rb
file looks like?

https://cdn.discordapp.com/attachments/1125712633320980551/1125722751630589972/CleanShot_2023-07-04_at_12.40.14.pngโ–พ

I think you should add this last line too
pin_all_from "app/javascript/avo_controllers", under: "avo_controllers"
so it's available on the app
can you try that please
n
๐Ÿค” yeah I can try that, should I move the controller to
avo_controllers
?
l
not necesarily
where do you have them?
under
controllers
?
n
yeah, and I have those lines except the last one
l
hmmm
let me try it the same way and deploy it somewhere
n
Copy code
diff
-import ProductResourceController from './controllers/product_resource_controller'
+import { default as ProductResourceController } from 'controllers/product_resource_controller'
I'm trying this in the meantime
l
and do you have
import "controllers"
in your
avo.custom.js
file?
n
No, just this
Copy code
javascript
import { default as ProductResourceController } from 'controllers/product_resource_controller'

// Hook into the stimulus instance provided by Avo
const application = window.Stimulus
application.register('product-resource', ProductResourceController)

// eslint-disable-next-line no-console
console.log('Hi from Avo custom JS ๐Ÿ‘‹')
The diff above is that first line
l
ok. let me try this
it works on my end with just this

https://cdn.discordapp.com/attachments/1125712633320980551/1125724755262197781/CleanShot_2023-07-04_at_12.48.03.pngโ–พ

n
That change I made actually worked too
l
can you show me
product_resource_controller.js
?
n
maybe the
./controllers/
vs
controllers/
?
l
yup
TBH,
controllers
should work too because that's how the dir is defined in importmap.rb
n
course_resource_controller
rather
l
that's interesting the
export default class extends Controller
statement usually is imported as
import NAME from 'controllers/NAME.js
as a default import
so does it work in prod?
n
with
import { default as ProductResourceController } from 'controllers/product_resource_controller'
yes
l
๐Ÿคทโ€โ™‚๏ธ
are you able to use it like that?
n
I'm betting it was the
./
, I'll try
import ProductResourceController from 'controllers/product_resource_controller'
next time I deploy
yeah! Thank you for helping me with that!
l
yup! could be
for sure! anytime
2 Views