Shopify / shopify-app-bridge

https://shopify.dev/docs/api/app-bridge
85 stars 9 forks source link

Optimized mobile loading - getSessionToken does not resolve when new token is about to be issued #163

Closed galmis closed 1 year ago

galmis commented 1 year ago

Describe the bug

When optimized mobile loading is enabled, getSessionToken does not resolve if called at the end of token's life cycle (unless the getSessionToken function is called again when the token is available).

The issue affects the authenticatedFetch utility function, which I'm assuming, is relying on the getSessionToken under the hood.

To Reproduce

Steps to reproduce the behaviour:

  1. Enable "Optimized loading on mobile devices".
  2. Open an embedded app in Shopify Admin mobile app.
  3. Call getSessionToken at the end of token's life cycle (e.g. ~50+ seconds after it was issued). The following snippet and the attached console log highlight the issue.
  const app = useAppBridge();

  useEffect(() => {

    const intervalMs = 20000;

    const interval = setInterval(async() => {

      const requestTime = new Date().toISOString();
      // @ts-ignore
      console.log(`${requestTime} request session token`);

      try {
        const token = await getSessionToken(app);
        // @ts-ignore
        console.log(`${new Date().toISOString()} session token - ${token}, requestTime - ${requestTime}`);
      } catch(e) {
        console.error(`getSessionToken failed, requestTime - ${requestTime}`);
      }

    }, intervalMs)

    return () => {
      console.log(`clearing interval...`);
      clearInterval(interval);
    }
  }, [app]);

Screenshot 2022-12-18 at 14 21 20

Expected behaviour

Ideally, getSessionToken should always resolve with a session token. At the very least, I'd expect getSessionToken to throw an error if the token can't be issued.

Contextual information

Packages and versions

Platform

Additional context

Next.js embedded app created with Shopify cli 2.

The issue isn't reproducible when the optimized mobile loading is disabled. I've mainly tested this on Android 11 but noticed the same side effects on iOS 16.1.1. I couldn't reproduce it on iPad 15.1, though.

janklimo commented 1 year ago

Seeing the same issue. @shopify/app-bridge-utils@3.5.0 but also initially tested with version 3.2.6. Can't replicate the problem when optimized mobile loading is turned off.

Edit: this seems to be happening regardless if optimized mobile loading is on or off – eventually calling await getSessionToken(app) fails to resolve or throw.

I'd appreciate if we can escalate the severity of this. Most consumers of the API will await the token before making an API call. Doing that will completely freeze the app for the end user and the app developers will be the ones to get the blame from the merchants.

henrytao-me commented 1 year ago

@janklimo @galmis can I know what version of Shopify Mobile you are using?

henrytao-me commented 1 year ago

I believe that the issue has been fixed in the latest version of Shopify Mobile 9.94.0. Feel free to reopen if necessary. Thanks for reporting this issue 🙇