Veröffentlicht · wird verbessert
Angular-Anleitung · 6/6
Dieses Kapitel ist vorerst nur auf Englisch verfügbar.
An Angular app is compiled ahead of time into static JavaScript, CSS and HTML. You can host that output on any static file server or CDN, or add server-side rendering for faster first loads and better SEO. This chapter covers production builds, static hosting, SSR and prerendering.
ng build uses the production configuration by default. It compiles templates ahead of time, removes unused code, minifies the output and adds content hashes to file names for long-term caching:
ng build
ng build --configuration development
ng build --base-href /my-app/The output goes to dist/my-app/browser/. Use --base-href when the app is served from a sub-path instead of the domain root.
angular.json can set size budgets so a build warns or fails when bundles grow too large. New projects come with sensible defaults that you can tighten:
"budgets": [
{ "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" },
{ "type": "anyComponentStyle", "maximumWarning": "4kB", "maximumError": "8kB" }
]Lazy-loaded routes with loadComponent and @defer blocks are the most effective ways to keep the initial bundle small.
Copy the contents of dist/my-app/browser/ to your web server or hosting service. Because the router handles URLs in the browser, the server must return index.html for any path that is not a real file; otherwise reloading a deep link such as /heroes/42 returns 404. With nginx:
server {
listen 80;
root /usr/share/nginx/html;
location / {
try_files $uri $uri/ /index.html;
}
}A multi-stage Docker image builds the app with Node.js and serves it with nginx:
FROM node:lts-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npx ng build
FROM nginx:alpine
COPY --from=build /app/dist/my-app/browser /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.confng deploy can publish directly once a deployment package is added, for example ng add @angular/fire for Firebase Hosting or the community angular-cli-ghpages package for GitHub Pages.
Enable SSR when creating a project with ng new my-app --ssr, or add it later:
ng add @angular/ssrThis adds a Node.js server entry (server.ts, built on Express), server configuration files and client hydration, so the browser reuses the server-rendered DOM instead of rebuilding it. In you choose how each route is rendered:
app.routes.server.tsimport { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{ path: '', renderMode: RenderMode.Prerender },
{ path: 'heroes/:id', renderMode: RenderMode.Server },
{ path: '**', renderMode: RenderMode.Client },
];| Render mode | When HTML is produced |
|---|---|
Prerender | At build time, as static HTML files (SSG) |
Server | On every request, by the Node.js server |
Client | In the browser, like a regular single-page app |
Prerendering a route with parameters also requires a getPrerenderParams function that returns the values to build. After ng build, start the server bundle:
ng build
node dist/my-app/server/server.mjsThe ng add @angular/ssr schematic also adds an npm script for this, named like serve:ssr:my-app. Code that runs on the server has no window or document, so guard browser-only work with afterNextRender() or by checking the platform. If every route is prerendered, setting the build's outputMode to static produces files that can go on any static host.
ng build and the unit tests in CI so broken builds never reach production.index.html uncached.ng update, which migrates code automatically between major versions.ng build produces an optimized, hashed build in dist/<project>/browser/.index.html for client-side routes.ng add @angular/ssr adds server rendering, hydration and per-route render modes.ng deploy publishes through providers that offer a deployment package.
0 Kommentare
Anmelden · Melde dich an, um einen Kommentar zu schreiben.
Schreib den ersten Kommentar.