From fd775a15cb951b66fdb2a1a927917b0e0bec8944 Mon Sep 17 00:00:00 2001 From: Rezaan Syed Date: Thu, 1 Apr 2021 15:49:27 -0400 Subject: [PATCH] Support App Bridge 2.0 in shopify_app --- docs/Troubleshooting.md | 15 ++++++++++++++- .../home_controller/templates/index.html.erb | 2 +- .../templates/unauthenticated_home_controller.rb | 1 + .../install/templates/embedded_app.html.erb | 3 ++- .../shopify_app/install/templates/shopify_app.js | 2 +- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/Troubleshooting.md b/docs/Troubleshooting.md index f45936e1e..c7f32bce4 100644 --- a/docs/Troubleshooting.md +++ b/docs/Troubleshooting.md @@ -16,6 +16,8 @@ * [My app is still using cookies to authenticate](#my-app-is-still-using-cookies-to-authenticate) * [My app can't make requests to the Shopify API](#my-app-cant-make-requests-to-the-shopify-api) +[Migrating to App Bridge 2.0](#migrating-to-app-bridge-2.0) + ## Generators ### The shopify_app:install generator hangs @@ -138,4 +140,15 @@ _Example:_ If your embedded app cannot handle server-side XHR redirects, then co X-Shopify-API-Request-Failure-Unauthorized: true ``` -Then, use the [Shopify App Bridge Redirect](https://shopify.dev/tools/app-bridge/actions/navigation/redirect) action to redirect your app frontend to the app login URL if this header is set. \ No newline at end of file +Then, use the [Shopify App Bridge Redirect](https://shopify.dev/tools/app-bridge/actions/navigation/redirect) action to redirect your app frontend to the app login URL if this header is set. + +## Migrating to App Bridge 2.0 + +In order to upgrade your embedded app to the latest App Bridge 2.0 version, please refer to the [migration guide](https://shopify.dev/tutorials/migrate-your-app-to-app-bridge-2). + +To ensure that your app's embedded layout doesn't import App Bridge 2.0 before fully migrating, make the following change to bind it to v1.x. + +```diff + - + + +``` diff --git a/lib/generators/shopify_app/home_controller/templates/index.html.erb b/lib/generators/shopify_app/home_controller/templates/index.html.erb index 9ef304c55..f32af53c9 100644 --- a/lib/generators/shopify_app/home_controller/templates/index.html.erb +++ b/lib/generators/shopify_app/home_controller/templates/index.html.erb @@ -18,7 +18,7 @@ // Save a session token for future requests window.sessionToken = await new Promise((resolve) => { - app.subscribe(SessionToken.ActionType.RESPOND, (data) => { + app.subscribe(SessionToken.Action.RESPOND, (data) => { resolve(data.sessionToken || ""); }); }); diff --git a/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb b/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb index 1e8328311..6b5fd9414 100644 --- a/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +++ b/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb @@ -7,5 +7,6 @@ class HomeController < ApplicationController def index @shop_origin = current_shopify_domain + @host = params[:host] end end diff --git a/lib/generators/shopify_app/install/templates/embedded_app.html.erb b/lib/generators/shopify_app/install/templates/embedded_app.html.erb index e95020a08..04865cc23 100644 --- a/lib/generators/shopify_app/install/templates/embedded_app.html.erb +++ b/lib/generators/shopify_app/install/templates/embedded_app.html.erb @@ -24,11 +24,12 @@ <%= render 'layouts/flash_messages' %> - + <%= content_tag(:div, nil, id: 'shopify-app-init', data: { api_key: ShopifyApp.configuration.api_key, shop_origin: @shop_origin || (@current_shopify_session.domain if @current_shopify_session), + host: @host, debug: Rails.env.development? } ) %> diff --git a/lib/generators/shopify_app/install/templates/shopify_app.js b/lib/generators/shopify_app/install/templates/shopify_app.js index a9d3a4c2b..b3a70c789 100644 --- a/lib/generators/shopify_app/install/templates/shopify_app.js +++ b/lib/generators/shopify_app/install/templates/shopify_app.js @@ -4,7 +4,7 @@ document.addEventListener('DOMContentLoaded', () => { var createApp = AppBridge.default; window.app = createApp({ apiKey: data.apiKey, - shopOrigin: data.shopOrigin, + host: data.host, }); var actions = AppBridge.actions;