Import Maps not using precompiled JavaScript assets in pipeline in production environment only (Rails 7)

Viewed 273

Anyone managed to wrap their head around importmaps in Rails 7? I have a headscratcher.

If I run ./bin/importmap json in development, I get this (as expected):

{
  "imports": {
    "application": "/assets/application-a7fd3fc58be844f89656edec1ec73e18f9ab627e54b2aea67a97aad4613b6305.js",
    "@hotwired/turbo-rails": "/assets/turbo.min-96cbf52c71021ba210235aaeec4720012d2c1df7d2dab3770cfa49eea3bb09da.js",
    "@hotwired/stimulus": "/assets/stimulus.min-900648768bd96f3faeba359cf33c1bd01ca424ca4d2d05f36a5d8345112ae93c.js",
    "@hotwired/stimulus-loading": "/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js",
    "stimulus": "https://ga.jspm.io/npm:stimulus@2.0.0/dist/stimulus.umd.js",
    "stimulus-use": "https://ga.jspm.io/npm:stimulus-use@0.41.0/dist/index.js",
    "hotkeys-js": "https://ga.jspm.io/npm:hotkeys-js@3.8.8/dist/hotkeys.esm.js",
    "trix": "/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js",
    "@rails/actiontext": "/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js"
  }
}

...and all my JavaScript works fine.

If, however, I run the same command in production, I get the following:

{
  "imports": {
    "application": "/application.js",
    "@hotwired/turbo-rails": "/turbo.min.js",
    "@hotwired/stimulus": "/stimulus.min.js",
    "@hotwired/stimulus-loading": "/stimulus-loading.js",
    "stimulus": "https://ga.jspm.io/npm:stimulus@2.0.0/dist/stimulus.umd.js",
    "stimulus-use": "https://ga.jspm.io/npm:stimulus-use@0.41.0/dist/index.js",
    "hotkeys-js": "https://ga.jspm.io/npm:hotkeys-js@3.8.8/dist/hotkeys.esm.js",
    "trix": "/trix.js",
    "@rails/actiontext": "/actiontext.js"
  }
}

...and nothing (JavaScript) works, because requesting /application.js returns a 404, though curiously I can see the assets compiling to fingerprinted files within the /assets directory on deploy - it's just then the app tries to use the original scripts, which (being in the app/javascript directory) are not served for the app.

FWIW, I am using render.com for deployment (though the same issue occurs in Heroku). Build script as follows:

#!/usr/bin/env bash
# exit on error
set -o errexit

bundle install
bundle exec rake assets:precompile
bundle exec rake assets:clean
bundle exec rake db:migrate

and in my production.rb I have the following (as per render docs):

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? || ENV['RENDER'].present?

...which (in production) evaluates to true. Any thoughts?

0 Answers
Related