问题
I'm trying to access to my pre-compiled files of my application from another. I've a specific architecture. Here is a simplified tree
├── app
│ ├── assets
│ │ ├── javascripts
│ │ │ ├── application.coffee
│ │ │ ├── my_js_file.coffee
├── my-other-app
│ ├── index.html
│ ├── javascript
│ │ ├── anotherJSFile.js
I would like to load on index.html
the compiled file of my_js_file.coffee
.
my-other-app
isn't a Rails app. It contains a basic index.html
file where a specific URL redirect where I try something like:
<script src="http://myapp.com/assets/my_js_file.js"></script>
I've defined it on an Apache
configuration file (this part is ok).
My problem is that I can't find any way to access to a compiled my_js_file.js
file. The access to the file and the file name itself (with fingerprint). How could I solve this?
Edit: I think the main problem comes from the fingerprint because I need to know it to adapt my url on my second app dynamically.
Edit2: I've found a way to generate dynamic url with correct fingerprint but I still can't access to compiled files (unauthorized)
回答1:
I did it but it was a bit tricky.
There are 2 points :
- js fingerprint
- authentification token from the rails app to the other webpage (outside this rails app environment)
To solve the first point I had to create a method on a model that I call at the end of a deployment (with capistrano
) to modify my index.html
file dynamically. Here is how I did it:
#my-other-app/index.html
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- BEGIN DYNAMIC URI -->
<!-- END DYNAMIC URI -->
</head>
<body >
<div id="awesome">
Some stuff here..
</div>
</body>
</html>
#myModel.rb
def self.generate_index_file
js_files = ['my_js_file.js']
tempfile = File.open Rails.root.join('my-other-app/index.tmp'), 'w'
f = File.new('my-other-app/index.html')
f.each do |line|
if line =~ /^.*<!-- BEGIN DYNAMIC URI -->/
tempfile << line
js_files.each do |filename|
fingerprinted_name = Rails.application.assets.find_asset(filename).digest_path
tempfile << "<script src='#{Rails.application.default_url_options[:host]}/assets/" + fingerprinted_name + "?body=1'></script>\n"
end
else
tempfile << line
end
end
f.close
tempfile.close
FileUtils.mv("my-other-app/index.tmp", "my-other-app/index.html")
end
#config/deploy/production.rb
# Available only for Capistrano 2.x
namespace :deploy do
task :generate_samsung_index, roles: :app do
run %Q{cd #{latest_release} && RAILS_ENV=#{rails_env} bundle exec rails runner 'MyModel.generate_index_file'}
end
end
after "deploy:restart", "deploy:generate_samsung_index"
Now, to solve the second part of the problem (authentification part), I needed to add token to the url before adding it to the <head>
. Here is my code:
#my_models_controller.rb
def my_method
redirect_to "http://myawesomeurl.com?token=#{form_authenticity_token}"
end
#my-other-app/index.html
# On <head> with my previous code
<script language="javaScript" type="text/javascript">
meta1 = document.createElement("meta");
meta1.name = "csrf-param";
meta1.content = "authenticity_token";
$("head").append(meta1);
token = "token"
token_result = new RegExp(token + '=([^&]*)', 'i').exec(window.location.search)
meta2 = document.createElement("meta");
meta2.name = "csrf-param";
meta2.content = token_result;
$("head").append(meta1);
$("head").append(meta2);
</script>
I hope this will help someone else.
来源:https://stackoverflow.com/questions/32048438/to-access-javascript-files-from-another-app