Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 546c0a9

Browse files
committed
Makes requirejs work with CDN
1 parent 3204cef commit 546c0a9

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

app/helpers/requirejs_helper.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ def _requirejs_data(name, &block)
1111
{}.tap do |data|
1212
if name
1313
name += ".js" unless name =~ /\.js$/
14-
data['main'] = _javascript_path(name).sub(/\.js$/,'')
14+
data['main'] = _javascript_path(name).
15+
sub(/\.js$/,'').
16+
sub(baseUrl(name), '').
17+
sub(/\A\//, '')
1518
end
1619

1720
data.merge!(yield controller) if block_given?
@@ -54,6 +57,8 @@ def requirejs_include_tag(name=nil, &block)
5457
# and in the build_config.
5558
run_config['paths'] = paths
5659
end
60+
61+
run_config['baseUrl'] = baseUrl(name)
5762
html.concat <<-HTML
5863
<script>var require = #{run_config.to_json};</script>
5964
HTML
@@ -89,4 +94,11 @@ def _javascript_path(name)
8994
"/assets/#{name}"
9095
end
9196
end
97+
98+
def baseUrl(js_asset)
99+
js_asset_path = javascript_path(js_asset)
100+
uri = URI.parse(js_asset_path)
101+
asset_host = uri.host && js_asset_path.sub(uri.request_uri, '')
102+
[asset_host, Rails.application.config.assets.prefix].join
103+
end
92104
end

0 commit comments

Comments
 (0)