Question presented to candidate:
"Our analytics page lazy-loads a heavy chart with defineAsyncComponent. On flaky mobile networks the chunk sometimes fails and users see nothing. Walk me through the options defineAsyncComponent gives you for loading and error states, how you would add retries, and what changed recently."
What a strong answer should cover:
defineAsyncComponent(loader)returns a wrapper component that calls the loader the first time it renders and caches the resolved component for every later use.- The options object:
loader,loadingComponent,errorComponent,delay(default 200 ms before the loading component shows),timeout(no timeout by default),suspensible,onError(error, retry, fail, attempts)and, since 3.5,hydrate. - Retry through
onError: callretry()to run the loader again, orfail()to give up and showerrorComponent. - Inside a
<Suspense>the component is suspensible by default and its own loading, error, delay and timeout options are ignored;suspensible: falseopts out. - Lazy hydration (3.5):
hydrateOnIdle,hydrateOnVisible,hydrateOnMediaQuery,hydrateOnInteractionfor server-rendered pages. - The loader must resolve to a component or an ES module whose
defaultis one.
Clarifying questions expected:
- "How many retries are acceptable before showing an error, and should the user be able to retry manually?"
- "Is the page server-rendered?"
Code / implementation expected: Yes β a defineAsyncComponent with loading and error components, delay, timeout and an onError retry policy.