Using the new @shopify/app-bridge with @shopify/polaris-react

I am unsure how to use the new @shopify/app-bridge and @shopify/polaris together. For example, I have tried to make a React component that will use the app-bridge and polaris to display a toast.

import React, { Component } from "react";
import * as PropTypes from "prop-types";
import { Toast } from "@shopify/app-bridge/actions";
import { Page } from "@shopify/polaris";

class Start extends Component {
  static contextTypes = {
    polaris: PropTypes.object
  };

  showToast() {
    console.log("SHOW TOAST");
    console.log(this.context.polaris.appBridge);
    const toastNotice = Toast.create(this.context.polaris.appBridge, {
      message: "Test Toast",
      duration: 5000
    });
    toastNotice.dispatch(Toast.Action.SHOW);
  }

  render() {
    this.showToast();
    return (
      <Page title="Do you see toast?">
        <p>I do not see toast.</p>
      </Page>
    );
  }
}

export default Start;

But it does not seem to dispatch the action. Any ideas on why not? Note that my app is wrapped in the AppProvider and app-bridge is initialized.

ReactDOM.render(
  <AppProvider
    apiKey={process.env.REACT_APP_SHOPIFY_API_KEY}
    shopOrigin={queryString.parse(window.location.search).shop}
  >
    <Start />
  </AppProvider>,
  document.getElementById("root")
);

Any ideas on why this doesn’t work?

Hi Chad! I’m puzzled too; your code looks good. What version of Polaris are you using?

Hi Brian, these are my versions:

"dependencies": {
    "@shopify/polaris": "^3.11.0",
    "query-string": "^6.4.0",
    "react": "^16.8.5",
    "react-dom": "^16.8.5",
    "react-redux": "^6.0.1",
    "react-router-dom": "^5.0.0",
    "react-scripts": "2.1.8",
    "redux": "^4.0.1"
  }

Hmmm, I don’t see any problems. Two more things to try:

  1. Are there any errors or warnings in the console when you run your app in Shopify Admin?

  2. Follow our documentation on using the Redux DevTools and let me know what you see when the app loads.

One warning in the console:

[bugsnag] notify endpoint is set but sessions endpoint is not. No sessions will be sent.

And no sign of my toast action in Redux Dev Tools:

Hi Chad, Hanna here taking over for Iain while he’s away.

The code in your OP looks to be correct, I’m curious about the reason behind using the Toast action from App Bridge vs Feedback indicators from Polaris (https://polaris.shopify.com/components/feedback-indicators/toast#section-use-in-an-embedded-application). The difference between the two can be a bit confusing, and I was wondering if there was a particular reason for using the Toast action?

Another question I have is whether you have @shopify/app-bridge in your package.json?

Hi Hanna. I’m just using the Toast as a way of proving that the app-bridge is working. Is there another App-Bridge action I should test?

My dependencies:

"dependencies": {
    "@shopify/app-bridge": "^1.2.0",
    "@shopify/polaris": "^3.11.0",
    "query-string": "^6.4.0",
    "react": "^16.8.5",
    "react-dom": "^16.8.5",
    "react-redux": "^6.0.1",
    "react-router-dom": "^5.0.0",
    "react-scripts": "2.1.8",
    "redux": "^4.0.1"
  }

I see! In that case, could you please confirm that @shopify/app-bridge is in your package.json?

see above :slight_smile:

Oops, thanks! Hmm, it’s still not working even with @shopify/app-bridge package installed? ?

Nope :frowning:

Any other ideas? Do you have a repo with App-Bridge and React working together?

I ran out of ideas :frowning: So I wanted to try out your code in my own test app: https://github.com/hannachen/polaris-app-test

Somehow, it appears to be working for me after I added the necessary npm packages! Looking at packages.json, index.jsx, and Start.jsx, does anything stand out as different from your setup?

They look the same! Can I invite you to a DEV store so you can see my app deployed and confirm it doesn’t work. Email me at hello@saskatoonlabs.com and I’ll add you.

Well with help from @hannachen we tracked down the bug. It turns out that inside App Bridge, before taking any action, they check that the localOrigin matches the appURL (one that’s entered in the partners dashboard). In my case, I have a backend (node.js on heroku used for authentication) and a frontend (react bundle on firebase) my app starts by hitting the backend, and then if authentication checks out, it redirects to the front end. And hence the localOrigin does not match… hmmm, I’m very glad to have figured this out since I lost a lot of sleep over it. Now the question is what to do about it… maybe this is something that could be updated with AppBridge? Or is there a better design I should be considering?

Hey Chad! After consulting with my team, it’s recommended that both front-end and back-end live under the same hostname. That’s because the check is in place for security. We need to ensure apps are being served from an expected source, and not redirected to an arbitrary URL. So unfortunately, it’s not something that can be changed on the App Bridge side.

I normally look into frameworks that will handle both front-end and back-end for me under the same project. Might something like NextJS work for you? https://help.shopify.com/en/api/tutorials/build-a-shopify-app-with-node-and-react

Next.js is a good suggestion. I’ll look into it.

Man am I glad I found this thread. I think there are a couple of points I would like to make:

  • It’s fine for that to be an explicit policy decision, but right now appbridge fails silently and without error. This is extremely frustrating, since many people build react and SPA apps using a client/server model. I designed my application the same way as chad, and I think there are many others. I would have taken a different approach if the documentation around appBridge and AppProvider were better documented and/or threw the errors.
  • The security model is a good one, but appBridge should allow for other hosts whitelisted in the redirect section of the app settings in Shopify Partners, not just the root appurl.