I’m using rails 6.1.3 with bootstrap, jquery and popper.js Drop down menus work in development but not in production In config webpacker.yml extract_css is set to true and compile is set to false for production environment These are my application.js and config/webpack/environment.js In Firefox the web developer console is blank I always expect to see something in the console
application.js:
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
require("jquery")
require("@rails/ujs")
require("turbolinks")
require("js.cookie")
require("@rails/activestorage")
require("bootstrap")
require("channels")
require.context('../images', true)
require.context('./stylesheets', true)
require.context('./stylesheets/fonts', true)
require("trix")
require("@rails/actiontext")
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
import "bootstrap"
import "./stylesheets/application"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
import "controllers"
//require("@rails/ujs").start()
//import "./stylesheets/application"
import flatpickr from "flatpickr"
import datepickr from "flatpickr"
import timepickr from "flatpickr"
require("flatpickr/dist/flatpickr.css")
//require("bootstrap")
// import "blueimp-gallery"
document.addEventListener("turbolinks:load", () => {
flatpickr("[data-behavior='flatpickr']", {
altInput: true,
altFormat: "F j, Y",
dateFormat: "Y-m-d",
})
})
import JQuery from 'jquery';
window.jQuery = $;
window.$ = $;
document.addEventListener("turbolinks:load", () => {
$('[data-toggle="tooltip"]').tooltip()
$('[data-toggle="popover"]').popover()
})
require("./stylesheets/application")
require.context('file-loader?name=[path][name].[ext]&context=node_modules/jquery-ui-dist!jquery-ui-dist', true, /jquery-ui\.css/ );
require.context('file-loader?name=[path][name].[ext]&context=node_modules/jquery-ui-dist!jquery-ui-dist', true, /jquery-ui\.theme\.css/ );
Rails doesn’t serve static files by default in production so are you using nginx/apache to serve files? Firefox should show an error in the network console if it doesn’t find your files. From the rails guide:
config.public_file_server.enabled configures Rails to serve static files from the public directory. This option defaults to true , but in the production environment it is set to false because the server software (e.g. NGINX or Apache) used to run the application should serve static files instead. If you are running or testing your app in production using WEBrick (it is not recommended to use WEBrick in production) set the option to true . Otherwise, you won’t be able to use page caching and request for files that exist under the public directory.
It defaults to false so unless I changed it this shouldn’t be relevant It only evaluates to true if ENV[‘RAILS_SERVE_STATIC_FILES’] is present, although I couldn’t find any occurrences of it by searching for it project wide with atom
Rails.application.config.public_file_server.enabled evaluates to true on my development configuration, but is in fact false in production where we use nginx to serve the public files. The name of this option has changed from Rails 4 / 5 so if you’re upgrading an app that might be the reason?
config.public_file_server.enabled = ENV[‘RAILS_SERVE_STATIC_FILES’].present?
I guess RAILS_SERVE_STATIC_FILES would be set as a passenger_env_var in nginx.conf but I don’t have it set so it isn’t persisted, but it does make sense to consider production.rb first since the problem is only in production
The problem was the last two lines of application.js where I try to require.context for jquery-ui.css After commenting out those lines my app behaves normally So then it seems I shouldn’t ever try to require css from a node_module in application.js ?