2019-02-07 07:41:10 -05:00
# App shell
2021-04-09 01:33:21 -04:00
Application shell is a way to render a portion of your application using a route at build time.
2019-02-07 07:41:10 -05:00
It can improve the user experience by quickly launching a static rendered page (a skeleton common to all pages) while the browser downloads the full client version and switches to it automatically after the code loads.
2021-04-09 01:33:21 -04:00
This gives users a meaningful first paint of your application that appears quickly because the browser can render the HTML and CSS without the need to initialize any JavaScript.
2019-02-07 07:41:10 -05:00
Learn more in [The App Shell Model ](https://developers.google.com/web/fundamentals/architecture/app-shell ).
## Step 1: Prepare the application
You can do this with the following CLI command:
2019-07-20 16:02:36 -04:00
< code-example language = "bash" >
2019-02-07 07:41:10 -05:00
ng new my-app --routing
< / code-example >
For an existing application, you have to manually add the `RouterModule` and defining a `<router-outlet>` within your application.
## Step 2: Create the app shell
2021-04-09 01:33:21 -04:00
Use the CLI to automatically create the application shell.
2019-02-07 07:41:10 -05:00
2019-07-20 16:02:36 -04:00
< code-example language = "bash" >
2019-10-08 13:31:44 -04:00
ng generate app-shell
2019-02-07 07:41:10 -05:00
< / code-example >
2019-08-23 02:05:25 -04:00
* `client-project` takes the name of your client application.
2019-02-07 07:41:10 -05:00
After running this command you will notice that the `angular.json` configuration file has been updated to add two new targets, with a few other changes.
2019-07-20 16:02:36 -04:00
< code-example language = "json" >
2019-02-07 07:41:10 -05:00
"server": {
"builder": "@angular-devkit/build-angular:server",
2021-03-12 02:14:30 -05:00
"defaultConfiguration": "production",
2019-02-07 07:41:10 -05:00
"options": {
2020-12-19 12:39:05 -05:00
"outputPath": "dist/my-app/server",
2019-02-07 07:41:10 -05:00
"main": "src/main.server.ts",
"tsConfig": "tsconfig.server.json"
2020-12-19 12:39:05 -05:00
},
"configurations": {
2021-03-12 02:14:30 -05:00
"development": {
"outputHashing": "none",
},
2020-12-19 12:39:05 -05:00
"production": {
"outputHashing": "media",
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"sourceMap": false,
"optimization": true
}
2019-02-07 07:41:10 -05:00
}
},
"app-shell": {
"builder": "@angular-devkit/build-angular:app-shell",
2021-03-12 02:14:30 -05:00
"defaultConfiguration": "production",
2019-02-07 07:41:10 -05:00
"options": {
"route": "shell"
2019-09-04 09:52:57 -04:00
},
"configurations": {
2021-03-12 02:14:30 -05:00
"development": {
"browserTarget": "my-app:build:development",
"serverTarget": "my-app:server:development",
},
2019-09-04 09:52:57 -04:00
"production": {
"browserTarget": "my-app:build:production",
"serverTarget": "my-app:server:production"
}
2019-02-07 07:41:10 -05:00
}
}
< / code-example >
## Step 3: Verify the app is built with the shell content
Use the CLI to build the `app-shell` target.
2019-07-20 16:02:36 -04:00
< code-example language = "bash" >
2021-03-12 02:14:30 -05:00
ng run my-app:app-shell:development
2019-02-07 07:41:10 -05:00
< / code-example >
2019-09-04 09:52:57 -04:00
Or to use the production configuration.
< code-example language = "bash" >
ng run my-app:app-shell:production
< / code-example >
2021-04-09 01:33:21 -04:00
To verify the build output, open `dist/my-app/browser/index.html` . Look for default text `app-shell works!` to show that the application shell route was rendered as part of the output.