perf: prefetch the bundle when the host page already loaded embed.js - #56
perf: prefetch the bundle when the host page already loaded embed.js#56sidgaikwad wants to merge 1 commit into
Conversation
The load listener prefetches the versioned bundle so the first createEditor does not pay a second round trip, but the early return for an already-installed window.ImageEditor bypassed it entirely. So on exactly the pages the tag-reuse logic exists to support — where the host injected embed.js itself — the prefetch never happened and the first mount was a full round trip slower than the injected path. The embed loader caches its own promise, so the duplicate call is a no-op when the host already triggered it. Also make the reused-tag timeout a parameter (defaulting to the exported REUSED_TAG_TIMEOUT_MS) rather than a hardcoded constant, so the bound is overridable and directly testable.
|
@sidgaikwad is attempting to deploy a commit to the Unlayer Team on Vercel. A member of the Team first needs to authorize it. |
lucasbesen
left a comment
There was a problem hiding this comment.
I don’t think we should merge this as written.
On the normal mount path, createEditor() runs immediately after await loadScript() and starts the bundle request itself. Prefetching here moves that request slightly earlier; it doesn’t eliminate an additional network round trip.
It also changes version selection for host-installed embeds. With embed.js already evaluated and no bundle loaded, passing options={{ version: '2.6.0' }} loads 2.6.0 on the base branch but 2.7.0 on this branch. The new load() call selects “latest” first, and the cached promise prevents createEditor() from honoring the pin. The mocked tests don’t catch this.
Please remove the prefetch change, or demonstrate a meaningful performance improvement while preserving explicit version selection and adding regression coverage.
The timeout parameter is reasonable internal cleanup, but it remains inaccessible to package consumers, so it doesn’t address consumer configurability yet.
Fixes #44.
1. The prefetch was skipped on host-injected pages (the real fix)
The
loadlistener prefetches the versioned bundle so the firstcreateEditordoesn't pay a second round trip — but the early return bypassed it:So on exactly the pages the tag-reuse logic exists to support — where the host injected
embed.jsitself — the prefetch never happened and the first mount was a full network round trip slower than the injected path.The embed loader caches its own promise, so the duplicate call is a no-op when the host already triggered it. The rejection is swallowed identically to the existing listener, since the same failure resurfaces through
createEditor.Red without the fix:
2. The reused-tag timeout
REUSED_TAG_TIMEOUT_MSis now exported andloadScripttakes it as an optional second argument, so the bound is overridable and directly testable rather than requiringvi.advanceTimersByTime(30_000).I deliberately stopped short of a component prop.
loadScriptisn't exported from the package entry point, so surfacing this to consumers means either exporting the module or adding a prop alongsidescriptUrl— an API decision that's yours, not mine. This change makes either one a one-liner later. Say the word and I'll add the prop in this PR.Verification
lint,typecheck,buildclean