RSC vs Dynamic Imports Demo

This page demonstrates the difference between React Server Components (RSC) automatic code-splitting and client-side dynamic imports. Both reduce bundle size, but serve different purposes.

🟢 Server Components (RSC)

  • • Automatic code-splitting by Next.js
  • • Run on server, HTML sent to client
  • • Libraries never reach the browser
  • • Perfect for static content & data fetching
  • • No client-side JavaScript needed
  • • SEO-friendly by default
When to use: Static content, data processing, heavy computations

🔵 Dynamic Imports

  • • Manual code-splitting with next/dynamic
  • • Load components on-demand
  • • Reduce initial bundle size
  • • Perfect for interactive components
  • • Custom loading states
  • • Can disable SSR if needed
When to use: Interactive features, modals, admin panels

🟡 Regular Client Components

  • • Included in main bundle
  • • Run on client with React hooks
  • • All dependencies bundled
  • • Interactive and stateful
  • • Larger initial bundle
  • • Immediate availability
When to use: Core UI, always-visible components

🟢 Server Components (Automatic Code-Splitting)

These components run on the server and are automatically code-split by Next.js. Heavy libraries like Lodash and date-fns are never sent to the client.

// ✅ Server Component (no 'use client')
import _ from 'lodash';
import { format } from 'date-fns';

export default function ServerComponent() {
  // Heavy processing on server
  const data = _.range(100).map(i => ({
    id: i,
    date: format(new Date(), 'yyyy-MM-dd')
  }));
  
  return <div>{/* Rendered HTML */}</div>;
}

Server-Side Data Table

Server Component

Total Revenue

$650,397.63

Total Users

13,841

Avg Conversion

4.89%

WeekRevenueUsersConversionCategory
Jan 01, 2024$6,111.249697.2%SMB
Jan 08, 2024$10,425.246464.19%Enterprise
Jan 15, 2024$18,538.969924.93%Startup
Jan 22, 2024$18,614.363495.23%SMB
Jan 29, 2024$42,446.095574.71%SMB
Feb 05, 2024$20,903.385676.1%SMB
Feb 12, 2024$8,259.155046.44%Startup
Feb 19, 2024$37,139.85492.44%Startup
Feb 26, 2024$29,857.365304.67%Startup
Mar 04, 2024$43,216.65875.2%Enterprise
Mar 11, 2024$41,690.031232.21%SMB
Mar 18, 2024$9,203.955566.27%Enterprise
Mar 25, 2024$48,521.591844.93%Startup
Apr 01, 2024$10,379.181682.12%Startup
Apr 08, 2024$30,400.226034.51%SMB
Apr 15, 2024$5,423.678807.73%Startup
Apr 22, 2024$41,367.578685.52%Startup
Apr 29, 2024$47,366.482783.35%Enterprise
May 06, 2024$9,915.155334.96%Enterprise
May 13, 2024$34,116.313203.69%Startup
May 20, 2024$42,325.697294.18%SMB
May 27, 2024$11,885.134266.52%Enterprise
Jun 03, 2024$25,677.612202.25%Enterprise
Jun 10, 2024$25,002.449955.42%Startup
Jun 17, 2024$31,610.437087.36%Enterprise

📊 Server Component Benefits:

  • • Data processing happens on the server
  • • Lodash bundle not sent to client (Server-only)
  • • Automatic code splitting by Next.js
  • • SEO-friendly, fully rendered on server
  • • Reduced client-side JavaScript

Server-Side Analytics Dashboard

Server Component

Total Revenue

$124,766

Avg Growth

17.8%

Best Month

Jun

Monthly Performance

Jan 2024+9.6% growth

Revenue: $16,355

Customers: 1,775

Enterprise: $9,813

SMB: $4,907

Feb 2024+24.6% growth

Revenue: $22,032

Customers: 1,825

Enterprise: $13,219

SMB: $6,610

Mar 2024+18.4% growth

Revenue: $11,135

Customers: 1,173

Enterprise: $6,681

SMB: $3,341

Apr 2024+9.7% growth

Revenue: $19,984

Customers: 1,190

Enterprise: $11,990

SMB: $5,995

May 2024+24.1% growth

Revenue: $14,621

Customers: 1,756

Enterprise: $8,773

SMB: $4,386

Jun 2024+20.1% growth

Revenue: $40,639

Customers: 1,814

Enterprise: $24,383

SMB: $12,192

🚀 RSC Code Splitting Benefits:

  • • Heavy computations run on server only
  • • Lodash & date-fns never reach the client
  • • Rendered HTML sent to browser (faster)
  • • No JavaScript needed for static content
  • • Perfect for SEO and initial page load
  • • Automatic code splitting without dynamic()

🟢 Conditionals in Server Components

Server Components can evaluate conditionals at render time on the server. This means different code paths and heavy computations only run when the condition is met.

// ✅ Conditional Server Component
export default function ConditionalRSC({ userRole }) {
  if (userRole === 'admin') {
    // Heavy computation only runs for admins
    const adminData = performHeavyProcessing();
    return <AdminDashboard data={adminData} />;
  }
  
  return <UserDashboard />;
}

Conditional Server Component

Server Component

👑 Admin Dashboard

Role: admin | Data points: 100 | Total value: $579,350

📊 Analytics View

Average Value: $5793.5

Max Value: $9999

Min Value: $1036

Data Types: admin_data

⚙️ Advanced Admin Features

• System configuration access

• User management tools

• Advanced analytics and reporting

• Server performance metrics

Server computed: Complex aggregation completed on server

IDTypeValuePermissions
0admin_data$1546read, write, delete
1admin_data$3850read, write, delete
2admin_data$5094read, write, delete
3admin_data$7504read, write, delete
4admin_data$9479read, write, delete

Showing 5 of 100 items

✅ RSC Conditional Benefits:

  • • Conditions evaluated on server at render time
  • • Heavy computations only run when needed
  • • No client-side JavaScript for conditional logic
  • • Different data processing based on conditions
  • • Lodash computations never reach the browser

🔵 Dynamic Imports (Manual Code-Splitting)

These client components are loaded on-demand using next/dynamic. Perfect for interactive features that aren't needed immediately.

// ✅ Dynamic Import (manual code-splitting)
const DynamicComponent = dynamic(
  () => import('./HeavyComponent'),
  {
    loading: () => <LoadingSpinner />,
    ssr: false // Optional: disable SSR
  }
);

🟡 Regular Client Components (Main Bundle)

Small, lightweight components that should remain in the main bundle. These are typically core UI elements used throughout your application.

// ❌ Don't dynamically import lightweight components
'use client';
import React from 'react';

export default function SimpleButton() {
  // Lightweight component with minimal dependencies
  return <button>Click me</button>;
}

Simple Interactive Button

This is a lightweight component that should be in the main bundle

❌ Don't Dynamic Import:

  • • Small, lightweight components
  • • Core UI elements used everywhere
  • • Components without heavy dependencies
  • • Critical user interface elements

Main Bundle Components

These components should stay in the main bundle for optimal performance

Navigation Menu✓ Main Bundle
Header/Footer✓ Main Bundle
Basic Form Inputs✓ Main Bundle
Loading Spinners✓ Main Bundle

🔄 Conditional Dynamic Loading

Conditional Client Component

Client Component

Choose a view to load:

Select a view above to see dynamic loading in action

🔵 Dynamic Import Benefits Here:

  • • Heavy components only load when user selects them
  • • Role-based access controls which components can load
  • • Interactive state management requires client-side code
  • • Each component is a separate chunk, loaded on-demand
  • • Better UX with loading states for heavy components

📊 Bundle Impact Comparison

Component TypeBundle ImpactLoading BehaviorConditionalsBest For
Server ComponentZero client JSRendered HTML, no loading✓ Server-side evaluationStatic content, data display
Dynamic ImportSeparate chunksOn-demand with loading state✓ Runtime conditional loadingInteractive features, modals
Regular ClientMain bundleImmediate availability⚠️ All code loadedCore UI, critical features

🤔 When to Use Each with Conditionals:

Server Components

Use when conditions are known at render time (user role, feature flags, etc.)

Dynamic Imports

Use when conditions change based on user interaction or runtime state

Regular Components

Use for lightweight components that are likely to be used regardless of conditions

🎯 Best Practices & Decision Tree

Use Server Components when:

  • Displaying static data or computed results
  • Heavy data processing or transformations
  • SEO is important (blog posts, product pages)
  • Using large server-only libraries
  • No user interaction needed

Use Dynamic Imports when:

  • Component has user interactions (forms, charts)
  • Feature is used conditionally or in modals
  • Large third-party interactive libraries
  • Admin panels or power-user features
  • A/B testing or feature flags