Overview
The Odontologia Frontend project uses Angular Universal with @angular/ssr to provide server-side rendering capabilities. SSR improves:- SEO: Search engines can crawl pre-rendered content
- Performance: Faster initial page load
- Social Sharing: Better preview cards on social media
SSR Architecture
The project uses Angular’s modern SSR implementation with Express:Server Configuration
The SSR server is configured insrc/server.ts:
Key Components
- AngularNodeAppEngine: Handles Angular application rendering
- Express: Web server framework
- Static File Serving: Serves pre-built browser assets
Running the SSR Server
Build the Application
First, create a production build:dist/ directory.
Start the Server
Run the SSR server:PORT environment variable):
Server Implementation Details
Static File Serving
The server serves static assets with optimized caching:- maxAge: Files cached for 1 year (safe with content hashing)
- index: No automatic index.html serving
- redirect: No trailing slash redirects
Angular Request Handling
All non-static requests are rendered by Angular:Server Entry Point
The server starts when:- Run as the main module
- Run via PM2 process manager
Build Configuration
SSR is configured inangular.json:
Entry Points
- browser:
src/main.ts- Client-side bootstrap - server:
src/main.server.ts- Server-side bootstrap - ssr.entry:
src/server.ts- Express server
Environment Variables
PORT
Specify the server port:4000
Adding API Endpoints
You can add REST API endpoints to the Express server:SSR Dependencies
The project includes these SSR-specific packages:- @angular/ssr: ^21.1.4 - Angular SSR support
- @angular/platform-server: ^21.1.0 - Server platform
- express: ^5.1.0 - Web server
- @types/express: ^5.0.1 - TypeScript definitions
Production Deployment
Using Node.js
-
Build the application:
-
Deploy the
dist/directory andpackage.json -
Install production dependencies:
-
Start the server:
Using PM2
PM2 is automatically detected and supported:Using Docker
Example Dockerfile:SSR Best Practices
- Avoid Browser APIs: Use platform checks for browser-only code
- Use TransferState: Share data between server and client
- Optimize Images: Use proper image optimization
- Cache Static Assets: Leverage HTTP caching headers
- Monitor Performance: Track TTFB and rendering time
Debugging SSR
Server Logs
The server logs all requests to the console. Monitor these logs to debug issues.Development Mode
For debugging, build in development mode:Next Steps
- Review Build Configuration
- Learn about Testing
- Check Contributing Guidelines