keepcalculator

06 Numbers and Powers

Next.js Parallel Routes 404 on Refresh: Fix

Card No. 06·31410 · Updated · Standard formula, shown below

Next.js parallel routes can return a 404 error when you refresh the page because the route slot’s default page is missing or the URL doesn’t match a valid route segment. The fix is to add a default.js file for each parallel route slot and ensure your page.js files handle unmatched routes.

What Are Parallel Routes in Next.js?

Parallel routes in Next.js (introduced in App Router) let you render multiple pages simultaneously within the same layout. They are defined using the @folder convention—for example, @analytics or @team. Each slot is a separate route segment that can have its own page.js, loading.js, and error.js files. This pattern is useful for dashboards, split views, or any UI where independent sections update without a full page reload.

Why Does a 404 Occur on Refresh?

When you navigate client-side (e.g., clicking a link), Next.js uses the URL to match all parallel slots. But on a full page refresh, the server renders the route directly from the URL. If a slot doesn’t have a default.js file, the server doesn’t know what to render for that slot when the URL doesn’t explicitly match a nested page. In that case, Next.js throws a 404 error for the entire route.

For example, consider a layout with @sidebar and @main. If you visit /dashboard and @sidebar has page.js but @main only has a page.js for /dashboard/settings, a refresh on /dashboard will fail because @main has no default content to show.

How to Fix the 404 Error

The primary fix is to add a default.js file to every parallel route slot that might not have a matching page for a given URL. The default.js file acts as a fallback and is used when Next.js cannot find a specific page.js for that slot. Here are the steps:

  1. Identify all parallel route slots in your app directory (folders prefixed with @).
  2. For each slot, create a default.js file that exports a React component. This component can be a simple placeholder, a loading state, or a redirect.
  3. Ensure that your default.js is at the same level as the slot’s page.js (e.g., app/@sidebar/default.js).
  4. If you have nested parallel routes, add default.js at each level where a mismatch can occur.

After adding the missing files, rebuild and restart your development server. The 404 on refresh should be resolved.

Common Mistakes and Misconceptions

One common mistake is thinking that default.js is optional if you have a page.js in the slot. That’s not true—default.js is required for any slot that can be rendered without a specific subpath. Another misconception is that not-found.js in the root app folder will catch these errors; it only handles 404s for unmatched URLs, not missing slot defaults.

Also, don’t confuse default.js with page.js in the same folder. They serve different purposes: page.js defines the UI for the exact path, while default.js defines the fallback UI when no page matches.

Alternative Approaches

If you don’t want to create a separate fallback component, you can use a conditional in your layout to check if a slot has content. For example, in your layout file, you can do:

export default function Layout({ children, sidebar }) {
  return (
    <div>
      {sidebar || <p>No sidebar content</p>}
      {children}
    </div>
  );
}

But this only works for client-side navigation; on refresh, the server still needs a default.js to render the slot. Another approach is to use generateStaticParams if your routes are static, but this is not applicable to dynamic or user-specific content.

How to Debug Parallel Route Issues

If you’re still seeing 404s, check the Next.js server console for errors. It will often log which slot is missing a default. You can also inspect the generated route tree by running next build and looking at the output. Additionally, verify that your app directory structure is correct—slots must be direct children of a layout folder.

Another debugging tip: temporarily add a default.js that returns null to all slots. If the 404 disappears, you know the issue is with missing defaults. Then you can refine each default component.

Frequently Asked Questions

Why does my parallel route work on navigation but fail on refresh?

Client-side navigation uses the in-memory route tree, which includes slot data. On refresh, the server re-renders from the URL, and if a slot doesn’t have a default, it can’t resolve. Adding default.js fixes this.

Can I use not-found.js inside a slot?

Yes, you can add not-found.js to a slot to handle 404s within that slot, but it doesn’t replace the need for default.js. The server looks for default.js first when no page matches.

Do I need default.js for every slot?

Yes, if the slot can be rendered without a specific subpath. If a slot always has a page.js for every possible URL (e.g., a catch-all route), you might not need it, but it’s safer to include it.

What should I put in default.js?

It can be any React component. Common choices are a loading spinner, a message like “Select an item,� or a redirect to a default subpage. You can also return null if the slot should be empty.

Does this affect performance?

No, default.js is only used as a fallback and has negligible performance impact. It’s a standard part of the App Router.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →