Code Monkey home page Code Monkey logo

Comments (6)

josh avatar josh commented on July 23, 2024

Hmm, this should just work fine in development. The mtime of the file path should be checked on each lookup call.

In production, if you have cache.cache_classes enabled in Rails. Thats a configuration in Rails that says don't automatically reload files, only reload them when I restart the entire server.

You should also check the order of the load paths. Rails.application.assets.paths. If your gem path is before vendor/assets/javascripts, your gem is always going to be preferred since it has a higher priority.

from sprockets.

rmosolgo avatar rmosolgo commented on July 23, 2024

Hey @josh, thanks for getting back to me! Here's what I've found:

Asset Paths

Asset paths look alright, I see Rails paths ahead of gem paths:

puts Rails.application.assets.paths
# /Users/rmosolgo/code/react-rails/test/dummy/app/assets/images
# /Users/rmosolgo/code/react-rails/test/dummy/app/assets/javascripts
# /Users/rmosolgo/code/react-rails/test/dummy/app/assets/stylesheets
# /Users/rmosolgo/code/react-rails/test/dummy/vendor/assets/javascripts
# /Users/rmosolgo/code/react-rails/test/dummy/vendor/assets/react
# /Users/rmosolgo/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/turbolinks-2.5.3/lib/assets/javascripts
# /Users/rmosolgo/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/es5-shim-rails-4.0.1/vendor/assets/javascripts
# /Users/rmosolgo/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/coffee-rails-4.1.0/lib/assets/javascripts
# /Users/rmosolgo/code/react-rails/lib/assets/javascripts
# /Users/rmosolgo/code/react-rails/lib/assets/react-source
# /Users/rmosolgo/code/react-rails/lib/assets/react-source/production
# /Users/rmosolgo/code/react-rails/lib/assets/javascripts

cache_classes

As for cache_classes, someone who set this up before took care of it I think, we have this in app/config/envrionments/test.rb:

  # we need this to reload the jsx transformer when different version is dropped in
  config.cache_classes = false
  config.reload_plugins = true

I double-checked that our tests really run with Rails.env.test by adding a puts Rails.env in a test. It printed "test" as expected.

mtime

Ok, I tried overriding #fresh? again and I had some luck. Here's what I did in a test:

    # put nonsense content in the drop-in file:
    app_react_file_path = File.expand_path("../dummy/vendor/assets/javascripts/react.js",  __FILE__)
    File.write(app_react_file_path,  "'test_confirmation_token_react_content_non_production';\n")

    # reload the asset (it has been loaded before, in a previous test)
    react_asset = Rails.application.assets['react.js']
    puts "From asset pipeline:   #{File.mtime(react_asset.pathname.to_s)} (#{react_asset.pathname.to_s})"
    puts "From vendor directory: #{File.mtime(app_react_file_path)} (#{app_react_file_path})"

    def react_asset.fresh?(env)
      puts "Testing 'fresh?'"
      false
    end

    # reload the asset again
    react_asset = Rails.application.assets['react.js']
    puts "From asset pipeline:   #{File.mtime(react_asset.pathname.to_s)} (#{react_asset.pathname.to_s})"
    puts "From vendor directory: #{File.mtime(app_react_file_path)} (#{app_react_file_path})"

Output:

From asset pipeline:   2015-05-05 09:49:39 -0700 (/Users/rmosolgo/code/react-rails/lib/assets/react-source/development/react.js)
From vendor directory: 2015-05-14 11:53:13 -0700 (/Users/rmosolgo/code/react-rails/test/dummy/vendor/assets/javascripts/react.js)
Testing 'fresh?'
From asset pipeline:   2015-05-14 11:53:13 -0700 (/Users/rmosolgo/code/react-rails/test/dummy/vendor/assets/javascripts/react.js)
From vendor directory: 2015-05-14 11:53:13 -0700 (/Users/rmosolgo/code/react-rails/test/dummy/vendor/assets/javascripts/react.js)

So, this makes the tests pass, but not pretty!

from sprockets.

josh avatar josh commented on July 23, 2024

Just curious if you given Sprockets 3.x a try?

from sprockets.

rmosolgo avatar rmosolgo commented on July 23, 2024
rmosolgo ~/code/react-rails $ bundle show sprockets
/Users/rmosolgo/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/sprockets-3.1.0

I think that's a yes!

from sprockets.

josh avatar josh commented on July 23, 2024

Ok, I tried overriding #fresh? again and I had some luck. Here's what I did in a test:

Interesting, cause Asset#fresh? doesn't exist in 3.x.

from sprockets.

rmosolgo avatar rmosolgo commented on July 23, 2024

Ohhhh, I'm sorry, I've left out an important part of this issue!

We're using appraisal to test against many Rails versions (3.1 to 4.2). bundle exec appraisal test runs the tests with a bunch of different gemfiles, this one is failing on the first one which is sprockets (2.2.3)

So that's why bundle showed 3.x, but in fact this is failing in 2.2.3 (with Rails 3.1). And I guess that means it's actually an old Sprockets where I'm encountering this issue.

It's alright with me to just work around this! Thanks for your help and sorry I missed that before 😅

from sprockets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.