Next.js 13+ introduced the App Router, which uses a file-based routing system. This system includes several special files that serve specific purposes within your application. Let's explore each of these files and their roles.
1. layout.tsx
Purpose: Defines a layout that wraps page or child segment.
2. page.tsx
Purpose: Represents the unique UI of a route and makes the route publicly accessible.
3. loading.tsx
Purpose: Creates a loading UI for a specific part of the app.
4. error.tsx
Purpose: Creates error UI for a specific part of the app.
5. not-found.tsx
Purpose: Creates custom UI for 'not found' errors.
6. template.tsx
Purpose: Similar to layout, but creates a new instance on navigation.
7. default.tsx
Purpose: Fallback page for parallel routes.
8. route.ts
Purpose: Creates API endpoints.
9. middleware.ts
Purpose: Defines middleware to be run before requests are completed.
10. globals.css
Purpose: Adds global styles to the application.
11. favicon.ico
Purpose: The favicon for your application.
(This is an image file, typically 16x16 or 32x32 pixels)
12. sitemap.ts
Purpose: Generates a sitemap for your application.
13. robots.ts
Purpose: Generates a robots.txt file for your application.
14. metadata.ts
(or layout.tsx
with metadata export)
Purpose: Defines metadata for the application or a specific route.
These special files in Next.js 13+ with the App Router provide powerful tools for structuring your application, handling routing, managing layouts, and implementing various features like error handling and loading states.