de.
de.

Reputation: 8477

Serve compiled javascript and css files from rails app for use in external site

How can I make some asset files (.js and .css) from my rails app available to another site?

Example:

// allow this to be added to some external website
<link rel="stylesheet" href="https://myrailsapp.com/external/mystyle.css">
<script src="https://myrailsapp.com/external/myscript.js"></script>

The js and css files should be compiled by the standard asset pipeline.

Upvotes: 0

Views: 197

Answers (1)

Vladimir Dimitrov
Vladimir Dimitrov

Reputation: 1088

For rails 5 add the desired assets to be precompiled in config/initializers/assets.rb

Rails.application.config.assets.precompile += %w( external/myscript.js external/mystyle.css )

This will generate a precompiled version of your assets on each deploy.

With digest (default rails behavior, recommended)

There is a problem with appended digest though. Precompiled assets will be named something like

mystyle-36050fdf64ed881b967d4216084ed3072da6369f1b4dcf783ea28435f6db0091.css

You can alter your deployment setup to run a rake task that will remove the digest from the asset's filename. For example https://github.com/galetahub/ckeditor/blob/master/lib/tasks/ckeditor.rake

Let's say you are using capistrano to deploy your application. You have to add something like this to config/deploy.rb

namespace :deploy do
  after :restart, 'your_rake_task_namespace:task_name'
end

Without digest (simpler but hurts cache invalidation)

If you don't want to add complexity to your deploy setup just disable assets fingerprinting. Do it globaly by adding config.assets.digest = false in config/application.rb or for a single environment config/environments/production.rb

Upvotes: 1

Related Questions