Skip to content

Instantly share code, notes, and snippets.

@gragland
Last active October 1, 2023 15:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gragland/7f8614e5f5121a08b0df05947215adf5 to your computer and use it in GitHub Desktop.
Save gragland/7f8614e5f5121a08b0df05947215adf5 to your computer and use it in GitHub Desktop.

A fix for Next.js modals (intercepted routes) not closing when navigating away with <Link> or router.push()

app
├─ @photoModal
│  ├─ (.)photo
|  |  └─ [id]
│  │  |  └─ page.tsx <----- a modal with links to home, /profile, and /login
│  └─ [...catchAll]
│  │  └─ page.tsx <----- returning null here is supposed to cause modal to hide when
│  │  │                  navigating away, as this should match all other routes, doesn't work though.
│  │  │                  So we create dummy pages to match the specific routes below.
│  └─ profile             
│  │  └─ page.tsx <----- page that returns null, causing modal to hide when navigating to /profile
│  └─ login
│  │  └─ page.tsx <----- page that returns null, causing modal to hide when navigating to /login
|  └─ page.tsx <----- page that returns null, causing modal to hide when navigating to /
|  └─ default.tsx
├─ photo
|  └─ [id] 
|  |  └─ page.tsx 
├─ profile
│  └─ page.tsx
|  login
|  └─ page.tsx
└─ page.tsx
└─ layout.tsx

Dummy page looks like this:

export default function Page() {
  return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment