Shopify / shopify-app-bridge

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

App Bridge v4 CDN check always fails on Cloudflare #229

Closed uurcank closed 9 months ago

uurcank commented 11 months ago

There is a CDN check added to app bridge to ensure that it is loaded from CDN as the first script in the document.

However this does not work with Cloudflare. if the app is proxied through Cloudflare, it gets injected cdn-cgi scripts which are always loaded before any of the app's scripts, and then this CDN check of App bridge v4 fails.

There is no need to enforce a first item position for app bridge. It should converted into a warning rather than an error and app bridge should continue to load.

the related code piece is this

try { if (!document.currentScript) return console.error('The script tag loading App Bridge hastype="module"'), !1; const t = document.currentScript; return t.async ? (console.error("The script tag loading App Bridge hasasync"), !1) : t.defer ? (console.error("The script tag loading App Bridge hasdefer."), !1) : t.src ? new URL(t.src).hostname != b ? (console.error("The script tag loading App Bridge is not loading App Bridge from the Shopify CDN."), !1) : 0 === [...document.scripts].filter((t=>function(t) { return !!t.src && !t.defer && !t.async && "module" !== t.type && !t.dataset.appBridgeCompatible && /^ *(|(text|application)\/(x-)?(java|ecma)script) *$/i.test(t.type) }(t))).indexOf(t) || (console.error("The script tag loading App Bridge must be the first script tag in the document."), !1) : (console.error("The script tag loading App Bridge is not loading App Bridge from the Shopify CDN."), !1) } catch (t) { return console.error("App Bridge failed to self-validate", t), !1 } }())

uurcank commented 9 months ago

@charlesdobson sorry to buzz you but any update on this? it has been a month and script check is still there.

the code should be refactored to ignore cloudflare scripts

uurcank commented 9 months ago

i found that cloudflare injects a script before shopify app bridge, if cloudflare apps are enabled. Disabling cloudflare apps fixes the issue.

charlesdobson commented 9 months ago

@uurcank no problem for the ping!

Does that work for you with Cloudflare apps disabled then or do you normally use that?

uurcank commented 8 months ago

@charlesdobson it works with Cloudflare apps disabled.

I do not normally use that though I can see other devs using it. I am still not sure if it is a good idea to enforce first position for app bridge.