Nuxt.js provides powerful static site generation capabilities, allowing developers to pre-render applications into static HTML files, improving performance and SEO.
How Static Site Generation (SSG) works:
-
Build Process
- Use the
nuxt generatecommand to trigger static site generation - Nuxt.js traverses all routes and generates corresponding HTML files for each route
- Also generates necessary JavaScript and CSS files
- Use the
-
Data Fetching
- During generation, each page's
asyncDataandfetchmethods are executed - Fetched data is injected into the generated HTML files
- Generated pages contain complete content, no need for client-side data fetching
- During generation, each page's
-
Generated Output
- Generated static files are stored in the
distdirectory - Includes HTML files, JavaScript bundles, CSS files, etc.
- Can be deployed to any static file server
- Generated static files are stored in the
-
Dynamic Content Handling
- For pages requiring dynamic content, client-side data fetching can be used
- Can use
nuxtServerInitor other methods to handle dynamic data
Key differences between SSG and SSR:
| Feature | Static Site Generation (SSG) | Server-Side Rendering (SSR) |
|---|---|---|
| Build Timing | Generates static files at build time | Renders dynamically at request time |
| Deployment Environment | Any static file server | Requires Node.js server |
| Performance | Fast loading, no server processing | Requires server rendering for each request |
| SEO | Excellent, static HTML easy to index | Excellent, server-rendered content easy to index |
| Use Cases | Websites with infrequent content changes | Applications with frequently changing content |
| Cost | Low deployment cost | Requires server running cost |
Advantages of SSG:
-
Excellent Performance
- Static files load quickly, no server processing required
- Can leverage CDN for accelerated distribution
- Short first-screen loading time, good user experience
-
Simple Deployment
- Can be deployed to any static file server
- Supports Netlify, Vercel, and other static hosting services
- No need to maintain Node.js server
-
SEO-Friendly
- Static HTML files contain complete content, easily indexed by search engines
- Fast page loading speed, beneficial for search rankings
-
High Security
- Static files have no server-side code execution risks
- Reduced attack surface
Best Practices:
-
Content Types
- Suitable for websites with infrequent content changes, such as blogs, corporate websites, documentation sites
- Not suitable for applications with high real-time data requirements, such as real-time chat, dynamic dashboards
-
Data Fetching
- Use
asyncDataandfetchmethods to fetch data during build time - For dynamic content, consider using client-side data fetching
- Use
-
Route Handling
- Ensure all dynamic routes are correctly generated during build time
- Use
generate.routesconfiguration to handle dynamic routes
-
Caching Strategy
- Set static file caching strategy appropriately
- For frequently changing content, consider using client-side data fetching
Notes:
- Static site generation requires fetching all page data during build time
- For large websites, build time may be longer
- Dynamic routes need to be explicitly specified in configuration, otherwise they may not be generated
- Real-time data needs to be obtained through client-side API calls
Example Configuration:
javascript// nuxt.config.js export default { generate: { routes: [ '/users/1', '/users/2', '/posts/1', // Can dynamically generate routes through functions ...Array.from({ length: 10 }, (_, i) => `/products/${i + 1}`) ] } }