1. When HTML is Generated
SSR: HTML is dynamically generated on each request by the server. The server processes the request, fetches data, and renders the page in real-time.
SSG: HTML is pre-rendered at build time. All pages are generated upfront during the build process and stored as static files.
2. Use Case
SSR: Best for dynamic applications with real-time or user-specific content (e.g., dashboards, e-commerce product pages). Ideal when data changes frequently.
SSG: Best for static content that rarely changes (e.g., blogs, landing pages, documentation). Suitable for SEO-heavy sites with pre-defined content.
3. Performance
SSR: Slightly slower initial response due to server processing per request, but ensures fresh content. Requires a Node.js server.
SSG: Faster delivery since pre-built HTML is served directly. Can leverage CDN caching for global scalability.
4. Data Handling
SSR: Fetches data on every request, ensuring up-to-date content (e.g., live stock prices, user sessions).
SSG: Fetches data once at build time. Content becomes stale until the site is rebuilt (requires redeployment for updates).
5. Deployment
SSR: Requires a server or cloud platform that supports Node.js (e.g., Firebase Cloud Functions, AWS EC2).
SSG: Can be hosted on static hosting services (e.g., GitHub Pages, Netlify, Vercel) with no server overhead.
6. Implementation in Angular
Both use Angular Universal but with different workflows:
SSR: Enabled via
server-side-rendering
schematic. Usesapp.server.module.ts
to handle dynamic rendering.SSG: Achieved via prerendering (e.g.,
ng run app:prerender
). Generates static HTML files for specified routes during build.
Example Commands
SSR:
ng add @nguniversal/express-engine
(sets up a Node.js server for on-demand rendering).SSG:
ng run your-app:prerender
(pre-renders static HTML files at build time).
When to Choose?
Choose SSR if your app needs real-time data, user authentication, or frequent content updates.
Choose SSG if your content is static, SEO is critical, and you want low-cost, high-performance hosting.
No comments:
Post a Comment