Next.js 13's App Router experiences a 404 error when refreshing due to the parallel route modal configuration

I am attempting to create a modal using parallel routes in Next.js version 13.4.

Everything functions properly when I am on the homepage (/) and open the modal by navigating to /login.

However, if I refresh the /login page, instead of displaying the homepage with the modal, it shows a 404 error page along with the modal:

https://i.sstatic.net/z1BP6.png

This is the structure of my folders:

https://i.sstatic.net/lmsQP.png

This is @overlays/login/page.tsx:

'use client';

import {useRouter} from 'next/navigation';
import {Dialog} from '@mui/material';

export default function Page() {
    const router = useRouter();

    return (
       <Dialog
          open
          onClose={router.back}
          keepMounted
          disablePortal
       >
          Hello
       </Dialog>
    );
}

Is there a way to display the home page (/) when directly accessing the parallel route /login?

Answer №1

If you're facing a 404 error, it's possible that you are missing the file @overlays/default.tsx:

export default function Default() {
  return null;
};

You can find more information about this in the documentation for Parallel Routes > Unmatched Routes:

Unmatched Routes

When no matching route is found, Next.js will render content based on routing techniques and folder structures.

[...]

Reload

In case of an unmatched route, Next.js attempts to render the default.js file in the slot first. If that's not available, a 404 error is displayed.

If your intention is to prevent the modal from being accessed as a standalone page, which is discussed as one of the main use cases in the documentation, consider using router interception:

For example, you may want to display a login modal in a top navbar without a dedicated /login page, or show a shopping cart in a side modal.

To redirect from /login to / when directly accessed, utilize router interception to render a different component handling redirection, like this:

app
  (home)
  login
    page.tsx <= LoginPage
  @overlays
    login
      page.tsx <= LoginModal
    page.tsx

Ensure that login/page.tsx contains the following code snippet:

import { redirect } from 'next/navigation'

export default function LoginPage() {
  redirect('/')

  return null;
};

Answer №2

I encountered a similar issue recently. It turned out that the solution was to have a default.tsx file in the @modals folder (or @overlays folder in your case). I mistakenly placed it in the /app directory instead of /app/@modals.

You can learn more about this issue by visiting: parallel routes - default.js

The content of the default.tsx file should look like this:

export default function Default() {
  return null;
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Press the button to activate the function

<table class="calc" cellpadding=2> <td><input type="button" class="calc" id="screen" value="0" ></td> <td><input type="button" class="calc" id="screen" value="0" ></td> <tr> </ ...

How can tick values be displayed on a c3js line chart when all data is unselected?

I'm currently working with a c3js line chart that displays 3 different lines. However, I noticed that when I remove all data sources, the x-axis tick values disappear. Is there a way to keep the x-axis tick values visible even when there is no data pr ...

Encountering module resolution issue following npm-link for the shared component repository

Attempting npm-link for the first time to establish a shared component repository, and encountering an issue. The project seems to be linked correctly based on this message: /Users/tom.allen/Development/main/project/node_modules/@linked/project -> /usr ...

Guide on sending a JavaScript function to a Pug file using Node.js and Express

In my recent project, I developed a node app using express along with a pug file. The express app is configured to listen on port 3000 and render the pug file. One of the key functionalities involves fetching information from an external API. My goal is to ...

Integrate the dForm plugin with a convenient time picker widget

Currently, I am attempting to integrate a time picker widget into a jQuery plugin called dForm. The specific timepicker widget can be found at the following link: https://github.com/jonthornton/jquery-timepicker Despite reviewing the dForm documentation, ...

Creating packing features specifically designed for resolution within a reusable module

I've decided to revamp my Angular application based on John Papa's style guide (well, mostly) and my main focus is on improving modularity. The stumbling block I've encountered is with route resolves. So far, I've been using a global ap ...

Acquiring the underlying code of websites within the local network

In the tool I am creating, it takes a URL as a parameter and retrieves the source code from that URL for analysis. The tool will be hosted on our domain, with the capability of analyzing web pages of sites within our internal network domain. Here is the Jq ...

Struggling with AngularJs as I attempt to retrieve my Twitter timeline. It's been a challenge

Currently, I am using the code below to fetch tweets for a timeline. This snippet was originally taken from an AngularJS tutorial and worked perfectly with Twitter's search API. angular.module( 'Twitter', [ 'ngResource' ]); func ...

Exploring the Power of JQuery and Iterating with For-

I'm currently facing a small issue with my code. I need to retrieve information about each module when I display the hidden table row. The information is fetched from the page {modules/$moduleid}. While I understand how to utilize AJAX, my challenge l ...

How to place an element in a specific location within the DOM using JavaScript

How can I position a created element in a specific place within the DOM using this code? Currently, it appends at the bottom of the page. var x = document.getElementById('options_10528_1'); var pos = document.getElementById('options-10528- ...

Steps for retrieving the identifier of a duplicated element

I've copied an element and changed their IDs, but I'm having trouble fetching the IDs of the cloned elements in jQuery. Can someone assist me with this issue? The HTML view source code is provided below: <table id="dataTable" borde ...

Having trouble with clearInterval in my Angular code

After all files have finished running, the array this.currentlyRunning is emptied and its length becomes zero. if(numberOfFiles === 0) { clearInterval(this.repeat); } I conducted a test using console.log and found that even though ...

Error encountered while trying to load component: template or render function is not defined

I have set up a basic vue.js configuration using browserify and vueify. Following advice from previous tutorials, I included aliasify as a dependency to utilize the template engine. Below is my package.json file: { "name": "simple-vueify-setup", "ve ...

Using TypeScript's reference function within an HTML document

It feels like ages since my early days of web development. Back when I first started coding, we would reference a script using a <script> tag: <html> <head> <script src="lealet.js"></script> <!-- I know the path isn´t c ...

There was a TypeError encountered while attempting to read the 'useState' properties of null for the swiper and next.js 13 modules

We recently updated to next.js 13 and encountered the following error message: TypeError: Cannot read properties of null (reading 'useState') The error stack trace is displayed below: Warning: Invalid hook call. Hooks can only be called inside ...

How to add 1 to the final element in a JavaScript

I'm currently working on a task that involves incrementing the last element in an array using pop() and push(). However, I'm facing an issue where the original values are being retained after I try to increment the popped array. The objective is ...

Analyzing information through jsonParse

After using the command {{$employees}} in my laravel controller to pass data to the blade view, I am receiving the following information: [{"id":"1","name":"june"},{"id":"2","name":"joan"}] When trying to parse this JSON data in my JavaScript as shown be ...

Ways to utilize a single HTML page for various URLs while changing one variable value based on the queried URL

My current HTML page structure looks like this: <body ng-controller="DashboardDisplay" onload="submit()"> <div class="container-fluid" > {{scope.arr}} </div> </body> <script> var myApp = angular.module(&apos ...

JavaScript functions are not being triggered when the submit button is clicked

I am facing an issue where the form buttons are not triggering functions in my React application. The post request is being made using Axios. Could this be related to the structure of my components? Or perhaps it has something to do with file naming conven ...

The call function in Tween.js fails to execute after adding an EventListener

I encountered an issue while using tween.0.6.2. The following code snippet (borrowed from the tween.js Getting Started page and slightly simplified) works perfectly: createjs.Tween.get(circle) .to({x: 400}, 1000, createjs.Ease.getPowInOut ...