Next.js 13 Notes

File Based Routing System

Every file inside the `pages` directory becomes a route automatically. This eliminates the need for a separate routing configuration, leading to a more intuitive project structure.

If you create a file named `about.js` inside the `pages` directory, it will be accessible at `yoursite.com/about`. Nested folders create nested routes. This automatic routing system is one of Next.js's hallmark features, streamlining development.

Special File Names - can be used in subdirectories

  • error.js: An error file defines an error UI boundary for a route segment. It is useful for catching unexpected errors that occur in Server Components and Client Components and displaying a fallback UI. Error
  • layout.js: A layout is UI that is shared between routes. Layout
  • loading.js: The special file loading.js helps you create meaningful Loading UI with React Suspense  Loading
  • page.js: A page is UI that is unique to a route. Page

Server Side Rendering (SSR)

This feature enables a page to be pre-rendered on the server for each request. Useful for SEO and initial page loading performance.


                    

For all post records


                    

Static Site Generation (SSG)

Pages can be pre-rendered at build time and served to users without needing to render them on every request. The pre-rendered pages can then be cached by a CDN for performance.


                    

Incremental Static Regeneration (ISR)

This allows you to use static generation on a per-page basis, but to also re-generate these pages when data changes. By using the `revalidate` option, you can specify how often (in seconds) a page should be regenerated.


                    

API Routes

Next.js offers a feature to create server-side APIs quickly. By creating files inside the `api` directory, you can setup endpoints that run server-side code. This is especially useful for performing operations that shouldn't be done client-side or for creating mock endpoints during development.

Built-in CSS and Sass Support

Next.js provides an integrated CSS-in-JS solution, but it also supports global CSS and modular styles via .module.css files. If you want to use Sass, it’s as simple as installing the Sass package and importing .scss files.

Optimized Image Component

The built-in `Image` component in Next.js automates the optimization of images in your application. It supports lazy loading, responsive sizing, and multiple formats, which can lead to significant performance improvements in your web applications.