# The default.js Convention

> The critical fallback for Parallel Routes on hard reload.

Subject: Next.js | Lesson 13 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/default-file
Previous: [The not-found.js Convention](https://codewithanimation.in/nextjs/not-found-file.md) | Next: [Dynamic Routes ([slug])](https://codewithanimation.in/nextjs/dynamic-routes.md)

## The parallel fallback

The `default.js` file is highly specialized: it is used *exclusively* in conjunction with Parallel Routes (slots named with an `@` prefix, like `@modal` or `@analytics`).

It serves as a fallback UI for a parallel slot when Next.js doesn't know what else to render for that specific slot based on the current URL.

## The hard reload problem

When a user navigates around your app using Next.js Links (client-side navigation), unmatched parallel slots simply remember and keep rendering whatever they were showing previously.

But if the user hits **Refresh** (a hard reload), the server must rebuild the page from scratch. The server has no memory of previous client state. If the current URL doesn't match any specific folder inside a parallel slot, Next.js panics and throws a 404 error.

## Returning null

To fix this, you drop a `default.js` file into your slot folder (e.g., `app/@modal/default.js`).

When the server encounters an unmatched slot during a hard reload, it will render the `default.js` instead of throwing a 404. In many cases—especially for things like modals—your `default.js` will simply `return null;` to render nothing.

## Common mistake: The "works on my machine" bug

This is one of the most frustrating bugs for Next.js beginners.

You build a beautiful parallel routing setup. You click around your app, and everything works flawlessly. Then, you deploy it, send the link to a friend, and they immediately get a 404 page. Why? Because they did a hard load, and you forgot the `default.js` fallback.

## Tip: A mirror of page.js

As a general rule when building parallel routes, if you have a `page.js` at the root of a slot, you almost always want a `default.js` right next to it doing the exact same thing (or returning null).

Next.js will use `page.js` on the initial load of that route, and `default.js` on hard reloads of unmatched sub-routes.

## Try it: The Hard Reload Trap

Study the diagram. Notice how client-side navigation (clicking a Link) is perfectly safe, but hitting the Refresh button forces the server to rebuild. Without a default.js, the server panics and throws a 404.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/default-file

## Check yourself

### Question 1

Under what specific condition does Next.js look for a default.js file in a parallel route?

- A. When a Server Component throws an error
- B. When a user navigates client-side via a <Link>
- C. When the page is hard-reloaded and the URL doesn't match a route in the slot
- D. When a user types a completely invalid URL for the whole site

Correct answer: C

On a hard reload, the server loses the client-side state of parallel slots. If the URL doesn't match anything inside the slot, it needs `default.js` as a fallback to avoid throwing a 404.

- Why not A: That triggers error.js.
- Why not B: Client-side navigation preserves the active state of unmatched slots; it doesn't need default.js.
- Why not D: That triggers not-found.js.

### Question 2

If you are using a parallel route (@modal) to show a popup window, what should @modal/default.js usually return?

- A. A generic 404 message
- B. null
- C. The same component as the homepage
- D. A redirect function

Correct answer: B

By returning `null`, you tell Next.js: 'If you don't know what modal to show on this hard reload, just don't show a modal at all.' This is the correct behavior 99% of the time for modal slots.

- Why not A: You don't want a 404 message where your modal should be.
- Why not C: That would render your homepage twice.
- Why not D: Redirecting is disruptive; you usually just want the slot to be empty.

## Remember this

- `default.js` is only used with Parallel Routes (slots).
- It prevents 404 errors on hard reloads (page refreshes).
- It acts as a fallback when the current URL doesn't match a route inside the slot.
- For modals, `default.js` often just returns `null`.
