Skip to content
tezvyn:
Menu

How do you configure a route for lazy loading?

Source: angular.devMediumHow cards are made

How do you configure a route for lazy loading?

Tests route-level code splitting and initial bundle control. Outline: dynamic imports in route config, on-demand chunk fetching, and measurable load-time wins versus eager loading. Red flag: treating lazy loading as just images or components, not route code.

What's really being asked

This question tests whether you can design for performance at the architecture level rather than just writing components. Interviewers want to see that you understand how modern bundlers split code at route boundaries and how router configuration triggers those splits. They also care if you can explain the user impact in concrete terms like time-to-interactive and initial transfer size.

The full answer

A good answer hits four things in order. First, it states that lazy loaded routes are configured by using dynamic imports inside the route definition instead of statically importing the component or module at the top of the router file. Second, it explains that this causes the build tool to emit a separate JavaScript chunk for that route which is only fetched when the user first navigates to the path. Third, it quantifies the benefit by contrasting an eager loaded SPA where every page ships in the initial bundle with a lazy loaded one where the user downloads perhaps fifty to one hundred kilobytes for the shell and then pulls additional chunks on demand. Fourth, it notes that this is critical for large applications because initial bundle size directly correlates with parse and compile time on mobile devices, so route splitting is often the single biggest performance win.

The mistakes people make

A common wrong answer focuses on deferring offscreen images or using the loading attribute on img tags, which is a different optimization. Another red flag is saying lazy loading is automatic or handled by the framework without mentioning dynamic imports or route configuration. Some candidates also claim it reduces server load, but the real win is reducing initial download and main thread work on the client.

What usually comes next

An interviewer might ask how you would handle preloading critical routes or how to avoid a waterfall when nested routes are lazy loaded. They could also ask about error boundaries if a dynamic import fails, or how to measure the before and after with Lighthouse or Webpack Bundle Analyzer.

A concrete example

Imagine an e-commerce admin dashboard with ten distinct sections such as orders, inventory, and analytics. Without lazy loading, the user downloads one megabyte of JavaScript on first visit even if they only check orders that day. With route lazy loading, the initial load might drop to one hundred fifty kilobytes for the layout and shared code, and each admin section loads an additional sixty to one hundred kilobytes only when visited. This keeps the app booting in under one second on mid-tier mobile instead of four or five seconds.

Interview question

Which approach correctly configures route-level lazy loading to reduce the initial JavaScript bundle?

  • a.Define routes using dynamic imports so the build tool emits separate chunks loaded on demand.Correct
  • b.Import all page components statically at the top of the router file and rely on the bundler to split them automatically.
  • c.Add loading='lazy' to offscreen images and components within each route view.
  • d.Offload route rendering to the server to decrease initial client download size.
Why?

Dynamic imports inside route definitions cause the bundler to emit separate JavaScript chunks that are only fetched when the user first navigates to the path, shrinking the initial bundle. The most tempting distractor assumes static imports can be automatically split, but route-level lazy loading requires explicit dynamic imports in the route config.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles