有时(不可预料地)我的 Rails 应用程序为我的资产提供摘要。有时,没有

Sometimes (unpredictably) my Rails application serves my assets with a digest. Sometimes, without

查看我主页的来源:

  <link data-turbolinks-track="true" href="/assets/application-ea3565aaa6bc4c481fc9425308d6bd54.css" media="all" rel="stylesheet" />
  <script data-turbolinks-track="true" src="/assets/application-2192d37159557835f39e5f3914e03276.js"></script>

然后我刷新...从 "incognito" window:

  <link data-turbolinks-track="true" href="/stylesheets/application.css" media="all" rel="stylesheet" />
  <script data-turbolinks-track="true" src="/javascripts/application.js"></script>

那我再刷新一下。它恢复到原来的状态。

如您所想,这样的行为非常令人沮丧。

我正在使用 Unicorn + nginx。摘自config/environments/production.rb

  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both thread 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

  # Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = true

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

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

  # Generate digests for assets URLs.
  config.assets.digest = true

Headers 对于使用摘要生成资产的 curl:

< HTTP/1.1 200 OK
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Mon, 27 Apr 2015 12:22:32 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Status: 200 OK
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-UA-Compatible: chrome=1
< ETag: "b2c85c1389b561dadeb93132827e948d"
< Cache-Control: max-age=0, private, must-revalidate
< Set-Cookie: _Foobar_session=EVN3bklLdk9zVE93YllRVVRNY21BejdGRlJJTVUya1JycmlkYUJwSVFjV1lQcmp3em5LQlB1OUlWYk8zRlZlVS8rZTlLQkx4N0MrT1lCUWc4MkgvUTB5YklFUC9XRnJPZTZnTXM0OENPdmtiaWlpTUpOMlRJZUMvb2F4N0dLMEZVZjJDZ1hMdUFHT3k4VzdvMXk4NFJhcE03YysvTUdHUzgvbm5jY0g1UnozaXdtem1SdjYvU25MdFpiL1dqWlVnLS1WbWNHZ3JleHZaWERTbjllVFpFUHVnPT0%3D--1ac9ee1e8f941da47069271998f8cbdec9df68a0; path=/; HttpOnly
< X-Request-Id: 4ff86cd4-229c-4d8b-8f85-71c5f56af0d6
< X-Runtime: 0.016675
< 

没有摘要:

< HTTP/1.1 200 OK
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Mon, 27 Apr 2015 12:22:30 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Status: 200 OK
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-UA-Compatible: chrome=1
< ETag: "141287c98bf53d0894adb5d754bcc2fd"
< Cache-Control: max-age=0, private, must-revalidate
< Set-Cookie: _Foobar_session=XzRrc3BwZHY5dXYyMFRBRFQzTVVTNlZNOEZBbFNQZHdjZ0pSditEenY1YjA1N0h2aW1LeFhJZGdRQVJUZFByaGpaQnYzZkNTallTT0J5TGRmM2Y0ZzVwdjdHT0xabC9YeHk4NzV5TVJFVFk0RmFiVERMRnhwVTIwdDFPTjFFdml0eE1VQ3pSd2lvVGh6czNhT2Q4blZ2cm05K0dNZnhtNnZVOUtSQWdqdnQrSXZGNXcrNlRkYm9VTktnVTRMVmJqLS1saVNldE8zRVdmU3JyK1VqZDdUazN3PT0%3D--276bde7cd6c87d6860a32fac6796fd0ac12bf90b; path=/; HttpOnly
< X-Request-Id: f967633d-2fe5-4705-a6b0-467fb8ce867f
< X-Runtime: 0.019299

当某处设置了缓存时会发生这种情况,因此:

  1. 重新检查环境,应该设置为production
  2. app/views/layouts/application.html
  3. 中检查 headers 上的 cache
  4. 从远程服务器上的旧缓存文件中清除目录your_app/public/assets

然后重新部署

希望对您有所帮助。