Shopify / shopify-app-bridge

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

App bridge reloads app multiple times on first open #265

Closed quard8 closed 3 months ago

quard8 commented 6 months ago

Describe the bug

I started debugging my LCP score and found out that app loading twice. Because of that all JS code started loading multiple times and I guess LCP score impacted.

I checked "Search and Discovery" app and found same behaviour. On app without App Bridge from CDN everything working as expected.

I found that App Bridge script send messages "APP::NAVIGATION::HISTORY::REPLACE" to host and looks like this is reason to reload entire app.

Screenshot 2024-01-04 at 00 55 11

Code from minified script:

"./navigation.ts": ({protocol: t})=>{
            const n = self.navigation;
            function e(n, e) {
                if (!n)
                    return;
                const o = f(new URL(n,location.href))
                  , i = `${o.pathname}${o.search}${o.hash}`;
                l && t.send("Navigation.history." + e, {
                    path: i
                })
            }

l is always true because of l = window.name.startsWith("app-iframe")

To Reproduce

Steps to reproduce the behaviour:

  1. Launch the "Search and Discovery" app
  2. See network tab with multiple loads of the app
Screenshot 2024-01-04 at 01 22 33 Screenshot 2024-01-04 at 01 22 37

Expected behaviour

Embedded app loads once

Contextual information

Packages and versions

List the relevant packages you’re using, and their versions. For example:

Platform

expertvillagemed commented 5 months ago

Hi quard8, I am facing the same issue. Did you get any solution for it? Please share here if you have any solution.

kyakaze commented 5 months ago

Yea we got this same issue, the app sends request twice, the first is cancelled, ~but our server already processes the request.~ . Actually when the app is pin in the list, shopify will automatically send request to all apps as if user is actually visit them, making our server handle the same request twice.

wbelk commented 4 months ago

I've debugged this as well. The Shopify admin is loading my app into three separate iframes, causing three requests to my index route each page reload

iframe[name="app-iframe"]
iframe[name="modal://content/classic"]
iframe[name="modal://content/max"]

To me this seems like a bug and inefficient, and makes my debugging much more difficult, what am I missing?

trev0r commented 4 months ago

Same issue here as well

wbelk commented 4 months ago

Yea we got this same issue, the app sends request twice, the first is cancelled, ~but our server already processes the request.~ . Actually when the app is pin in the list, shopify will automatically send request to all apps as if user is actually visit them, making our server handle the same request twice.

Yes I noticed this as well. I have 5 apps. If I'm on my dev shop it can be a nightmare.

awd commented 4 months ago

This seems to be especially problematic when opening the app from a nested link (think of a support tool or email to the merchant).

TheSecurityDev commented 3 months ago

Yea we got this same issue, the app sends request twice, the first is cancelled, ~but our server already processes the request.~ . Actually when the app is pin in the list, shopify will automatically send request to all apps as if user is actually visit them, making our server handle the same request twice.

I've noticed when this happens, it includes a prefetch=1 query parameter, so you can just ignore requests with that parameter in your server.

henrytao-me commented 3 months ago

Hi all, there are serval related issues here. I would like to summarize as below:

I will close this ticket once the canceled request is merged.