Next.js course ยท Module 1: Next.js Configuration
Next.js Developer Tools
In this lesson7
In Quantum Metropolis, every engineer has an advanced set of diagnostic tools that allow real-time monitoring of city systems, detecting anomalies, and optimizing infrastructure performance. Similarly, Next.js 16 offers programmers a powerful set of developer tools that significantly speed up work and make finding bugs easier.
Fast Refresh - Instant Updates
Imagine that Quantum Metropolis engineers can modify city systems and see the effects of changes in a fraction of a second, without having to restart the entire infrastructure. That's exactly how Fast Refresh works in Next.js.
Fast Refresh is a hot reload mechanism that automatically updates the application preview in the browser after saving code changes. Key features:
- Preserves component state - if you edit a React component, Fast Refresh updates it without resetting
useStateoruseReducerstate - Works instantly - changes are visible in less than a second
- Handles syntax errors - if you make a mistake, after fixing it the application returns to its normal state
// You edit this component and save the file...
export default function Dashboard() {
const [count, setCount] = useState(0);
return (
<div>
<h1>Quantum Panel: {count}</h1>
<button onClick={() => setCount(count + 1)}>+1</button>
</div>
);
}
// Fast Refresh updates the component, but count keeps its value!When does Fast Refresh reset state? If you edit a file that exports something other than a React component (e.g., a constant, helper function), Fast Refresh will reload the entire module. This is normal behavior.
Error Overlay - Readable Error Messages
In the command center of Quantum Metropolis, every anomaly is immediately displayed on the main screen with an exact description of the problem and its location. Next.js offers an analogous mechanism - Error Overlay.
When an error appears in the code, Next.js displays a full-screen overlay with:
- Error type - e.g.,
TypeError,SyntaxError,ReferenceError - Error message - a readable description of the problem
- Code location - the exact file, line, and column
- Stack trace - the call path leading to the error
// If you write code with an error like this:
export default function Page() {
const user = null;
return <h1>{user.name}</h1>; // TypeError: Cannot read property 'name' of null
}
// Error Overlay will show:
// TypeError: Cannot read properties of null (reading 'name')
// app/page.tsx (3:22)Error Overlay works for both compile-time errors (e.g., TypeScript syntax errors) and runtime errors (e.g., references to undefined variables). Just fix the error and save the file - the overlay will disappear automatically.
Turbopack - Next.js's New Bundler
In Quantum Metropolis, the old energy generator was replaced by TurboCore, a new, much more efficient quantum reactor. Similarly, in Next.js 16 Turbopack became the default bundler, replacing Webpack in both next dev and next build.
Turbopack is a bundler written in Rust, created by the creator of Webpack. Its main advantages:
- 2-5x faster production builds than with Webpack - according to the Next.js team at the Next.js 16 release
- Up to 10x faster Fast Refresh (Hot Module Replacement, HMR)
- Incremental compilation - processes only changed files
In Next.js 16 you do not need to switch anything on - Turbopack works out of the box, and the --webpack flag takes you back to Webpack:
# Development and production builds - Turbopack by default
npx next dev
npx next build
# Switch back to Webpack if your project needs it
npx next dev --webpack
# Scripts in package.json no longer need the --turbopack flag
{
"scripts": {
"dev": "next dev",
"build": "next build"
}
}Since Next.js 16, Turbopack is the default for both development and production builds. In Next.js 15 it was stable only for next dev and had to be switched on with the --turbopack flag. It significantly speeds up page loading time during development, especially in large projects.
React DevTools with Next.js
Every technician in Quantum Metropolis uses an advanced scanner for analyzing city systems. For React programmers, the equivalent is the React DevTools extension.
React DevTools is a browser extension (Chrome, Firefox, Edge) that allows:
- Browsing the component tree - you see the hierarchy of all React components
- Inspecting props and state - you can view values of
props,useState,useContext - Performance profiling - you measure which components take the longest to render
- Highlighting re-renders - you visually see which components are re-rendering
// React DevTools will show the structure:
// <RootLayout>
// <Navbar />
// <MainContent>
// <Dashboard count={5} />
// <Sidebar items={[...]} />
// </MainContent>
// <Footer />
// </RootLayout>In Next.js, React DevTools automatically recognize Server Components and Client Components, displaying appropriate labels.
Debugging Basics in Next.js
Every Quantum Metropolis engineer must know the basic diagnostic tools. Here are key debugging techniques in Next.js:
Console - Diagnostic Terminal
The simplest but very effective tool. In Next.js, console.log works both on the server side (terminal) and client side (browser DevTools):
// Server Component - logs visible in terminal
export default async function Page() {
console.log("This log will appear in the server terminal");
const data = await getData();
console.log("Fetched data:", data);
return <div>{data.title}</div>;
}
// Client Component - logs visible in browser
"use client";
export default function Button() {
console.log("This log will appear in the browser console");
return <button onClick={() => console.log("Clicked!")}>Click</button>;
}Network Tab - Transmission Monitoring
The Network tab in browser DevTools lets you monitor all network requests:
- API requests - you can see URL, status, response time
- Static resources - images, CSS, JavaScript
- Prefetching - Next.js automatically fetches data for links visible on screen
next.config.js Configuration - Overview
The next.config.js (or next.config.mjs, and next.config.ts in projects created with create-next-app) file is the central configuration point for a Next.js project, similar to the main control panel of Quantum Metropolis. Here are the most important options:
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
// Image optimization - allowed external servers (images.domains is deprecated)
images: {
remotePatterns: [
{ protocol: 'https', hostname: 'example.com' },
{ protocol: 'https', hostname: 'cdn.example.com' },
],
},
// URL redirects
async redirects() {
return [
{
source: '/old-path',
destination: '/new-path',
permanent: true,
},
];
},
// Environment variables available in code
env: {
APP_VERSION: '1.0.0',
},
};
module.exports = nextConfig;Most commonly used configuration options:
images- image optimization configurationredirects- URL redirects (e.g., after changing site structure)env- additional environment variablesexperimental- experimental Next.js features
Summary
Next.js developer tools are like the advanced diagnostic system of Quantum Metropolis - they allow you to:
- Fast Refresh - instantly see the effects of code changes without losing state
- Error Overlay - quickly locate and fix errors with readable messages
- Turbopack - work with lightning-fast compilation thanks to the new bundler written in Rust
- React DevTools - analyze component structure, props, and state
- Console and Network tab - debug the application on both server and client side
- next.config.js - centrally configure project behavior
Knowledge of these tools is the foundation of effective work with Next.js. In the following modules, you'll regularly use them while building increasingly advanced applications in Quantum Metropolis.
Code for this lesson: App.tsx
import React, { useState } from 'react';
// Simulation of Next.js developer tools
const DevToolsDemo = () => {
const [activeTab, setActiveTab] = useState('fast-refresh');
const [code, setCode] = useState('<h1>Welcome to Quantum!</h1>');
const [logs, setLogs] = useState<string[]>([]);
const [error, setError] = useState<string | null>(null);
const tabs = [
{ id: 'fast-refresh', label: 'Fast Refresh' },
{ id: 'error-overlay', label: 'Error Overlay' },
{ id: 'console', label: 'Console' },
];
const simulateFastRefresh = () => {
setLogs(prev => [...prev, '[Fast Refresh] Change detected, updating...']);
setTimeout(() => {
setLogs(prev => [...prev, '[Fast Refresh] Component updated (12ms)']);
setLogs(prev => [...prev, '[Fast Refresh] Component state preserved']);
}, 300);
};
const simulateError = () => {
setError('TypeError: Cannot read properties of null (reading "name")\n at DashboardPage (app/page.tsx:5:18)');
setLogs(prev => [...prev, '[Error] Error detected in component']);
};
const fixError = () => {
setError(null);
setLogs(prev => [...prev, '[Fast Refresh] Error fixed, overlay closed']);
};
const addConsoleLog = (type: string) => {
const messages: Record<string, string> = {
server: '[Server] console.log: Data fetched from API (145ms)',
client: '[Browser] console.log: Component mounted',
network: '[Network] GET /api/data -> 200 OK (89ms)',
};
setLogs(prev => [...prev, messages[type] || '[Log] Action executed']);
};
return (
<div style={{ background: '#0f0f23', minHeight: '100vh', padding: '20px', color: '#fff', fontFamily: 'monospace' }}>
<h1 style={{ color: '#64ffda', textAlign: 'center', marginBottom: '20px' }}>
Next.js Dev Tools - Quantum Lab
</h1>
<div style={{ display: 'flex', gap: '8px', justifyContent: 'center', marginBottom: '20px' }}>
{tabs.map(tab => (
<button key={tab.id} onClick={() => { setActiveTab(tab.id); setError(null); }}
style={{ padding: '8px 16px', background: activeTab === tab.id ? '#7c4dff' : 'rgba(255,255,255,0.1)',
border: 'none', borderRadius: '6px', color: '#fff', cursor: 'pointer' }}>
{tab.label}
</button>
))}
</div>
<div style={{ maxWidth: '800px', margin: '0 auto' }}>
{activeTab === 'fast-refresh' && (
<div>
<div style={{ background: '#1a1a2e', padding: '16px', borderRadius: '8px', marginBottom: '12px' }}>
<p style={{ color: '#888', marginBottom: '8px' }}>Code editor (simulation):</p>
<textarea value={code} onChange={(e) => setCode(e.target.value)}
style={{ width: '100%', background: '#0d1117', color: '#64ffda', border: '1px solid #333',
borderRadius: '4px', padding: '12px', fontFamily: 'monospace', fontSize: '14px', minHeight: '60px' }} />
</div>
<button onClick={simulateFastRefresh}
style={{ padding: '10px 24px', background: '#4caf50', border: 'none', borderRadius: '6px',
color: '#fff', cursor: 'pointer', marginBottom: '12px' }}>
Save file (Ctrl+S)
</button>
<div style={{ background: '#000', padding: '12px', borderRadius: '8px', marginTop: '8px', border: '1px solid #333' }}>
<p style={{ color: '#888', marginBottom: '4px' }}>Preview:</p>
<div dangerouslySetInnerHTML={{ __html: code }} style={{ color: '#64ffda' }} />
</div>
</div>
)}
{activeTab === 'error-overlay' && (
<div>
{error ? (
<div style={{ background: 'rgba(244,67,54,0.15)', border: '2px solid #f44336', borderRadius: '8px', padding: '20px' }}>
<h3 style={{ color: '#f44336', marginBottom: '12px' }}>Unhandled Runtime Error</h3>
<pre style={{ color: '#ff8a80', whiteSpace: 'pre-wrap', marginBottom: '16px' }}>{error}</pre>
<button onClick={fixError}
style={{ padding: '8px 20px', background: '#4caf50', border: 'none', borderRadius: '6px',
color: '#fff', cursor: 'pointer' }}>
Fix error
</button>
</div>
) : (
<div style={{ textAlign: 'center' }}>
<p style={{ color: '#4caf50', marginBottom: '16px' }}>No errors</p>
<button onClick={simulateError}
style={{ padding: '10px 24px', background: '#f44336', border: 'none', borderRadius: '6px',
color: '#fff', cursor: 'pointer' }}>
Simulate error
</button>
</div>
)}
</div>
)}
{activeTab === 'console' && (
<div>
<div style={{ display: 'flex', gap: '8px', marginBottom: '12px' }}>
<button onClick={() => addConsoleLog('server')}
style={{ padding: '8px 16px', background: '#1a73e8', border: 'none', borderRadius: '6px', color: '#fff', cursor: 'pointer' }}>
Server Log
</button>
<button onClick={() => addConsoleLog('client')}
style={{ padding: '8px 16px', background: '#ff9800', border: 'none', borderRadius: '6px', color: '#fff', cursor: 'pointer' }}>
Client Log
</button>
<button onClick={() => addConsoleLog('network')}
style={{ padding: '8px 16px', background: '#0f9d58', border: 'none', borderRadius: '6px', color: '#fff', cursor: 'pointer' }}>
Network
</button>
<button onClick={() => setLogs([])}
style={{ padding: '8px 16px', background: 'rgba(255,0,0,0.2)', border: 'none', borderRadius: '6px', color: '#f44336', cursor: 'pointer' }}>
Clear
</button>
</div>
</div>
)}
<div style={{ background: '#000', borderRadius: '8px', padding: '12px', marginTop: '16px', minHeight: '150px', maxHeight: '250px', overflowY: 'auto' }}>
<p style={{ color: '#555', marginBottom: '8px' }}>Logs:</p>
{logs.length === 0 ? (
<p style={{ color: '#333', textAlign: 'center', paddingTop: '40px' }}>No logs...</p>
) : (
logs.map((log, i) => (
<div key={i} style={{ padding: '2px 0', fontSize: '13px',
color: log.includes('Error') ? '#f44336' : log.includes('Server') ? '#1a73e8' : log.includes('Network') ? '#0f9d58' : '#ccc' }}>
{log}
</div>
))
)}
</div>
</div>
</div>
);
};
export default DevToolsDemo;Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What does the Fast Refresh mechanism do in Next.js?
2. What is Turbopack in Next.js 16?