进口地图不使用生产环境中的管道中的预编译JavaScript资产(Rails 7)
任何人都设法将自己的头缠绕在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于您像我一样来自较旧的Rails版本(可能尝试使用软件包管理器或JS捆绑),遇到了与您的类似问题,因此您可能在某个地方可以将这种配置
删除,从而解决了我的问题。我的猜测是,这在某种程度上使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
Removing that fixed my issue. My guess is this somehow messes with what importmap trying to do.