Skip to content

perf: prefetch the bundle when the host page already loaded embed.js - #56

Open
sidgaikwad wants to merge 1 commit into
unlayer:mainfrom
sidgaikwad:perf/prefetch-on-host-injected
Open

perf: prefetch the bundle when the host page already loaded embed.js#56
sidgaikwad wants to merge 1 commit into
unlayer:mainfrom
sidgaikwad:perf/prefetch-on-host-injected

Conversation

@sidgaikwad

Copy link
Copy Markdown
Contributor

Fixes #44.

1. The prefetch was skipped on host-injected pages (the real fix)

The load listener prefetches the versioned bundle so the first createEditor doesn't pay a second round trip — but the early return bypassed it:

if (window.ImageEditor) {
  return Promise.resolve();   // no prefetch
}

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 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:

× prefetches the bundle when the host page already installed the global

2. The reused-tag timeout

REUSED_TAG_TIMEOUT_MS is now exported and loadScript takes it as an optional second argument, so the bound is overridable and directly testable rather than requiring vi.advanceTimersByTime(30_000).

I deliberately stopped short of a component prop. loadScript isn't exported from the package entry point, so surfacing this to consumers means either exporting the module or adding a prop alongside scriptUrl — 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

  • 3 new tests; 49 total; coverage still 100% across the board
  • lint, typecheck, build clean

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.
@vercel

vercel Bot commented Sep 4, 2026

Copy link
Copy Markdown

@sidgaikwad is attempting to deploy a commit to the Unlayer Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

loadScript: prefetch skipped on host-injected pages, and the 30s reused-tag timeout is not configurable

1 participant