Bird
Raised Fist0
Angularframework~20 mins

Preloading strategies in Angular - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Angular Preloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Angular's PreloadAllModules strategy behave?
Consider an Angular app using the PreloadAllModules strategy. What happens after the app's initial load?
ALazy-loaded modules load only if the user clicks a preload button.
BOnly the first lazy-loaded module is loaded after the app starts; others load on demand.
CNo lazy-loaded modules load until the user navigates to their routes.
DAll lazy-loaded modules start loading immediately after the app finishes initial rendering.
Attempts:
2 left
💡 Hint
Think about what 'preload all' means for modules that are lazy-loaded.
state_output
intermediate
2:00remaining
What is the effect of NoPreloading strategy on lazy-loaded modules?
If an Angular app uses the NoPreloading strategy, when do lazy-loaded modules load?
AAll lazy-loaded modules load immediately after app startup.
BLazy-loaded modules load only when the user navigates to their routes.
CLazy-loaded modules never load, causing navigation errors.
DLazy-loaded modules load randomly at intervals.
Attempts:
2 left
💡 Hint
Consider the default behavior without preloading.
📝 Syntax
advanced
2:00remaining
Identify the correct way to enable PreloadAllModules in Angular Router
Which code snippet correctly sets the router to use PreloadAllModules?
ARouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
BRouterModule.forRoot(routes, { preloadingStrategy: 'PreloadAllModules' })
CRouterModule.forRoot(routes, { preloadStrategy: PreloadAllModules })
DRouterModule.forRoot(routes, { preloadingStrategy: new PreloadAllModules() })
Attempts:
2 left
💡 Hint
Check the exact property name and value type for preloadingStrategy.
🔧 Debug
advanced
2:30remaining
Why does a custom preloading strategy fail to preload modules?
A developer wrote a custom preloading strategy but finds no modules preload. What is a common mistake causing this?
Angular
import { Observable, of } from 'rxjs';
import { PreloadingStrategy, Route } from '@angular/router';

export class CustomPreloadingStrategy implements PreloadingStrategy {
  preload(route: Route, load: () => Observable<any>): Observable<any> {
    if (route.data && route.data['preload']) {
      return load();
    } else {
      return of(null);
    }
  }
}

// In routing module:
RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloadingStrategy })
AThe preload method must return a Promise, not an Observable.
BThe CustomPreloadingStrategy class must extend PreloadAllModules to work.
CThe routes lack the 'data: { preload: true }' property to trigger preloading.
DThe preloadingStrategy option must be a string, not a class.
Attempts:
2 left
💡 Hint
Check what the preload method checks in the route data.
🧠 Conceptual
expert
3:00remaining
Why choose a custom preloading strategy over built-in ones?
Which reason best explains why an Angular app might use a custom preloading strategy instead of NoPreloading or PreloadAllModules?
ATo selectively preload only certain modules based on app logic or user roles.
BBecause PreloadAllModules causes syntax errors in Angular 17+.
CTo disable lazy loading entirely and load all modules eagerly.
DBecause NoPreloading preloads all modules immediately, causing delays.
Attempts:
2 left
💡 Hint
Think about control and flexibility in loading modules.

Practice

(1/5)
1. What is the main purpose of preloading strategies in Angular?
easy
A. To load lazy modules in the background to improve navigation speed
B. To prevent any modules from loading until explicitly requested
C. To compile all modules at build time for faster startup
D. To automatically update Angular to the latest version

Solution

  1. Step 1: Understand lazy loading in Angular

    Lazy loading delays loading modules until needed, which can slow navigation initially.
  2. Step 2: Role of preloading strategies

    Preloading strategies load lazy modules in the background after app startup to speed up future navigation.
  3. Final Answer:

    To load lazy modules in the background to improve navigation speed -> Option A
  4. Quick Check:

    Preloading = background loading for faster navigation [OK]
Hint: Preloading means loading modules quietly before needed [OK]
Common Mistakes:
  • Confusing preloading with eager loading
  • Thinking preloading disables lazy loading
  • Assuming preloading updates Angular versions
2. Which of the following is the correct way to enable preloading of all lazy modules in Angular routing?
easy
A. RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
B. RouterModule.forRoot(routes, { preload: true })
C. RouterModule.forRoot(routes, { lazyLoad: true })
D. RouterModule.forRoot(routes, { preloadingStrategy: NoPreloading })

Solution

  1. Step 1: Identify Angular's preloading strategy option

    Angular uses the preloadingStrategy property in router config to set preloading behavior.
  2. Step 2: Recognize the correct strategy for all modules

    PreloadAllModules is the built-in strategy to preload all lazy modules after app loads.
  3. Final Answer:

    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules }) -> Option A
  4. Quick Check:

    PreloadAllModules = preload all lazy modules [OK]
Hint: Use preloadingStrategy: PreloadAllModules to preload all [OK]
Common Mistakes:
  • Using 'preload: true' which is invalid
  • Confusing NoPreloading with preloading all
  • Using lazyLoad option which doesn't exist
3. Given this Angular routing setup:
const routes = [
  { path: 'home', loadChildren: () => import('./home/home.module').then(m => m.HomeModule) },
  { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
  exports: [RouterModule]
})
export class AppRoutingModule {}

What happens after the app loads?
medium
A. Only 'home' module loads eagerly, 'admin' loads on demand
B. 'admin' module preloads, 'home' module does not
C. Neither module preloads; both load on demand
D. Both 'home' and 'admin' modules preload in the background

Solution

  1. Step 1: Analyze the preloading strategy used

    The routing uses PreloadAllModules, which preloads all lazy modules after app startup.
  2. Step 2: Identify lazy loaded modules

    Both 'home' and 'admin' modules are lazy loaded via loadChildren.
  3. Final Answer:

    Both 'home' and 'admin' modules preload in the background -> Option D
  4. Quick Check:

    PreloadAllModules preloads all lazy modules [OK]
Hint: PreloadAllModules preloads every lazy module automatically [OK]
Common Mistakes:
  • Assuming only first lazy module preloads
  • Thinking modules load only on demand with PreloadAllModules
  • Confusing eager loading with preloading
4. Identify the error in this Angular routing configuration:
const routes = [
  { path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
  exports: [RouterModule]
})
export class AppRoutingModule {}
medium
A. Missing 'pathMatch' property in route
B. Using string syntax for loadChildren is deprecated; should use dynamic import
C. Preloading strategy must be NoPreloading when using lazy loading
D. RouterModule.forRoot should not have a second argument

Solution

  1. Step 1: Check loadChildren syntax

    The string syntax ('./dashboard/dashboard.module#DashboardModule') is deprecated in Angular; dynamic import is required.
  2. Step 2: Confirm preloading strategy and other options

    Preloading strategy can be used with lazy loading; no error there. 'pathMatch' is optional here.
  3. Final Answer:

    Using string syntax for loadChildren is deprecated; should use dynamic import -> Option B
  4. Quick Check:

    Use dynamic import() for loadChildren [OK]
Hint: Use dynamic import() syntax for loadChildren [OK]
Common Mistakes:
  • Using old string syntax for loadChildren
  • Thinking preloading strategy conflicts with lazy loading
  • Assuming pathMatch is mandatory for all routes
5. You want to preload only the 'admin' module but not the 'user' module in your Angular app. Which approach correctly implements this custom preloading strategy?
hard
A. Set preloadingStrategy to NoPreloading and preload 'admin' module eagerly
B. Use PreloadAllModules and then lazy load 'user' module manually
C. Create a class implementing PreloadingStrategy that preloads only routes with data.preload = true
D. Mark 'admin' module as eager loaded and 'user' as lazy loaded without preloading

Solution

  1. Step 1: Understand custom preloading strategy

    Angular allows creating a class implementing PreloadingStrategy to control which modules preload.
  2. Step 2: Use route data property to select modules

    By checking data.preload flag in routes, the strategy can preload only selected modules like 'admin'.
  3. Final Answer:

    Create a class implementing PreloadingStrategy that preloads only routes with data.preload = true -> Option C
  4. Quick Check:

    Custom strategy uses data.preload flag [OK]
Hint: Use data.preload flag with custom PreloadingStrategy [OK]
Common Mistakes:
  • Using PreloadAllModules when selective preload is needed
  • Trying to preload modules eagerly without strategy
  • Confusing eager loading with preloading