进口地图不使用生产环境中的管道中的预编译JavaScript资产(Rails 7)

发布于 2025-02-09 14:26:29 字数 3028 浏览 3 评论 0原文

任何人都设法将自己的头缠绕在Rails 7中的ImportMaps周围?我有一个头抓手。

如果我在开发中运行./ bin/importmap json,我会得到(如预期的):

{
  "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:[email protected]/dist/stimulus.umd.js",
    "stimulus-use": "https://ga.jspm.io/npm:[email protected]/dist/index.js",
    "hotkeys-js": "https://ga.jspm.io/npm:[email protected]/dist/hotkeys.esm.js",
    "trix": "/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js",
    "@rails/actiontext": "/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js"
  }
}

...我所有的JavaScript都可以正常工作。

但是,如果我在生产中运行了相同的命令,则我会得到以下内容:

{
  "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:[email protected]/dist/stimulus.umd.js",
    "stimulus-use": "https://ga.jspm.io/npm:[email protected]/dist/index.js",
    "hotkeys-js": "https://ga.jspm.io/npm:[email protected]/dist/hotkeys.esm.js",
    "trix": "/trix.js",
    "@rails/actiontext": "/actiontext.js"
  }
}

...而且(JavaScript)无效,因为请求 /application.js返回404,尽管奇怪的是,我可以看到资产编译到指纹文件中的资产/Assets Deploy的目录 - 就在该应用程序中,该应用程序(在App/javascript目录中)就不会为应用程序提供。

fwiw,我正在使用render.com进行部署(尽管在Heroku发生了同样的问题)。如下:

#!/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

在我的生产中构建脚本。RB我有以下(按照渲染文档):

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

...(在生产中)评估为true。 有什么想法吗?

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:[email protected]/dist/stimulus.umd.js",
    "stimulus-use": "https://ga.jspm.io/npm:[email protected]/dist/index.js",
    "hotkeys-js": "https://ga.jspm.io/npm:[email protected]/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:[email protected]/dist/stimulus.umd.js",
    "stimulus-use": "https://ga.jspm.io/npm:[email protected]/dist/index.js",
    "hotkeys-js": "https://ga.jspm.io/npm:[email protected]/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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2025-02-16 14:26:30

鉴于您像我一样来自较旧的Rails版本(可能尝试使用软件包管理器或JS捆绑),遇到了与您的类似问题,因此您可能在某个地方可以将这种配置

# REMOVE THIS
Rails.application.config.assets.paths << Rails.root.join('app', 'javascript', 'controllers')

删除,从而解决了我的问题。我的猜测是,这在某种程度上使ImportMap试图做的事情混乱。

Encountered a similar issue to yours, given that like me you've come from an older rails version (probably tried to use a package manager or js-bundling), You probably have this configuration somewhere

# REMOVE THIS
Rails.application.config.assets.paths << Rails.root.join('app', 'javascript', 'controllers')

Removing that fixed my issue. My guess is this somehow messes with what importmap trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文