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

How to handle a full browser reload #1947

Open
jagthedrummer opened this issue Mar 6, 2025 · 1 comment
Open

How to handle a full browser reload #1947

jagthedrummer opened this issue Mar 6, 2025 · 1 comment

Comments

@jagthedrummer
Copy link

Overview

What's the recommended way of handling the case where a user hits the "reload" button on their browser? When this happens the host param is missing (which is provided by Shopify when someone first navigates to the app) and then the call to createApp (from @shopify/app-bridge fails) with a complaint about host missing.

11:04:50.219 Uncaught (in promise) 
Object { name: "AppBridgeError", message: "APP::ERROR::INVALID_CONFIG: host must be provided", stack: "AppBridgeError@https://ga.jspm.io/npm:@shopify/[email protected]/actions/Error/index.js:1:1675\nfromAction@https://ga.jspm.io/npm:@shopify/[email protected]/actions/Error/index.js:1:1844\nvalidateAndDecodeConfig@https://ga.jspm.io/npm:@shopify/[email protected]/index.js:5:4112\ncreateClientApp$1/<@https://ga.jspm.io/npm:@shopify/[email protected]/index.js:5:2733\ncreateApp$1@https://ga.jspm.io/npm:@shopify/[email protected]/index.js:12:89\ninitShopifyApp@https://crowdfunder-octolabs.ngrok.io/assets/superliner/shopify_app-4337f031.js:187:16\ninitPage@https://crowdfunder-octolabs.ngrok.io/assets/superliner/shopify_app-4337f031.js:243:9\n@https://crowdfunder-octolabs.ngrok.io/assets/superliner/shopify_app-4337f031.js:258:3\n", action: undefined, type: "APP::ERROR::INVALID_CONFIG" }
shopify_app-4337f031.js:243:9

Possibly related to: #1768

Possibly moot if a splash page really isn't needed anymore: #1946

@jagthedrummer
Copy link
Author

After poking at this I discovered that the seemingly indecipherable host parameter is actually a Base64 encoded representation of the full URL (without protocol) to the admin for that shop. Something like admin.shopify.com/store/my-test-shop.

So I updated the shopify-app-init block that comes from here:

<%= content_tag(:div, nil, id: 'shopify-app-init', data: {
api_key: ShopifyApp.configuration.api_key,
shop_origin: @shop_origin || (@current_shopify_session.shop if @current_shopify_session),
host: @host,
debug: Rails.env.development?
} ) %>

To look like this:

    <%
      shop_origin = @shop_origin || (@current_shopify_session.shop if @current_shopify_session)
      fallback_host = Base64.strict_encode64("admin.shopify.com/store/#{shop_origin.split(".").first}")
    %>
    <%= content_tag(:div, nil, id: 'shopify-app-init', data: {
      api_key: ShopifyApp.configuration.api_key,
      shop_origin: shop_origin,
      host: @host || fallback_host,
      debug: Rails.env.development?
    } ) %>

And that made it so that hitting the reload button on the browser works correctly and leaves the app in a state where further navigation works correctly.

Can someone from Shopify comment about whether this is safe? And if not, could you make an alternate recommendation? And if so, maybe you could update the gem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant