javascript - Rails 5 生产应用程序中的 Heroku JS 错误

标签 javascript ruby-on-rails heroku

我的问题已经存在两个月了,我在无数关于此问题的帖子中得到的答案都没有解决我的问题。所以我又进行了一次令人沮丧的尝试:

这是我的应用程序在 Heroku 上的生产环境中运行时遇到的 JavaScript 错误。

我在浏览器控制台中收到的错误是这样的:

Uncaught TypeError: ht.each is not a function
at application-b030a52a6989f6564d19974180747e99f4ac5a28109188d096b47fc12a38f2cf.js:1
at application-b030a52a6989f6564d19974180747e99f4ac5a28109188d096b47fc12a38f2cf.js:1
at application-b030a52a6989f6564d19974180747e99f4ac5a28109188d096b47fc12a38f2cf.js:1

当我在本地生产中运行我的应用程序时,我没有收到此错误,这是因为我的

/public/assets

文件夹如下所示:

.sprockets-manifest-a7e0d8d7f0627b40ca52eea9623fee45.json
application-a358e1eb31a8f590691be18692bb2f57be910bbb2c5800a4dcb95a6c56deb358.js
application-a358e1eb31a8f590691be18692bb2f57be910bbb2c5800a4dcb95a6c56deb358.js.gz
application-ea2c1b8977f914f6ea0d3aa17e9a18079d0fd8ab6b9aa1a88609bcaba62135d5.css
application-ea2c1b8977f914f6ea0d3aa17e9a18079d0fd8ab6b9aa1a88609bcaba62135d5.css.gz
bootstrap
rails-c094bc3a4bf50e5bb477109e5cb0d213af27ad75b481c4df249f50974dbeefe6.png

在 Heroku 上,同一文件夹包含以下内容:

application-18082fe8586b430e37211d8a126c18df28136b583c39a4a6ceb1418202d665a3.css
application-18082fe8586b430e37211d8a126c18df28136b583c39a4a6ceb1418202d665a3.css.gz
application-8344b3a5f740968bd820e663ceac72950f2c3dbec18583fa38efec5788e87e31.js
application-8344b3a5f740968bd820e663ceac72950f2c3dbec18583fa38efec5788e87e31.js.gz
application-a5ee6c15ec2d7545c56a0340738bf8bf07d80b954e685fd04cff6067f34ec439.css
application-a5ee6c15ec2d7545c56a0340738bf8bf07d80b954e685fd04cff6067f34ec439.css.gz
application-b030a52a6989f6564d19974180747e99f4ac5a28109188d096b47fc12a38f2cf.js
application-b030a52a6989f6564d19974180747e99f4ac5a28109188d096b47fc12a38f2cf.js.gz
application-baa8861fe552f67418ef50a3fcdbb69e3a2ab43226917999d296f85bb407d3e2.js
application-baa8861fe552f67418ef50a3fcdbb69e3a2ab43226917999d296f85bb407d3e2.js.gz
application-cb3abf099471b60aa6a5318e339b92c962f22034d16f78b0f6700a1a4abf42dd.css
application-cb3abf099471b60aa6a5318e339b92c962f22034d16f78b0f6700a1a4abf42dd.css.gz
bootstrap

在本地,这些 JavaScript 文件会加载到发生错误的网站中:

<script src="/assets/jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/bootstrap.self-b38817c3e3a3049abb1fc08dd6ae448b23330f8453226efdb074710209474f75.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/rails-ujs.self-3b600681e552d8090230990c0a2e8537aff48159bea540d275a620d272ba33a0.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable.self-69fddfcddf4fdef9828648f9330d6ce108b93b82b0b8d3affffc59a114853451.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/cable.self-8484513823f404ed0c0f039f75243bfdede7af7919dda65f2e66391252443ce9.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/forum.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/sessions.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/users.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/application.self-12be097b9a2442b0b6cdcb5146d1d63c00abcde3675ff34d1de6126cb13e6714.js?body=1" data-turbolinks-track="reload"></script>

但是在 Heroku 上,网站只加载这个 JS 文件(它似乎是由本地加载的所有文件组成的,现在彼此冲突,但我真的不知道)

<script src="/assets/application-b030a52a6989f6564d19974180747e99f4ac5a28109188d096b47fc12a38f2cf.js" data-turbolinks-track="reload"></script>

有人可以帮我让它在 Heroku 上运行吗?

所讨论的代码是 Michael Hartl 一对一执行其代码的标准“sample_app”,它或多或少适用于其他人,但不适用于我。

我正在使用最新的 Rails 和 Ruby 的 Windows 上工作

我尝试过的:

#1:预编译 public/assets 文件夹并清理然后推送,这当然很愚蠢,因为 git 不会推送这个文件夹,但人们建议我关注

#2:删除上面的文件夹,然后推送它,再次出现与上面相同的问题...

#3:以各种可能的方式重新排列我的 app/assets/javascripts/application.js 文件中的 require 语句:

//= require jquery
//= require bootstrap
//= require rails-ujs
//= require turbolinks
//= require_tree .

4:还有更多适用于其他人的“解决方案”,当人们搜索“Heroku Javascript 错误”、“Heroku Rails 生产中的 javascript 错误”、“等内容时,可以在堆栈溢出中轻松找到这些解决方案” Heroku Rails DELETE 方法在生产中不起作用”等等,正如我所说,我已经准备好 2 个月来寻找解决方案了,在 2.3 小时无果的搜索之后,我有足够的时间尝试和放弃。

#5 这已经有效:复制将代码从 /config/environments/development 粘贴到 /config/environments/product

/config/environments/development

# frozen_string_literal: true

Rails.application.configure do
  # Settings specified here will take precedence over those in
  # config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable/disable caching. By default caching is disabled.
  # Run rails dev:cache to toggle caching.
  if Rails.root.join('tmp', 'caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => "public, max-age=#{2.days.to_i}"
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  # Store uploaded files on the local file system (see config/storage.yml
  # for options)
  config.active_storage.service = :local

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_caching = true

  config.action_mailer.delivery_method = :test

  host = 'localhost:3000'

  config.action_mailer.default_url_options = { host: 'localhost:3000' }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'example.com',
    user_name:            '###',
    password:             '###',
    authentication:       'plain',
    enable_starttls_auto: true
  }

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Highlight code that triggered database queries in logs.
  config.active_record.verbose_query_logs = true

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
  # routes, locales, etc. This feature depends on the listen gem.
  # config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

/config/环境/生产

# frozen_string_literal: true

Rails.application.configure do
  # Settings specified here will take precedence over those in
  #  config/application.rb.

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Ensures that a master key has been made available in either
  #  ENV["RAILS_MASTER_KEY"]
  # or in config/master.key. This key is used to decrypt credentials
  # and other encrypted files).
  # config.require_master_key = true

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

  # `config.assets.precompile` and `config.assets.version` have moved
  #  to config/initializers/assets.rb

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = 'http://assets.example.com'

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

  # Store uploaded files on the local file system
  # (see config/storage.yml for options)
  config.active_storage.service = :local

  # Mount Action Cable outside main process or domain
  # config.action_cable.mount_path = nil
  # config.action_cafsdfsdble.url = 'wss://example.com/cable'
  # config.action_cable.allowed_request_origins = [ 'http://example.com',
  # /http:\/\/example.*/ ]

  # Force all access to the app over SSL, use Strict-Transport-Security,
  # and use secure cookies.
  config.force_ssl = false

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  # Prepend all log lines with the following tags.
  config.log_tags = [:request_id]

  # Use a different cache store in production.
  # config.cache_store = :mem_cache_store

  # Use a real queuing backend for Active Job (and separate
  # queues per environment)
  # config.active_job.queue_adapter     = :resque
  # config.active_job.queue_name_prefix = "sample_app_#{Rails.env}"

  config.action_mailer.perform_caching = false
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  host = 'mighty-springs-34070.herokuapp.com'
  config.action_mailer.default_url_options = { host: host }
  ActionMailer::Base.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'example.com',
    user_name:            '####',
    password:             '####',
    authentication:       'plain',
    enable_starttls_auto: true
  }

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to
  # raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  # Use a different logger for distributed setups.
  # require 'syslog/logger'
  # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new
  # 'app-name')

  if ENV['RAILS_LOG_TO_STDOUT'].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false
end

感谢您的阅读

最佳答案

这是uglifier gem 。我从 4.1.18 降级到 3.0.4

关于javascript - Rails 5 生产应用程序中的 Heroku JS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52138549/

相关文章:

javascript - jQuery 通过识别属性确定所选元素的 CSS 是否具有某些属性

ruby-on-rails - Ruby on Rails 中的静态页面

ruby-on-rails - Active Record 作用域链,忽略连接,如果已经连接

python - Heroku 和 OpenCV 与 Python

ruby-on-rails - heroku 上的多线程 ruby​​ worker

javascript - 使用 Enter 键提交表单时,语义 UI 表单验证器不会阻止请求

javascript - 如何检查用户是匿名的还是从 javascript 登录的?

javascript - 添加后清空 <li> 标签

ruby-on-rails - 使用用户 :id 添加 View

heroku - 连接到 Elasticsearch Heroku 数据库