What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How to integrate App Bridge's ContextualSaveBar with client-side routing (next/router) ?

Solved

How to integrate App Bridge's ContextualSaveBar with client-side routing (next/router) ?

mashabow
Shopify Partner
9 1 9

Hi, I'm developing an embedded app with Next.js.

I added App Bridge's ContextualSaveBar to my form, but it seems to fail to block next/router's navigation.

Nov-30-2020 18-23-32.gif

I also tried with useClientRouting and useRoutePropagation hooks, but I have no success. How can I make it work?

Here is my sample code: https://github.com/mashabow/shopify-contextual-save-bar-and-next-router

 

Steps

  1. Input some text to the TextField.
  2. ContextualSaveBar is shown.
  3. Click "Go to another page" link.
  4. ContextualSaveBar's confirmation modal appears.

Expected behavior

5. Stay on the current page until the user clicks "Leave page" in the modal.

Actual behavior

5. "another page" is immediately loaded behind the modal 😞

 

Accepted Solution (1)

Trish_Ta
Shopify Staff
58 13 29

This is an accepted solution.

Hi Mashabow,

The redirect is happening inside your app so App Bridge is not notified and is not able to block it. You have 2 options:

A) Instead of passing the url as in `href`, you can add an `onClick` handler to the Link and dispatch an App Bridge app redirect action: https://shopify.dev/tools/app-bridge/actions/navigation/redirect#redirect-to-a-relative-path-in-the-.... This allows App Bridge to know about the redirect and automatically block it

B) Implement your own handler for blocking the route change with Next.js by listening to the `routeChangeStart` event: https://github.com/vercel/next.js/discussions/12348#discussioncomment-131768. You could create your own leave confirmation modal by bringing up an App Bridge Message Modal. 

Trish | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

View solution in original post

Replies 2 (2)

Trish_Ta
Shopify Staff
58 13 29

This is an accepted solution.

Hi Mashabow,

The redirect is happening inside your app so App Bridge is not notified and is not able to block it. You have 2 options:

A) Instead of passing the url as in `href`, you can add an `onClick` handler to the Link and dispatch an App Bridge app redirect action: https://shopify.dev/tools/app-bridge/actions/navigation/redirect#redirect-to-a-relative-path-in-the-.... This allows App Bridge to know about the redirect and automatically block it

B) Implement your own handler for blocking the route change with Next.js by listening to the `routeChangeStart` event: https://github.com/vercel/next.js/discussions/12348#discussioncomment-131768. You could create your own leave confirmation modal by bringing up an App Bridge Message Modal. 

Trish | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

mashabow
Shopify Partner
9 1 9

Thank you, Trish!

A) Instead of passing the url as in `href`, you can add an `onClick` handler to the Link and dispatch an App Bridge app redirect action: https://shopify.dev/tools/app-bridge/actions/navigation/redirect#redirect-to-a-relative-path-in-the-.... This allows App Bridge to know about the redirect and automatically block it

I tried this way at first, but it seems unable to block the route change, too.

https://github.com/mashabow/shopify-contextual-save-bar-and-next-router/commit/b1a5d9bb6dbb4144b8ecf...

B) Implementation your own handler for blocking the route change with Next.js by listening to the `routeChangeStart` event: https://github.com/vercel/next.js/discussions/12348#discussioncomment-131768. You could create your own leave confirmation modal by bringing up an App Bridge Message Modal. 

I can block in this way, thanks! I didn't know that workaround of next/router.

https://github.com/mashabow/shopify-contextual-save-bar-and-next-router/commit/001b263286185e3a62045...

Unfortunately, `router.event.on` doesn't seem to accept Promise or async function, so it still needs some work to use App Bridge Modal. But anyway, this is the behavior I wanted.