How to redirect from inside a Polaris component in an embedded app?
I am attempting to redirect customers of my app to the confirmation_url to accept a billing charge in a Shopify Polaris embedded app. Per the billing API documentation, once we create the charge we need to redirect merchants to the charge's confirmation_url.
Neither a standard server-side nor client side redirect work due to CORS, so I pursued the App Provider documentation which describes creating redirects at the absolute top level of the app where we pass the API key to the AppProvider, and then creating a redirect via const redirect = Redirect.create(... at the same time.
Is the idea here that we would then pass redirect down as a prop down the component chain to every component that might need to make a redirect? The reason I ask is that it doesn't seem like the method implied for creating redirects would enable creating them at the component level.
Hi @paulisloud! Sorry for our delay in getting to your question. Were you able to find the info you needed in the Redirect documentation linked from the app provider page you mentioned?
Thanks for getting back to me @dpersing, but unfortunately that documentation also asks you to create the redirect instance using the shopOrigin and apiKey, meaning we would need to:
createAppwhile in a component, which is inside the app already, and passshopOriginandapiKeyall the way down to the component, or- should we instead create
redirectat the top level of the app and attach it to state and make it available to components around via Redux / Context?
Or, is there some other way to instantiate the redirect while already in a component, say, a Card on a Page and perhaps I am making this harder than it needs to be?
@paulisloud not sure if this helps you but I just started playing around with this library and got a redirect working using something like this
import { Redirect } from "@shopify/app-bridge/actions";
class ProductForm extends React.Component {
static contextTypes = {
polaris: PropTypes.object
};
handleSubmit = () => {
// after form is submitted redirect to admin product show page
const redirect = Redirect.create(this.context.polaris.appBridge);
return redirect.dispatch(
Redirect.Action.ADMIN_PATH,
`/products/${product.id}`
);
}
this is in a component that is a child of app provider
<AppProvider apiKey={apiKey}>
<ProductForm />
</AppProvider>
Hi @WesKetch! I followed the guidance on https://polaris.shopify.com/components/structure/app-provider. Redirect should work after button click, no errors appears but no redirect also. I have attached my source code briefly. Could you tell me why it doesn't works for me?
import * as PropTypes from 'prop-types';
import { Redirect } from "@shopify/app-bridge/actions";
class Help extends Component {
static contextTypes = {
polaris: PropTypes.object
};
gotoInstallPage = () => {
const redirect = Redirect.create(this.context.polaris.appBridge);
return redirect.dispatch(Redirect.Action.APP,'/install');
}
render() {
return(
<Link monochrome onClick={() => this.gotoInstallPage()}>troubleshooting</Link>
)
}
}
Edit: Turned out to be my own fault.
Are you sure your "App URL" matches the URL where the app is actually loaded from? I had exactly the same behavior, but it turned out there was a mismatch between the "App URL" and the origin of the postMessage the AppBridge was sending. Please see this issue: #1332
Hi @marguskoiduste! Yes, I have checked App URL and the origin of the postMessage the AppBridge. Everything seems okay but redirect is not working. Currently I am using react-redux in a polaris react. I think both of libraries doesn't compatible each other.
For me the problem was that I had "https://" preceding the app URL. If your including it, try taking it out (ex: "store.myshopify.com" instead of "https://store.myshopify.com") and see if that helps?
This issue has been inactive for 180 days and labeled with Icebox. It will be closed in 7 days if there is no further activity.
How would you do this within a FC? Where are the docs in how to access the App Bridge context?
https://shopify.dev/tools/app-bridge/react-components/provider#accessing-the-app-bridge-client-directly
Seems like you can do this for example:
import { useAppBridge } from '@shopify/app-bridge-react'
import { MediaCard } from '@shopify/polaris'
import { Redirect } from '@shopify/app-bridge/actions'
export const Component: React.FC<IComponentProps> = () => {
const app = useAppBridge()
const redirect = Redirect.create(app)
const params = new URLSearchParams({
resource_name: 'Product',
edit: editFields.join(',')
})
redirect.dispatch(Redirect.Action.ADMIN_PATH, {
path: `/bulk?${params.toString()}`,
newContext: true
})
}
I'm using an older version of app-bridge-react that doesn't have useAppBridge, so I used useContext for now.
Ref: https://shopify.dev/tools/app-bridge/react-components/provider
import React, { useContext } from 'react';
import { Context } from '@shopify/app-bridge-react';
import { Redirect } from '@shopify/app-bridge/actions';
const YourPage: React.FC = () => {
const app = useContext(Context);
const redirect = Redirect.create(app);
...
redirect.dispatch(Redirect.Action.APP, '/otherpage?somequeryparam=123');