我正在尝试使用我的Rails 7应用程序来设置Cable,该应用程序使用了importmaps。一切都在开发中工作,但在生产中我的通道JavaScript找不到:Failed to load resource: the server responded with a status of 404 ()
为我的所有notifications_channel.js
和所有其他渠道。
有什么我遗漏的吗?我没办法了。
我的设置如下:
ruby "3.0.0"
gem "rails", "~> 7.0.4", ">= 7.0.4.3"
javascript/application.js
console.log("application.js")
import "@hotwired/turbo-rails"
import "controllers"
import "channels"
// Start StimulusJS
import { Application } from "@hotwired/stimulus"
const application = Application.start();
javascript/channels/index.jsimport "./notification_channel"
javascript/channels/notification_channel.js
consumer.subscriptions.create("NotificationChannel", {
connected() {
console.log("connected NotificationChannel")
// Called when the subscription is ready for use on the server
},
disconnected() {
console.log("disconnected NotificationChannel")
// Called when the subscription has been terminated by the server
},
received(data) {
console.log(data)
let element = document.getElementById("flashes")
element.insertAdjacentHTML('beforeend', data.html);
}
});
channels/notification_channel.rb
class NotificationChannel < ApplicationCable::Channel
def subscribed
stream_from "notification_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
production.rb
config.action_cable.allowed_request_origins = [ "https://myapp.hatchboxapp.com", /http:\/\/localhost*/]
config.hosts << "myapp.hatchboxapp.com"
config.hosts << "localhost"
这是我的importmap.rb
# Pin npm packages by running ./bin/importmap pin tailwindcss-stimulus-components
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "https://ga.jspm.io/npm:@hotwired/[email protected]/dist/stimulus.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin "@rails/request.js", to: "https://ga.jspm.io/npm:@rails/[email protected]/src/index.js"
pin "tailwindcss-stimulus-components", to: "https://ga.jspm.io/npm:[email protected]/dist/tailwindcss-stimulus-components.module.js"
pin "@rails/actioncable", to: "actioncable.esm.js"
pin_all_from "app/javascript/channels", under: "channels"
cable.yml
development:
adapter: redis
url: redis://localhost:6379/1
test:
adapter: test
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: linus_production
ENV.fetch("REDIS_URL")
返回一个有效的url:redis://default: [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) :6379/1
1条答案
按热度按时间y3bcpkx11#
不要使用相对导入,它只在开发中起作用,因为资产请求通过
sprockets
。请注意,您没有在这里直接导入本地文件,导入必须匹配importmap中的某些内容: