{"componentChunkName":"component---src-components-blog-template-js","path":"/blog/2023-02-08-nextjs/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"Next.js","date":"2023-02-08"},"html":"<p>I have been using Create React App for most of my React projects, but recently I started working with Next.js and it offers quite a lot out of the box. Next.js is a React framework that adds features like server-side rendering, file-based routing, and API routes without needing to set up everything manually.</p>\n<h3>File-Based Routing</h3>\n<p>In Create React App, we need to install a routing library like React Router and define routes manually. In Next.js, routing is based on the file structure inside the <code class=\"language-text\">pages</code> folder. If we create a file called <code class=\"language-text\">about.js</code> inside <code class=\"language-text\">pages</code>, it automatically becomes accessible at <code class=\"language-text\">/about</code>. For dynamic routes, we use square brackets. A file named <code class=\"language-text\">[id].js</code> inside <code class=\"language-text\">pages/users</code> will match <code class=\"language-text\">/users/1</code>, <code class=\"language-text\">/users/2</code>, and so on.</p>\n<p>This makes the routing setup simpler because we do not need to maintain a separate route configuration. The file structure is the route structure.</p>\n<h3>Rendering Methods</h3>\n<p>Next.js supports multiple rendering methods, and we can choose different methods for different pages in the same application.</p>\n<p><strong>Static Site Generation (SSG)</strong> generates the HTML at build time. The page is built once and served as a static file. This is the fastest option because there is no server processing on each request. We use <code class=\"language-text\">getStaticProps</code> to fetch data at build time.</p>\n<p><strong>Server-Side Rendering (SSR)</strong> generates the HTML on every request. This is useful for pages that need fresh data on every visit. We use <code class=\"language-text\">getServerSideProps</code> to fetch data on each request.</p>\n<p><strong>Client-Side Rendering</strong> works like a regular React app where data is fetched in the browser after the page loads. We can still do this in Next.js using <code class=\"language-text\">useEffect</code> or data fetching libraries.</p>\n<h3>API Routes</h3>\n<p>Next.js also lets us create API endpoints inside the <code class=\"language-text\">pages/api</code> folder. A file called <code class=\"language-text\">pages/api/users.js</code> becomes an endpoint at <code class=\"language-text\">/api/users</code>. This is useful for small backends or for handling things like form submissions without setting up a separate server.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">export default function handler(req, res) {\n  if (req.method === \"GET\") {\n    res.status(200).json({ users: [] });\n  }\n}</code></pre></div>\n<h3>When to Choose Next.js</h3>\n<p>If the project needs good SEO, server-side rendering, or a mix of static and dynamic pages, Next.js is a solid choice. For a simple single-page application that does not need SEO or server-side rendering, Create React App or Vite might be simpler to work with. But for most of my new projects, I have been reaching for Next.js because the built-in features save a lot of setup time.</p>"}},"pageContext":{"slug":"/2023-02-08-nextjs/"}},"staticQueryHashes":[]}