ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
NJ
foundations · 3 min read

next js app router vs pages

=====================================================

=====================================================

As an APIary platform founder, you're likely familiar with the pain points of managing complex routes and layouts in your Next.js application. In this article, we'll delve into the differences between Next.js App Router and Pages Router, exploring their features, use cases, and migration paths.

What are Next.js App Router and Pages Router?

Next.js introduced two new routing systems: App Router and Pages Router. Both aim to improve the way you manage routes in your application, but they differ significantly in their approach.

App Router

App Router is a server-side rendering (SSR) solution that uses React Server Components to render pages on the server. This approach allows for improved performance, reduced latency, and better SEO.

// app/router.js
import type { AppRouter } from 'next/app-router';
import { Link, useLoaderData } from '@remix-run/react';

export default function Home() {
  const data = useLoaderData();

  return (
    <div>
      <h1>App Router</h1>
      <ul>
        {data.map((item) => (
          <li key={item.id}>
            <Link to={`/items/${item.id}`}>{item.name}</Link>
          </li>
        ))}
      </ul>
    </div>
  );
}

Pages Router

Pages Router, on the other hand, uses a more traditional approach, with each page being a separate JavaScript file. This method is easier to understand and set up but may lead to performance issues and slower page loads.

// pages/index.js
import Head from 'next/head';

export default function Home() {
  return (
    <div>
      <Head>
        <title>Pages Router</title>
      </Head>
      <h1>Pages Router</h1>
    </div>
  );
}

Key Differences

Server Components

App Router uses React Server Components to render pages on the server, whereas Pages Router relies on client-side rendering. This difference has a significant impact on performance and SEO.

// app/router.js (server components)
import type { AppRouter } from 'next/app-router';

export default function Home() {
  // server-side rendering
}

Layouts

App Router introduces a new concept called layouts, which allow you to reuse common UI components across your application. Pages Router does not support layouts out of the box.

// app/layouts/_app.js (layout component)
import type { AppRouter } from 'next/app-router';

export default function RootLayout({ children }) {
  return (
    <html>
      <head />
      <body>{children}</body>
    </html>
  );
}

Streaming

App Router supports streaming, which enables you to deliver content incrementally as it becomes available. This feature is particularly useful for large media files.

// app/router.js (streaming)
import type { AppRouter } from 'next/app-router';

export default function Home() {
  const stream = new ReadableStream({
    async pull(controller) {
      // simulate a streaming data source
      await new Promise((resolve) => setTimeout(resolve, 1000));
      controller.enqueue('Hello, ');
      controller.enqueue('World!');
      controller.close();
    },
  });

  return (
    <div>
      <h1>Streaming</h1>
      <p>{stream}</p>
    </div>
  );
}

Migration Path

If you're already using Pages Router, don't worry – Next.js provides a migration path to App Router. To migrate your application, follow these steps:

  1. Update your next.config.js file to use App Router.
  2. Replace your page components with server components.
  3. Use layouts to reuse common UI components.

Conclusion

Next.js App Router and Pages Router offer different approaches to managing routes in your Next.js application. While Pages Router is easier to understand and set up, App Router provides improved performance, reduced latency, and better SEO. By understanding the key differences between these two routing systems, you can make an informed decision about which one best suits your needs.

Related

Sources

Frequently asked
What is next js app router vs pages about?
=====================================================
What are Next.js App Router and Pages Router?
Next.js introduced two new routing systems: App Router and Pages Router. Both aim to improve the way you manage routes in your application, but they differ significantly in their approach.
What should you know about app Router?
App Router is a server-side rendering (SSR) solution that uses React Server Components to render pages on the server. This approach allows for improved performance, reduced latency, and better SEO.
What should you know about pages Router?
Pages Router, on the other hand, uses a more traditional approach, with each page being a separate JavaScript file. This method is easier to understand and set up but may lead to performance issues and slower page loads.
What should you know about server Components?
App Router uses React Server Components to render pages on the server, whereas Pages Router relies on client-side rendering. This difference has a significant impact on performance and SEO.
References & sources
  1. Apiary Reading RoomOpen, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room