简体   繁体   English

使用 Shopify ZDB974238714CA8DE634A7CE1D083A14Z 将纯 javascript 代码添加到 Shopify 主题

[英]Adding pure javascript code to Shopify Theme by using Shopify API

I developed a Shopify application that i install on Shopify Store and i want my application to add a javascript snippet in all the store pages.我开发了一个 Shopify 应用程序,我安装在 Shopify 商店上,我希望我的应用程序在所有商店页面中添加一个 javascript 片段。 For this I want my application to modify the current theme, by using Shopify API , and add the javascript snippet to that theme.为此,我希望我的应用程序使用Shopify API修改当前主题,并将 javascript 片段添加到该主题。

I know of several ways to do it but they have several disadvantage:我知道几种方法可以做到这一点,但它们有几个缺点:

  • Add a Shopify Asset/Snippet - The store owner has to manually modify the store's theme to link to the asset/snippet.添加 Shopify 资产/片段 - 店主必须手动修改商店的主题以链接到资产/片段。
  • Add a script tag - I want my javascript to be embedded and not to be downloaded from a different source.添加脚本标签 - 我希望嵌入我的 javascript,而不是从其他来源下载。 A different source means longer page loads and i do not want my site to extend the page load.不同的来源意味着更长的页面加载,我不希望我的网站扩展页面加载。
  • Add an application proxy - This can also extend the loading time.添加应用程序代理 - 这也可以延长加载时间。

Can you suggest on what is the correct way to do it?你能建议什么是正确的方法吗?

Add a script tag and be done with it.添加一个脚本标签并完成它。 No other pattern offers up the same customer experience.没有其他模式可以提供相同的客户体验。 When they install your App, your script is available.当他们安装您的应用程序时,您的脚本可用。 When they uninstall your App, their theme reverts nicely.当他们卸载您的应用程序时,他们的主题会很好地恢复。 Worrying about the load times of your asset is not worth it if you have done your job, and minimized your code and provided a fast CDN address for it.如果您已经完成了工作,并且最小化了您的代码并为其提供了一个快速的 CDN 地址,那么担心资产的加载时间是不值得的。

In 2022, you get Theme App Extensions for OS 2.0. 2022 年,您将获得适用于 OS 2.0 的主题应用扩展。 Do that.!去做。! Best way to add JS to a theme/store.将 JS 添加到主题/商店的最佳方式。

Are you using the embedded apps SDK ?您正在使用嵌入式应用程序 SDK吗? I'm pretty sure they have some option that makes this easier, like you can create a "sidebar" on every page and position it wherever you want (hidden if necessary).我很确定他们有一些选项可以让这更容易,比如你可以在每个页面上创建一个“侧边栏”,并在任何你想要的地方创建 position(如有必要,隐藏)。

Answering you question: the good way is using ScriptTag.回答您的问题:好方法是使用 ScriptTag。 Consider this article on this: https://docs.shopify.com/api/unlinked/using-javascript-responsibly考虑这篇文章: https://docs.shopify.com/api/unlinked/using-javascript-responsibly

Still if you need to add code snippet as an asset, I want to point out that it's not necessary the customer must manually update the theme.尽管如此,如果您需要将代码片段添加为资产,我想指出客户不必手动更新主题。 As far as I know, you can add your snippet as an asset and then update another -- theme liquid template.据我所知,您可以将片段添加为资产,然后更新另一个主题液体模板。 https://docs.shopify.com/api/asset#update https://docs.shopify.com/api/asset#update

For the reasons described in an article I've provided above, you should not do that.由于我在上面提供的一篇文章中描述的原因,您不应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM