
- Next.js - Home
- Next.js - Overview
- Next.js - Project Setup
- Next.js - Folder Structure
- Next.js - App Router
- Next.js - Page Router
- Next.js Features
- Next.js - Pages
- Next.js - Data Fetching
- Next.js - ISR
- Next.js - Static File Serving
- Next.js - Pre-Rendering
- Next.js - Partial Pre Rendering
- Next.js - Server Side Rendering
- Next.js - Client Side Rendering
- Next.js Routing
- Next.js - Routing
- Next.js - Nested Routing
- Next.js - Dynamic Routing
- Next.js - Parallel Routing
- Next.js - Imperative Routing
- Next.js - Shallow Routing
- Next.js - Intercepting Routes
- Next.js - Redirecting Routes
- Next.js - Navigation and Linking
- Next.js Configuration
- Next.js - TypeScript
- Next.js - Environment Variables
- Next.js - File Conventions
- Next.js - ESLint
- Next.js API & Backend
- Next.js - API Routes
- Next.js - Dynamic API Routes
- Next.js - Route Handlers
- Next.js - API MiddleWares
- Next.js - Response Helpers
- Next.js API Reference
- Next.js - CLI Commands
- Next.js - Functions
- Next.js - Directives
- Next.js - Components
- Next.js - Image Component
- Next.js - Font Component
- Next.js - Head Component
- Next.js - Form Component
- Next.js - Link Component
- Next.js - Script Component
- Next.js Styling & SEO
- Next.js - CSS Support
- Next.js - Global CSS Support
- Next.js - Meta Data
- Next.js Advanced Topics
- Next.js - Error Handling
- Next.js - Server Actions
- Next.js - Fast Refresh
- Next.js - Internationalization
- Next.js - Authentication
- Next.js - Session Management
- Next.js - Authorization
- Next.js - Caching
- Next.js - Data Caching
- Next.js - Router Caching
- Next.js - Full Route Caching
- Next.js - Request Memoization
- Next.js Performance Optimization
- Next.js - Optimizations
- Next.js - Image Optimization
- Next.js - Lazy Loading
- Next.js - Font Optimization
- Next.js - Video Optimization
- Next.js - Script Optimization
- Next.js - Memory Optimization
- Next.js - Using OpenTelemetry
- Next.js - Package Bundling Optimization
- Next.js Testing
- Next.js - Testing
- Next.js - Testing with Jest
- Next.js - Testing with Cypress
- Next.js - Testing with Vitest
- Next.js - Testing with Playwright
- Next.js Debugging & Deployment
- Next.js - Debugging
- Next.js - Deployment
- Next.js Useful Resources
- Next.js - Interview Questions
- Next.js - Quick Guide
- Next.js - Useful Resources
- Next.js - Discussion
Next.js - File Conventions
What is Next.js File Convention?
Next.js uses a default filename and folder structure for certain sections like pages, app, layout.js, and middleware.js, which automatically define routes, layouts, middleware, and other configurations. This is called the file convention in Next.js. In this chapter, we will learn about all the default file conventions in Next.js.
File Conventions
Next.js provide simple filename convention to simplify the development process. For example, to handle custom error pages, we can create a file named error.js
inside the pages
directory. The table below shows the all the default file conventions in Next.js.
File Name | Location | Purpose |
---|---|---|
default.js | app/ | Provides fallback content when NextJS cannot determine which page to render. |
error.js | app/ | Custom error page to handle application-wide errors. |
instrumentation.js | app/ | Used for tracking and monitoring app activity. |
layout.js | app/ | Defines the common layout structure (header, footer, etc.) for pages. |
loading.js | app/ | Used to define loading state while the page or data is being fetched. |
middleware.js | app/ | Custom middleware to handle requests and responses, often for authentication. |
not-found.js | app/ | Custom 404 page to handle not found errors. |
page.js | pages/ | Represents individual pages in the application, automatically routed. |
route.js | app/ | Defines custom server-side routing logic. |
template.js | app/ | Defines reusable templates for specific types of content or pages. |
The default.js File
The default.js
file is used to define the default content for a page when Next.js cannot determine which page to render. It happens when a user refreshes the page or navigates to a URL that does not match any of the defined routes. This file is located in the app
directory.
// File: app/default.js const DefaultLayout = ({ children }) => ( <div> This is the default layout {children} </div> ); export default DefaultLayout;
The error.js File
The error.js
file is used to define the custom error page for the application. It is located in the app
directory. This file is used to handle application-wide errors, such as 404 errors or 500 errors.
Know more about error handling in Next.js here.
// File: app/404.js export default function Custom404() { return ( <div> <h1>404 - Page Not Found</h1> <p>Sorry, the page you are looking for does not exist.</p> </div> ); }
The instrumentation.js File
The instrumentation.js
file is used to define the custom instrumentation for the application. It is located in the app
directory. This file is used to track and monitor app activity.
// File: app/instrumentation.js export default function instrumentation() { return { name: 'My Application', version: '1.0.0', url: 'https://my-application.com', }; }
The layout.js File
The layout.js
file is used to define the common layout structure for pages. It is located in the app
directory. This file is used to define the common layout structure for pages.
// File: app/layout.js import { Metadata } from 'next'; const metadata: Metadata = { title: 'My Application', description: 'My Application description', }; export default function RootLayout({ children, }: { children: React.ReactNode; }) { return ( <html lang="en"> <head> <meta name="description" content={metadata.description} /> </head> <body> {children} </body> </html> ); }
The loading.js File
The loading.js
file is used to define the loading state for pages. It is located in the app
directory. This file is used to setup custom animation or logo while the page is loading.
// File: app/loading.js export default function Loading() { return ( <div> <h1>Loading...</h1> </div> ); }