从另一个应用程序访问 javascript 个文件

To access javascript files from another app

我正在尝试从另一个访问我的应用程序的预编译文件。我有一个特定的架构。这是一个简化的树

├── app
│   ├── assets
│   │   ├── javascripts
│   │   │   ├── application.coffee
│   │   │   ├── my_js_file.coffee
├── my-other-app
│   ├── index.html
│   ├── javascript
│   │   ├── anotherJSFile.js

我想在 index.html 上加载 my_js_file.coffee 的编译文件。

my-other-app 不是 Rails 应用程序。它包含一个基本的 index.html 文件,其中一个特定的 URL 重定向我尝试类似的东西:

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

我已经在 Apache 配置文件中定义了它(这部分没问题)。

我的问题是我找不到任何方法来访问已编译的 my_js_file.js 文件。对文件的访问权限和文件名本身(带有指纹)。我该如何解决这个问题?

编辑: 我认为主要问题来自指纹,因为我需要知道它才能在我的第二个应用程序上动态调整我的 url。

编辑2: 我找到了一种方法来生成具有正确指纹的动态 url,但我仍然无法访问已编译的文件(未经授权)

我做到了,但有点棘手。

有2点:

  1. js 指纹
  2. 从 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>

我希望这会对其他人有所帮助。