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

时光毁灭记忆、已成空白 提交于 2021-01-29 16:20:42

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/59510688/serve-compiled-javascript-and-css-files-from-rails-app-for-use-in-external-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!