#Intercepting Routes for sharable modal
1 messages · Page 1 of 1 (latest)
To add a parallel route for a modal in Next.js, you can use the concept of Intercepting Routes and Parallel Routes. Here's a step-by-step guide on how you can achieve this:
- Create a main route: Start by creating a main route that renders your main page. For example, if you have a photo gallery, you might have a route like
/photothat renders your main photo page.
// app/photo/page.tsx
import { Photo } from '@/app/ui/photo'
export default function Page() {
return <Photo />
}
- Create a slot for the modal: Inside your application, add a slot (for example,
@modal) and adefault.jsfile that returnsnull. This ensures that the modal is not rendered when it's not active.
// app/@modal/default.tsx
export default function Default() {
return null
}
- Intercept the main route: Inside your
@modalslot, intercept the main route by creating a folder that matches the main route. Import the<Modal>component and its children into the intercepted route'spage.tsxfile:
// app/@modal/(.)photo/page.tsx
import { Modal } from '@/app/ui/modal'
import { Photo } from '@/app/ui/photo'
export default function Page() {
return (
<Modal>
<Photo />
</Modal>
)
}
In the above example, (.)photo is used to intercept the photo segment from within the same level. You can use (..) to match segments one level above, (..)(..) to match segments two levels above, and (...) to match segments from the root app directory.
Please note that the convention used to intercept the route, e.g. (.), depends on your file-system structure. You can refer to the Intercepting Routes convention (https://nextjs.org/docs/app/building-your-application/routing/intercepting-routes#convention) for more details.
Sources:
- Intercepting Routes (https://nextjs.org/docs/app/building-your-application/routing/intercepting-routes)
- Parallel Routes (https://nextjs.org/docs/app/building-your-application/routing/parallel-routes#modals)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@fiery reef kapa.ai is still learning and improving, please let us know how it did by reacting below
@swift skiff What will happen if the link is https://example.com/resize/example/photo1.jpg ?
Let me check the knowledge base for you. One moment! :books:
If you have a URL like <https://example.com/resize/example/photo1.jpg,> it will depend on how your Next.js application and its routing system are configured.
If you have a page or API route in your Next.js application that matches this URL structure, the corresponding page or API route will be served. For example, if you have a file named photo1.jpg.tsx or photo1.jpg.js inside the pages/resize/example directory in your project, that file will be used to generate the page.
However, if you're using dynamic routes, you might have a file like [photo].jpg.tsx or [photo].jpg.js inside the pages/resize/example directory. In this case, photo1.jpg would be passed as a query parameter to your page or API route.
If you're using the next/image component to handle images in your Next.js application, and you're using a loader like the Gumlet or Thumbor loaders, the URL might be passed to one of these loaders to generate a new URL for the image. The new URL would include parameters for things like the image format, width, and quality.
Here are examples of how the Gumlet and Thumbor loaders might handle this URL:
Gumlet Loader
export default function gumletLoader({ src, width, quality }) {
const url = new URL(`<https://example.com${src}`>)
url.searchParams.set('format', 'auto')
url.searchParams.set('w', width.toString())
url.searchParams.set('q', (quality || 75).toString())
return url.href
}
Thumbor Loader
export default function thumborLoader({ src, width, quality }) {
const params = [`${width}x0`, `filters:quality(${quality || 75})`]
return `<https://example.com${params.join('/'>)}${src}`
}
In these examples, src would be /resize/example/photo1.jpg, and width and quality would be whatever values you're passing to the loader.
Please note that these are just examples, and the actual behavior will depend on your specific Next.js configuration and code.
Sources:
- Next.js Image Component and Image Optimization (https://nextjs.org/docs/basic-features/image-optimization)
- Next.js Dynamic Routes (https://nextjs.org/docs/routing/dynamic-routes)
- Gumlet Loader Example (https://nextjs.org/docs/pages/api-reference/next-config-js/images#gumlet)
- Thumbor Loader Example (https://nextjs.org/docs/pages/api-reference/next-config-js/images#thumbor)