forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add eager to lazy route migration
- Loading branch information
1 parent
d0ebdde
commit 330ff84
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
adev/src/content/reference/migrations/route-lazy-loading.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Migration from eager to lazy routes | ||
|
||
This schematic helps developers to convert eagerly loaded component routes to lazy loaded routes. This allows to split the production bundle into smaller chunks, to avoid big JS bundle that includes all routes, which negatively affects initial page load of an application. | ||
|
||
Run the schematic using the following command: | ||
|
||
<docs-code language="shell"> | ||
|
||
ng generate @angular/core:route-lazy-loading | ||
|
||
</docs-code> | ||
|
||
## `path` config option | ||
|
||
By default, migration will go over the entire application. If you want to apply this migration to a subset of the files, you can pass the path argument as shown below: | ||
|
||
<docs-code language="shell"> | ||
|
||
ng generate @angular/core:route-lazy-loading --path src/app/sub-component | ||
|
||
</docs-code> | ||
|
||
The value of the path parameter is a relative path within the project. | ||
|
||
### How does it work? | ||
|
||
The schematic will attempt to find all the places where the application routes as defined: | ||
|
||
- `RouterModule.forRoot` and `RouterModule.forChild` | ||
- `Router.resetConfig` | ||
- `provideRouter` | ||
- `provideRoutes` | ||
- variables of type `Routes` or `Route[]` (e.g. `const routes: Routes = [{...}]`) | ||
|
||
The migration will check all the components in the routes, check if they are standalone and eagerly loaded, and if so, it will convert them to lazy loaded routes. | ||
|
||
#### Before | ||
|
||
<docs-code language="typescript"> | ||
// app.module.ts | ||
import { HomeComponent } from './home/home.component'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
RouterModule.forRoot([ | ||
{ | ||
path: 'home', | ||
// HomeComponent is standalone and eagerly loaded | ||
component: HomeComponent, | ||
}, | ||
]), | ||
], | ||
}) | ||
export class AppModule {} | ||
</docs-code> | ||
|
||
#### After | ||
|
||
<docs-code language="typescript"> | ||
// app.module.ts | ||
@NgModule({ | ||
imports: [ | ||
RouterModule.forRoot([ | ||
{ | ||
path: 'home', | ||
// ↓ HomeComponent is now lazy loaded | ||
loadComponent: () => import('./home/home.component').then(m => m.HomeComponent), | ||
}, | ||
]), | ||
], | ||
}) | ||
export class AppModule {} | ||
</docs-code> | ||
|
||
This migration will also collect information about all the components declared in NgModules and output the list of routes that use them (including corresponding location of the file). Consider making those components standalone and run this migration again. You can use an existing migration (see https://angular.dev/reference/migrations/standalone) to convert those components to standalone. |