4
1 Comment

iFrame vs ShadowDOM?

I'm currently developing a chrome extension and I've just converted to using the shadow dom for style encapsulation. Is there any benefit from using an iFrame over this?

So far what comes to mind is:

  1. faster developing time: you can use local host to develop UI (using react and npm start) rather than wait for the app to build and refresh the extension.
  2. more modularity: can have your content script app in another code base.

However, I think there can be more complexities and overhead by using iFrames, especially when trying to pass messages back and forth from the background script.

Thoughts?

posted to Icon for group Browser Extension Makers
Browser Extension Makers
on January 7, 2021
  1. 2

    I think your analysis is basically right: there's a tradeoff between messaging complexity vs. development iteration ergonomics.

    Another benefit of iframes is that you're able to embed a separate website that can be updated at a different cadence as your web extension. There's a couple caveats though:

    1. You might have to do the iframe in an iframe trick though get around some in CSP limitations, which adds another layer of messaging. (IIRC, an iframe added by the content script should not be restricted by the sites frame-src directive, but it is in practice)
    2. It's not clear whether it'd be considered "remote code" for purposes of Chrome banning remote code in Manifest V3

    You can mitigate the downsides of Shadow DOM by using a package like webpack-extension-reloader to automatically reload the extension (and also any pages it's running on) on code updates.

Trending on Indie Hackers
6 weeks solo, 2 rejections, finally live but nobody told me marketing would be this hard User Avatar 87 comments Building ExpenseSpy solo, no funding — launching June 17 on iOS & Android User Avatar 39 comments Hi IH — quick update. The MVP is live. User Avatar 34 comments I built a $5/1k-listing CRE data API because CoStar is overkill for first-pass scans User Avatar 18 comments Day 7: 51 people answered my question. I wasn't ready for what they said. User Avatar 18 comments Building LinkCover – Day 3: Payment is live. No more building, time to sell. User Avatar 12 comments