14

For some unknown reason Rails 7 (development environment) doesn't pick up changes in application.js automatically when I hit F5 in the browser. The location application.js is default. I'm using pretty much default setup.

When I run the server, it picks up javascript from some cached version. I need to explicitly rails assets:precompile to make it work.

Importmap looks standard:

# Pin npm packages by running ./bin/importmap

pin "application", preload: true
...

And layout file seems pretty standard as well:

<!DOCTYPE html>
<html>
  <head>
    <title>Whatever</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="<%= image_path('favicon.svg') %>">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
    <%= javascript_importmap_tags %>
  </head>
...

I tried to set config.importmap.sweep_cache = true in my development.rb (as per importmap docs), but it seems there is no effect.

At this point I'm pretty desperate and can't understand why do I need to rails assets:precompile on my development environment.

Also, I'm not running my app on "localhost" domain, it's on something like dev.server-somewhere.com (so it's accessible from anywhere) with SSH redirects, similar to ngrok. Not sure if it's the cause of a problem.

For clarification: I'm looking for no LIVE refresh feature, just the standard approach with F5 page refresh would work.

Jason FB
  • 4,752
  • 3
  • 38
  • 69
Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58

4 Answers4

15

I have same problem, simply use: rails assets:clobber to clear all precompiled assets. After that the hot reloading will start working again.

if existing precompiled JS assets exist in public/assets/ rails gives them a priority even in development mode (and even when, for example, esbuild or tsc-watch is otherwise working to transpile your source files into app/assets/buils/, which is unrelated to Sprocket hanging onto its old cache)

Jason FB
  • 4,752
  • 3
  • 38
  • 69
Tommaso Dri
  • 216
  • 2
  • 7
  • 1
    This works, if I then run `rails assets:precompile` AND then restart my server. But that's a very painful way to develop e.g. Stimulus controllers. – Jordan F Jun 28 '22 at 12:37
  • thanks, I just did rails assets:clobber , this command will delete /public/assets folder and now it' monitor also js files (including stimulus controllers), and I don't have to do precompile. – widjajayd Sep 18 '22 at 08:46
  • After refreshing, I experience the same problem again. – Jches Dec 08 '22 at 04:27
6

Normally, assets are "precompiled" only in production/deployed environments. That means that by default there shouldn't be a folder at public/assets/ while you develop (Sprockets compiles assets on-the-fly for you in development mode).

If you accidentally have run rails assets:precompile in development, you'll have an extra folder here (public/assets/) which does not get checked in to your repository.

But if it does exist, then its existence overrides Sprocket's development mode setting to recompile on every pageload, forcing your browser to load the already compiled (and stale) asset from public/assets/...

(Ironically, running rails assets:precompile or rake assets:precompile, while it does force a one-time recompile and seemingly gives you your latest JS compile, doing this in development is typically what causes the problem of Sprockets getting stuck in the first place)

Next, force Sprockets to both delete the public/assets/ folder and then bust the thumbprint cache using:

rails assets:clobber    

(alternatively, run rails assets:clean and then touch tmp/restart.txt)

To debug further, set assets.debug = true in your environment file.

Jason FB
  • 4,752
  • 3
  • 38
  • 69
  • I was struggling a bit but what Jason suggested was currently. Removing the public/assets from clobber is correct and don't need to run precompile locally. – Koppo Jan 10 '23 at 07:40
1

I have faced this issue today and I replaced the sprockets with the propshaft. Then it worked! You might look at the upgrade guide.

1

I was facing the same issue today and simply reverted back to Sprockets and everything worked out of the box after installing the gem. It seems that Sprockets is now optional in Rails 7. I don't know if Sprockets can be combined with Import Maps, though.

Tintin81
  • 9,821
  • 20
  • 85
  • 178