Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional link in manifest.js (assets.precompile discouraged) #784

Open
arianf opened this issue Mar 31, 2023 · 1 comment
Open

Conditional link in manifest.js (assets.precompile discouraged) #784

arianf opened this issue Mar 31, 2023 · 1 comment

Comments

@arianf
Copy link

arianf commented Mar 31, 2023

Sometimes, you may want to link certain files only in certain environments. For example, we only want to render the GraphiQL views in our staging and development environment, and not production, or test.

So in our Gemfile we set:

gem 'graphiql-rails', '~> 1.5', group: %I[staging development]

And then we add to our manifest.js :

//= link graphiql/rails/application.css
//= link graphiql/rails/application.js

If we do not add the above, in staging and development, we get the following error:

Asset `graphiql/rails/application.css` was not declared to be precompiled in production.
Declare links to your assets in `app/assets/config/manifest.js`.

  //= link graphiql/rails/application.css

But now that we have added the above, in production and test, we get the following error when trying to precompile assets

$ RAILS_ENV=production bundle exec rake assets:precompile

rake aborted!
Sprockets::FileNotFound: couldn't find file 'graphiql/rails/application.css'
Checked in these paths: 
  /Users/arian/repos/my-app/app/assets/config
  /Users/arian/repos/my-app/app/assets/images
  /Users/arian/repos/my-appi/app/assets/javascripts
  /Users/arian/repos/my-app/app/assets/stylesheets

This was discussed a bit here: rmosolgo/graphiql-rails#13 (comment) and here rmosolgo/graphiql-rails#75 (comment)

And here is a suggested solution to the problem: rmosolgo/graphiql-rails#75 (comment)

if Rails.env.development? || Rails.env.staging?
  Rails.application.config.assets.precompile += %w[graphiql/rails/application.js graphiql/rails/application.css]
end

But I was under the impression that we should be moving away from the precompile setting. https://github.com/rails/sprockets/blob/070fc01947c111d35bb4c836e9bb71962a8e0595/UPGRADING.md#manifestjs

Existing config.assets.precompile settings will still work for string values (although it is discouraged), but if you were previously using regexp or proc values, they won't work at all with Sprockets 4, and if you try you'll get an exception raised that looks like NoMethodError: undefined method 'start_with?'

Is there a proper way, that is not discouraged to include link conditionally, lets say per environment?

System configuration

  • Sprockets version:
sprockets (4.1.1)
sprockets-rails (3.4.2)
  • Ruby version: ruby-3.1.3

Example App (Reproduction) - THIS IS IMPORTANT YOUR ISSUE LIKELY WILL NOT BE RESOLVED WITHOUT THIS

I can add an example app if needed

@arianf arianf changed the title Conditional link in manifest.js (assets.precompile discouraged but currently only way) Conditional link in manifest.js (assets.precompile discouraged) Mar 31, 2023
@4lllex
Copy link

4lllex commented Sep 3, 2024

The discouragement is from adding hundreds of files to config.assets.precompile because proc and regex are not supported anymore, it's fine for a couple of files. But you could have a conditional secondary manifest:

if Rails.env.development? || Rails.env.staging?
  Rails.application.config.assets.precompile += %w[manifest.graphiql.js]
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants