乐闻世界logo
搜索文章和话题

How does Nuxt.js static site generation (SSG) work and how is it different from server-side rendering (SSR)?

3月6日 23:11

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:

  1. Build Process

    • Use the nuxt generate command 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
  2. Data Fetching

    • During generation, each page's asyncData and fetch methods are executed
    • Fetched data is injected into the generated HTML files
    • Generated pages contain complete content, no need for client-side data fetching
  3. Generated Output

    • Generated static files are stored in the dist directory
    • Includes HTML files, JavaScript bundles, CSS files, etc.
    • Can be deployed to any static file server
  4. Dynamic Content Handling

    • For pages requiring dynamic content, client-side data fetching can be used
    • Can use nuxtServerInit or other methods to handle dynamic data

Key differences between SSG and SSR:

FeatureStatic Site Generation (SSG)Server-Side Rendering (SSR)
Build TimingGenerates static files at build timeRenders dynamically at request time
Deployment EnvironmentAny static file serverRequires Node.js server
PerformanceFast loading, no server processingRequires server rendering for each request
SEOExcellent, static HTML easy to indexExcellent, server-rendered content easy to index
Use CasesWebsites with infrequent content changesApplications with frequently changing content
CostLow deployment costRequires server running cost

Advantages of SSG:

  1. Excellent Performance

    • Static files load quickly, no server processing required
    • Can leverage CDN for accelerated distribution
    • Short first-screen loading time, good user experience
  2. 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
  3. SEO-Friendly

    • Static HTML files contain complete content, easily indexed by search engines
    • Fast page loading speed, beneficial for search rankings
  4. High Security

    • Static files have no server-side code execution risks
    • Reduced attack surface

Best Practices:

  1. 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
  2. Data Fetching

    • Use asyncData and fetch methods to fetch data during build time
    • For dynamic content, consider using client-side data fetching
  3. Route Handling

    • Ensure all dynamic routes are correctly generated during build time
    • Use generate.routes configuration to handle dynamic routes
  4. 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}`) ] } }

标签:Nuxt.js