To access javascript files from another application

I am trying to access my precompiled files of my application from another. I have 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 upload to a index.htmlcompiled file my_js_file.coffee.

my-other-appnot a Rails application. It contains a base file index.htmlthat redirects to a specific url where I try something like:

<script src="http://myapp.com/assets/my_js_file.js"></script>

I defined it in the configuration file Apache(this part is fine).

My problem is that I cannot find a way to access the compiled file my_js_file.js. Access to the file and the file name itself (with a fingerprint). How can i solve this?

Edit: , , , url .

Edit2: url , ()

0
1

, .

2 :

  • js fingerprint
  • rails - ( rails)

, , ( capistrano) index.html. :

#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"

, ( ), URL-, <head>. :

#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>

, - .

0

Source: https://habr.com/ru/post/1693066/


All Articles