简体   繁体   English

在我运行 rails assets:precompile 之前,Rails 应用程序不会加载对 Javascript 文件的更新

[英]Rails app doesn't load updates to Javascript files until I run rails assets:precompile

I have an old Rails app that I'm trying to upgrade to Rails 7. Somewhere in the upgrade process I broke the asset pipeline.我有一个旧的 Rails 应用程序,我正在尝试升级到 Rails 7。在升级过程中的某个地方,我打破了资产管道。 When I make updates to a javascript file, the changes aren't visible unless I run rails assets:precompile and restart the server.当我对 javascript 文件进行更新时,更改是不可见的,除非我运行rails assets:precompile并重新启动服务器。

When I do run rails assets:precompile , I get a bunch of compiled JS and CSS assets in my public/assets folder.当我运行rails assets:precompile时,我在 public/assets 文件夹中得到了一堆已编译的 JS 和 CSS 资产。 This seems like an outdated version of the asset pipeline process.这似乎是资产管道流程的过时版本。

This project is a personal project that I've had lying around for years and it spans Rails versions from 4 to 7. It's never had a user base, it's just an idea that I go back and work on from time to time.这个项目是一个我已经搁置多年的个人项目,它涵盖了从 4 到 7 的 Rails 版本。它从来没有一个用户群,这只是我 go 不时回来并继续工作的一个想法。 So it's possible I have an old version of the assets pipeline running.所以我有可能正在运行旧版本的资产管道。

I created a new Rails 7 project from scratch and it works as expected.我从头开始创建了一个新的 Rails 7 项目,它按预期工作。 I've been trying to make the new project as close to my existing project as I can to see where my current project is breaking, but that is becoming extremely difficult and not yielding any results yet.我一直在努力使新项目尽可能接近我现有的项目,以查看我当前的项目在哪里中断,但这变得非常困难并且尚未产生任何结果。

I'm sure I must have some old configuration still in place but I have no idea where to look.我确定我一定还有一些旧的配置,但我不知道去哪里找。 I can't remember ever having this problem before.我不记得以前有过这个问题。 I would love any suggestions on where to look for differences between the old broken application and the new one that works.我很乐意就在哪里寻找旧的损坏的应用程序和可用的新应用程序之间的差异提出任何建议。

I started like this:我是这样开始的:

rails _6.1.4.4_ new rails_upgrade
cd rails_upgrade

bundle remove webpacker  # obsolete
bundle remove turbolinks # obsolete
bundle remove sass-rails # outdated
bundle remove rails
bundle add rails --version "~> 7.0"

bin/rails app:update
bin/rails db:migrate

Gemfile untouched, how are you supposed to know the new defaults unless you run rails new . Gemfile不变,除非你运行rails new否则你怎么知道新的默认值。 Digging deep into app generator you can generate just the necessary file:深入研究应用程序生成器,您可以只生成必要的文件:

require "rails/generators/rails/app/app_generator"

Rails::Generators::AppGenerator.new(["rails"],
  {javascript: "esbuild", force: true}, # `rails new` options
  destination_root: "tmp")              # don't mess up the root directory
    .template("Gemfile")                # or `.send(:build, :gemfile)`

puts File.read("tmp/Gemfile")

But you probably want to do:但你可能想这样做:

bundle add jsbundling-rails turbo-rails stimulus-rails sprockets-rails

bin/rails javascript:install:esbuild
bin/rails turbo:install
bin/rails stimulus:install

Remove app/javascript/packs and webpacker related things, like, javascript_pack_tag .删除app/javascript/packs和 webpacker 相关的东西,比如javascript_pack_tag

New javascript entry is app/javascript/application.js .新的 javascript 条目是app/javascript/application.js You have to build it with esbuild to use in your layout:您必须使用 esbuild 构建它才能在您的布局中使用:

# this every time
bin/dev

Compiled files go into app/assets/builds/ .将文件 go 编译到app/assets/builds/中。 This directory is in app/assets/config/manifest.js , so every file can be precompiled in production.该目录位于app/assets/config/manifest.js中,因此每个文件都可以在生产中预编译。

You should have this in your layout:你应该在你的布局中有这个:

<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
# NOTE: no turbolinks                      ^^^^^^^^^^^^^^^^

Do not precompile , it is not a solution.不要预编译,这不是解决方案。 Run this as well to undo, this is not optional:也运行它来撤消,这不是可选的:

bin/rails assets:clobber

bin/rails assets:precompile is for production, not development. bin/rails assets:precompile用于生产,而不是开发。

As far as javascript is concerned it should work and reload if it compiles without errors.就 javascript 而言,如果编译没有错误,它应该可以工作并重新加载。 These are simplified upgrade steps, but the number of times people said the assets are not reloading, I couldn't reproduce it.这些是简化的升级步骤,但是人们说资产没有重新加载的次数,我无法重现。 Most of the time these are the solutions:大多数时候这些是解决方案:

bin/rails assets:clobber
bin/dev # if you have it, and you should unless you're using importmap-rails

So this is really unsatisfying, but what finally resolved this issue was that I removed the bootsnap gem.所以这真的很不令人满意,但最终解决这个问题的是我删除了bootsnap gem。 I don't know what was happening or why, but after removing this gem, it behaved normally.我不知道发生了什么或为什么,但是在删除这个 gem 之后,它表现正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM