Skip to lesson content

PROJECT 1 · LESSON 2 OF 18

Add pages and shared navigation

Build About and Contact routes without duplicating the navigation.

  • file-based routing
  • Link
  • layout children

Your workspace: edushop

Continue from lesson 1: Create your first Next.js website. Keep existing files; create or replace the complete files listed below.

Extract the ZIP, open its project folder, and run npm install. Database lessons also need the .env.local setup described below. Checkpoints never contain credentials or node_modules.

1. Understand the problem and the concepts

Why we need this feature

Our site needs separate pages without three copies of the same header. This lesson makes the relationship between a URL, a folder and a layout explicit. The aim is to predict what Next.js will render before opening the browser.

A folder defines a URL segment; its page.tsx supplies the screen. A layout wraps all routes below it. The header belongs in the layout because every page needs it.

Link enables Next.js navigation. Ordinary anchors still work, but are useful for downloads and external sites rather than internal page transitions.

Read a URL as a folder path

For /about, Next.js looks for the about route segment under app and its page.tsx. For /contact, it selects app/contact/page.tsx. The app folder itself is not part of the public URL. A normal helper file inside a folder does not create a page; the page convention does.

You can extend this rule: app/company/team/page.tsx corresponds to /company/team. Creating the company folder does not require creating a /company page. The parent can organize nested routes without displaying its own screen.

What happens when a learner clicks Link

Link describes an internal destination and participates in Next.js navigation. On a client-side transition, Next.js updates the route content while preserving shared layouts. The browser address changes and Back/Forward remain useful. Directly visiting /about must also work; navigation is not limited to clicks from Home.

In production Next.js may prefetch eligible links, so a request can appear before you click. A full address-bar navigation and a Link transition can use different loading paths while reaching the same page. Do not infer the entire rendering model from one network entry.

Shared layout does not mean shared page content

About and Contact do not import the header. The root layout already wraps both. If you add a footer to the layout, both pages get it. If you add a paragraph inside About, only About changes. This gives you a simple test for placement: ask whether the content belongs to every route or just one.

The exported metadata object describes the page title and other document information. It does not render a visible heading. Your h1 and browser-tab title are separate outputs, and both should explain the current page. The contact link uses mailto because its destination is an email action, not a Next.js route.

2. Follow the execution flow

Read this sequence before the source. Identify the input, the component or function that handles it, and the result the learner sees.

  1. The user clicks the About Link.

  2. Next.js resolves /about to app/about/page.tsx.

  3. The root layout is shared with the previous route.

  4. About content appears in the children position and the page metadata updates.

Add a route by reasoning from its URL

  1. Requirement: visitors need /delivery. Translate that into app/delivery/page.tsx.
  2. Export a component returning a Delivery information heading. Do not add html or body.
  3. Add one Link with href="/delivery" to the shared navigation.
  4. Verify both clicking the link and pasting /delivery into a fresh tab. These checks catch different kinds of mistakes.

3. Build it, one file at a time

  1. Continue from lesson 1; keep its files. Add the following two pages.
  2. Click About, then Contact. Open /about directly in a new browser tab.
  3. Notice that the layout supplies the navigation to both pages.

The paths below are relative to your practice project. Each block is the complete file at this checkpoint. Read its responsibility first, type or copy the code, then use the walkthrough to explain the important statements.

app/about/page.tsx

This is the page selected by the /about URL.

app/about/page.tsx
export const metadata = { title: "About EduShop" };
export default function About() {
  return (
    <>
      <h1>About EduShop</h1>
      <p>We sell practical learning supplies.</p>
    </>
  );
}

Read the code

  1. The folder determines the route; the function’s name is for readability. The default export supplies the route’s React component.
  2. The fragment groups the heading and paragraph without adding an extra DOM wrapper. The root layout already supplies the document structure.
  3. metadata sets the browser title, while h1 sets the visible heading. They are separate outputs.

app/contact/page.tsx

This gives /contact its own page content while reusing the shared layout.

app/contact/page.tsx
export const metadata = { title: "Contact EduShop" };
export default function Contact() {
  return (
    <>
      <h1>Contact us</h1>
      <a href="mailto:hello@example.com">hello@example.com</a>
      <p>This is a practice address; replace it before publishing.</p>
    </>
  );
}

Read the code

  1. The anchor uses mailto because it asks the browser to handle an email destination. It is not an internal application route.
  2. The example address is deliberately a placeholder. Replace it with an intended public contact before using this practice site as a real business website.

4. Run it and check your result

  • /about and /contact both load directly.
  • Both pages have the same navigation and distinct browser titles.

5. Predict, change and observe

Create app/company/team/page.tsx and visit /company/team. Then try /company.

Compare your prediction with the expected result

What you should observe

The nested page can work while /company returns a not-found screen.

Why it happens

A folder establishes a segment; a page.tsx establishes a page at that segment. Nested pages do not automatically create parent pages.

6. Troubleshoot with evidence

A common mistake

A component named About does not create a route by itself. Its location must be app/about/page.tsx.

Clicking About opens a not-found screen.

Likely cause: The destination and folder path differ, or the page file is missing.

Check and correct: Check that the link is /about and the file is app/about/page.tsx, with a default component export.

The header appears twice.

Likely cause: The page repeats navigation already supplied by the layout.

Check and correct: Keep shared navigation in layout.tsx and leave only the route content in the page.

7. Practice and explain it back

Your independent task

Add /delivery with delivery information, then add its link to the root layout.

Attempt these questions before opening the explanations. Use the source above to justify your answer.

Why use Link for internal navigation?

It integrates with Next.js client navigation and prefetching. It also produces a real navigable link rather than a click handler that merely changes visible state.

Would a file called app/about/card.tsx create /about/card?

No. It is a normal module. That route would need app/about/card/page.tsx.