Shopify应用程序-如何使用带有红宝石的应用程序代理包含资产 [英] Shopify app - how to include assets using an app-proxy with ruby on rails

查看:300
本文介绍了Shopify应用程序-如何使用带有红宝石的应用程序代理包含资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails上的ruby构建shopify应用程序代理.我正在使用shopify_app宝石.

I'm building a shopify app-proxy using ruby on rails. i’m using the shopify_app gem.

在我的控制器操作中,我进行了以下设置:

in my controller actions i have the following set:

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

,以便将应用程序嵌入商店的布局/theme.liquid中.

,so that the app is embedded within the shop’s layout/theme.liquid.

我的问题是我该如何将资产提取到应用程序中?我尝试使用以下方法将它们手动添加到模板文件中:

my question is how do i then pull assets into the app? I’ve tried including them manually into the template files using:

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


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

但是可惜,我得到404 not found错误.目前,我正在将资产文件放入商店的主题中,这似乎并不理想.

but alas, i get 404 not found errors. currently i’m putting asset files into the store’s theme which doesn’t seem ideal.

推荐答案

不要跳过布局

如果要与其他所有主题一起在主题的<head>中插入自己的js和CSS,请不要设置layout: false. Shopify将获取您的资产并将其附加到主题中.

If you want to insert your own js and css inside the theme's <head> with all the others, don't set layout: false. Shopify will grab and append your assets into the theme.

404s的可能原因:资产托管人错误

查看返回404的请求,并确认资产宿主是您自己的自定义域或heroku域,而不是Shopify商店的域.您可以通过以下方式进行检查:

Take a look at the requests that are returning a 404 and verify that the asset host is your own custom or heroku domain, and not the Shopify store's domain. Here's how you can check:

  1. 打开您喜欢的浏览器,打开开发工具,然后转到网络"标签.
  2. 找到您的js和CSS 404请求.
  3. 如果请求要发送到 https://the-shopify -store-domain.com/assets/application-3b57310f71432b97ccf86f51aawtert735dwtertbd9783ed06a5296871536a24.css ,则表示它正在寻找错误域中的资产.相反,它应该向您自己的域发出请求,例如: https://your-own-name.herokuapp.com/assets/application-3b57310f71432b97ccf86f51aawtert735dwtertbd9783ed06a5296871536a24.css
  1. Open up your favorite browser, open the dev tool, then go to the "Network" tab.
  2. Locate your js and css 404 requests.
  3. If the requests are going to 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中轻松地做到这一点:

Since this is a proxy app, the host domain is getting updated to the Shopify store's domain instead of the original source. So you need to be specific about where your assets are stored. Luckily, you can do this easily in Rails by changing the asset host configuration to your own host:

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

提示:默认情况下,production.rb文件中的新Rails应用程序会将此设置注释掉.

Tip: This setting is commented out by default on a new rails app in the production.rb file.

希望这会有所帮助!

这篇关于Shopify应用程序-如何使用带有红宝石的应用程序代理包含资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆