call(back)
FrontendmediumSign in to star problems

Load CSS on Demand

Write loadCSS(href) in load-css.js; the page wiring is in demo.js.

Requirements

  • Add a <link rel="stylesheet"> for href, and return a promise.
  • Resolve with the link element once the stylesheet has loaded.
  • Reject with new Error("Failed to load " + href) when it fails.
  • Calling it again with the same href must not add a second link.

The demo loads stylesheets from blob URLs, because the preview has no network. A revoked one stands in for the missing file.

Hints

Hint 1

link.onload and link.onerror settle the promise. Append the link after attaching the handlers.

Hint 2

Keep a Map from href to its promise, so a second call returns the first promise — and delete the entry on failure so a retry can happen.

Asked at