r/javascript • u/Visible_Head_6828 • 4d ago
MatFrame: a drop-in <iframe> replacement that resizes itself, talks with Promises and shows a fallback when blocked
https://github.com/bossartemii/matframe3
u/genericallyloud 4d ago
what does the Mat part stand for?
2
u/Visible_Head_6828 2d ago
It comes from "Material You", Google's design language where the UI adapts to the user. Same idea here: the frame adapts to its content (height) and to the host page (theme) instead of being a fixed box. Not affiliated with Google, just the inspiration.
1
u/Discordbothost 2d ago
How do you tell blocked apart from just slow? A strict CSP or X-Frame-Options frame will not reliably fire load or error, so is the fallback purely a timeout, or do you also probe contentWindow access?
1
u/Visible_Head_6828 2d ago
Good question: it can't reliably tell them apart, and I don't claim it does. CSP / X-Frame-Options blocks still fire load in most browsers, and contentWindow access throws for any cross-origin frame, so neither is a usable signal.
The fallback is a handshake timeout: if the child doesn't call MatFrame.child.init() within N ms, you get the fallback. That covers blocked, broken and slow alike, which is why it offers Retry and Open in new tab instead of claiming a cause.
One improvement I'm adding: if load has fired but no handshake arrives, it's much more likely blocked or non-cooperating than slow, so the message can differ between those two cases.
1
u/Visible_Head_6828 2d ago
Shipped in v0.1.1: mf-error now carries a reason, 'timeout' (never loaded) or 'no-handshake' (loaded but never answered). Still can't tell blocked from "doesn't use MatFrame", and the docs now say so explicitly.
-4
u/Visible_Head_6828 4d ago
Author here. Zero dependencies, ~4 KB gzipped, works in any framework.
const info = await frame.call('getInfo') // parent → child MatFrame.child.call('save', data) // child → parent
Plus auto-height (also cross-origin, the child includes the same script), lazy loading with skeleton, handshake/timeout fallback, sandbox presets, theme sync. TypeScript types included.
Live demo: https://bossartemii.github.io/matframe/
What would you add to the API?
1
u/anderson_a-7952 4d ago
An explicit origin allowlist for RPC would make the cross-origin trust boundary clear at the call site.
1
u/Visible_Head_6828 2d ago
Agreed, the trust boundary should be visible where the RPC is defined, not only on the element. Right now
originaccepts a single origin for the whole frame. For v0.1.1 I'm adding an explicit allowlist:<mat-frame origin="https://a.com https://b.com"> frame.expose(handlers, { origins: ['https://a.com'] }) MatFrame.child.init({ parentOrigins: ['https://host.com'] })
Messages from anything else are dropped before they reach a handler.
1
u/Visible_Head_6828 2d ago
Shipped in v0.1.1: origin="https://a.com https://b.com" on the element, expose(handlers, { origins: [...] }) per method, and init({ parentOrigins: [...] }) in the child. It also warns in the console when the sandbox would make the origin "null". Thanks!
1
u/jjohnson-13233 2d ago
I'd add per-call cancellation, perhaps via AbortSignal, so callers can abandon an RPC result when navigation or teardown makes it irrelevant. The child may need explicit cancellation semantics too.
1
u/Visible_Head_6828 2d ago
Great idea, and it fits how fetch already works. Plan for v0.1.1:
const ctrl = new AbortController(); const info = await frame.request('getInfo', [id], { signal: ctrl.signal, timeout: 3000 }); ctrl.abort(); // rejects with AbortError, frees the pending entry
The abort is also sent to the child, and the handler gets its own signal (this.signal) so it can stop work too. Reloading or removing the frame aborts everything pending.
1
u/Visible_Head_6828 2d ago
Shipped in v0.1.1: frame.request('method', args, { signal, timeout }) on both sides. Aborting rejects with AbortError, notifies the other side, and the handler gets this.signal so it can stop work (e.g. pass it to fetch). Thanks for the idea!
15
u/NeetMastery 4d ago
This isn’t “a better <iframe>”; it serves a fundamentally different purpose. The purpose of an <iframe> is to display the content of one page in another. Any other behavior varies highly by the application of the tag. For example, this is a significantly clumsier tag for use in websites which use iframes for fixed, structured navigation - like the POSIX spec page. Or it simply is too much for the purpose of most use-cases; for example “theme synchronization” depends on what you define a theme to be. Do you include border radii, element layouts for different effects per theme, the ability to trigger javascript to switch layouts? If not, how do these trigger that is better than a native <iframe>? If so, what is the memory and performance impact at scale, network bandwidth for element setup for those who don’t need these? How are you handling cross-origin frames where the target isn’t implementing the theme format you’ve specified?
Your custom tag can be useful. But the web has been designed and evolved over decades to be helpful in the generic case and lightweight, and all the functionality you have can be done in a native fashion in the places where it’s helpful. This isn’t a “better <iframe>”.