Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor] Scrip tags manager #1948

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Unreleased
----------
- Adds a `script_tag_manager` that will automatically create script tags when the app is installed. [1948](https://github.com/Shopify/shopify_app/pull/1948)

22.5.1 (December 11, 2024)
----------
Expand Down
12 changes: 11 additions & 1 deletion app/controllers/shopify_app/callback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def perform_post_authenticate_jobs(session)
session_for_shop = session.online? ? shop_session : session

install_webhooks(session_for_shop)

install_scripttags(session_for_shop)
perform_after_authenticate_job(session)
end

Expand All @@ -161,6 +161,16 @@ def install_webhooks(session)
WebhooksManager.queue(session.shop, session.access_token)
end

def install_scripttags(session)
return unless ShopifyApp.configuration.has_script_tags?

ScriptTagsManager.queue(
session.shop,
session.access_token,
ShopifyApp.configuration.script_tags,
)
end

def perform_after_authenticate_job(session)
config = ShopifyApp.configuration.after_authenticate_job

Expand Down
52 changes: 52 additions & 0 deletions docs/shopify_app/script-tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Script Tags

ShopifyApp can manage your app's [Script Tags](https://shopify.dev/docs/admin-api/graphql/reference/online-store/scripttag) for you by setting which script tags you require in the initializer.
> [!NOTE]
> Script tags should only be used for vintage themes that do not support app blocks.

## Configuration

```ruby
ShopifyApp.configure do |config|
config.script_tags = [
# Basic script tag
{cache: true, src: 'https://example.com/fancy.js'},

# Script tag with template_types for app block detection
{
cache: true,
src: 'https://example.com/product-script.js',
template_types: ['product', 'collection']
}
]
end
```

## Required Scopes
Both the `write_script_tags` and `read_themes` scopes are required.

For apps created with the Shopify CLI, set these scopes in your `shopify.app.toml` file:

```toml
[access_scopes]
# Learn more at https://shopify.dev/docs/apps/tools/cli/configuration#access_scopes
scopes = "write_products,write_script_tags,read_themes"
```

For older apps, you can set the scopes in the initializer:

```ruby
config.scope = 'write_products,write_script_tags,read_themes'
```

## How It Works

### Script Tag Creation

Script tags are created in the same way as [Webhooks](/docs/shopify_app/webhooks.md), with a background job which will create the required scripttags.

### App Block Detection

When you specify `template_types` for a script tag, ShopifyApp will check if the store's active theme supports app blocks for those template types. If any template type doesn't support app blocks, the script tags will be created as a fallback

This allows your app to automatically adapt to the store's theme capabilities, using app blocks when available and falling back to script tags when necessary.
3 changes: 2 additions & 1 deletion lib/shopify_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ def self.use_webpacker?

# jobs
require "shopify_app/jobs/webhooks_manager_job"
require "shopify_app/jobs/script_tags_manager_job"

# managers
require "shopify_app/managers/webhooks_manager"

require "shopify_app/managers/script_tags_manager"
# middleware
require "shopify_app/middleware/jwt_middleware"

Expand Down
8 changes: 8 additions & 0 deletions lib/shopify_app/auth/post_authenticate_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def perform(session)
session_for_shop = session.online? ? shop_session(session) : session

install_webhooks(session_for_shop)
install_scripttags(session_for_shop)

perform_after_authenticate_job(session)
end
Expand All @@ -27,6 +28,13 @@ def install_webhooks(session)
WebhooksManager.queue(session.shop, session.access_token)
end

def install_scripttags(session)
ShopifyApp::Logger.debug("PostAuthenticateTasks: Installing scripttags")
return unless ShopifyApp.configuration.has_script_tags?

ScriptTagsManager.queue(session.shop, session.access_token, ShopifyApp.configuration.script_tags)
end

def perform_after_authenticate_job(session)
ShopifyApp::Logger.debug("PostAuthenticateTasks: Performing after_authenticate_job")
config = ShopifyApp.configuration.after_authenticate_job
Expand Down
10 changes: 5 additions & 5 deletions lib/shopify_app/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Configuration
attr_accessor :embedded_app
alias_method :embedded_app?, :embedded_app
attr_accessor :webhooks
attr_accessor :scripttags
attr_accessor :script_tags
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like the rename 👍

attr_accessor :after_authenticate_job
attr_accessor :api_version

Expand All @@ -33,7 +33,7 @@ class Configuration
attr_accessor :custom_post_authenticate_tasks

# customise ActiveJob queue names
attr_accessor :scripttags_manager_queue_name
attr_accessor :script_tags_manager_queue_name
attr_accessor :webhooks_manager_queue_name

# configure myshopify domain for local shopify development
Expand All @@ -58,7 +58,7 @@ def initialize
@root_url = "/"
@myshopify_domain = "myshopify.com"
@unified_admin_domain = "shopify.com"
@scripttags_manager_queue_name = Rails.application.config.active_job.queue_name
@script_tags_manager_queue_name = Rails.application.config.active_job.queue_name
@webhooks_manager_queue_name = Rails.application.config.active_job.queue_name
@disable_webpacker = ENV["SHOPIFY_APP_DISABLE_WEBPACKER"].present?
@scope = []
Expand Down Expand Up @@ -115,8 +115,8 @@ def has_webhooks?
webhooks.present?
end

def has_scripttags?
scripttags.present?
def has_script_tags?
script_tags.present?
end

def requires_billing?
Expand Down
3 changes: 2 additions & 1 deletion lib/shopify_app/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module RedactJobParams
private

def args_info(job)
log_disabled_classes = ["ShopifyApp::WebhooksManagerJob"]
log_disabled_classes = ["ShopifyApp::WebhooksManagerJob", "ShopifyApp::ScriptTagsManagerJob"]
return "" if log_disabled_classes.include?(job.class.name)

super
Expand All @@ -30,6 +30,7 @@ class Engine < Rails::Engine
ActiveSupport.on_load(:active_job) do
if ActiveJob::Base.respond_to?(:log_arguments?)
WebhooksManagerJob.log_arguments = false
ScriptTagsManagerJob.log_arguments = false
elsif ActiveJob::Logging::LogSubscriber.private_method_defined?(:args_info)
ActiveJob::Logging::LogSubscriber.prepend(RedactJobParams)
end
Expand Down
16 changes: 16 additions & 0 deletions lib/shopify_app/jobs/script_tags_manager_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module ShopifyApp
class ScriptTagsManagerJob < ActiveJob::Base
queue_as do
ShopifyApp.configuration.script_tags_manager_queue_name
end

def perform(shop_domain:, shop_token:, script_tags:)
ShopifyAPI::Auth::Session.temp(shop: shop_domain, access_token: shop_token) do |session|
manager = ScriptTagsManager.new(script_tags, shop_domain)
manager.create_script_tags(session: session)
end
end
end
end
Loading