Shopify 应用程序 - 如何在 rails 上使用 ruby 的应用程序代理来包含资产

Shopify app - how to include assets using an app-proxy with ruby on rails

我正在 rails 上使用 ruby 构建一个 shopify 应用程序代理。我正在使用 shopify_app gem。

在我的控制器操作中,我有以下设置:

render layout: false, content_type: ‘application/liquid’

,以便应用嵌入商店的 layout/theme.liquid.

我的问题是如何将资产拉入应用程序?我尝试使用以下方法将它们手动包含到模板文件中:

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>


<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

但是,唉,我收到 404 not found 错误。目前我正在将资产文件放入商店的主题中,这似乎不太理想。

不要跳过布局

如果您想在主题的 <head> 中插入自己的 js 和 css,请不要设置 layout: false。 Shopify 将抓取您的资产并将其附加到主题中。

404s 的可能原因:错误的资产宿主

查看返回 404 的请求并验证资产主机是您自己的自定义域或 heroku 域,而不是 Shopify 商店的域。您可以通过以下方式查看:

  1. 打开您最喜欢的浏览器,打开开发工具,然后转到 "Network" 选项卡。
  2. 找到你的 js 和 css 404 请求。
  3. 如果请求将https://the-shopify-store-domain.com/assets/application-3b57310f71432b97ccf86f51aawtert735dwtertbd9783ed06a5296871536a24.css, then that means it's looking for the assets in the wrong domain. Instead it should be making the requests to your own domain such as: https://your-own-name.herokuapp.com/assets/application-3b57310f71432b97ccf86f51aawtert735dwtertbd9783ed06a5296871536a24.css

解决方案

由于这是一个代理应用程序,主机域正在更新为 Shopify 商店的域而不是原始源。因此,您需要具体说明资产的存储位置。幸运的是,您可以在 Rails 中通过将资产主机配置更改为您自己的主机来轻松完成此操作:

config.action_controller.asset_host = "https://your-custom-domain-or-herokuapp-domain.com"

提示:此设置在 production.rb 文件中的新 rails 应用程序中默认被注释掉。

希望对您有所帮助!