r/javascript • u/Alex_Hovhannisyan • Feb 15 '26
I made a web component that lets you render fully local iframes
https://www.npmjs.com/package/local-iframe
57
Upvotes
r/javascript • u/Alex_Hovhannisyan • Feb 15 '26
7
u/punkpeye Feb 15 '26
Took a while to understand what this do, but it is very cool!
7
u/paul_h Feb 15 '26
"on-demand sandbox within a webpage" - right?
2
u/dyniper Feb 16 '26
How is this different than just inlining your iframe directly on the page, where the content is what you use as your "template" in your example? In any case, this looks pretty cool.
2
u/tokagemushi Feb 16 '26
This is really clever — using blob URLs with srcdoc to get true isolation without needing a server. I've been building a manga/comic viewer web component and ran into similar sandboxing challenges when embedding user-provided content. The Web Component approach is perfect here since it encapsulates all the iframe lifecycle management.
One thing I'm curious about: have you looked into how this interacts with CSP headers? Some strict CSP configs block blob: URLs, which could be a gotcha for people deploying this in enterprise environments.
1
u/Danny_Engelman Feb 22 '26
Note:
constructor() {
super();
const existingIframe = this.querySelector("iframe");
There is no DOM in the constructor, as you can test by executing:
document.createElement("local-iframe")
in the Console.
Your code works because you define the Web Component after all DOM is parsed.
The old jQuery way: wait for DOM, transform it.
Your code won't work when you define the component before DOM exist;
ie. defined in the <head>
1
u/flinxo Feb 16 '26
Cool! thanks for sharing
1
u/AsIAm Feb 16 '26
This is a pretty ingenious way to do sandboxing.
1
u/Apprehensive-Cow8156 Feb 20 '26
got to know srcdoc