javascript
37 lines · 6 steps
An intercepted photo modal route in Next.js
An async server component fetches a photo by id, sets its page metadata, and renders it inside a modal.
Explained by
highlit
1import { notFound } from 'next/navigation'
2import { getPhotoById } from '@/lib/photos'
3import Modal from '@/components/modal'
4import Image from 'next/image'
5
6export async function generateMetadata({ params }) {
7 const { id } = await params
8 const photo = await getPhotoById(id)
9 if (!photo) return {}
10 return { title: photo.title }
11}
12
13export default async function PhotoModal({ params }) {
14 const { id } = await params
15 const photo = await getPhotoById(id)
16
17 if (!photo) notFound()
18
19 return (
20 <Modal>
21 <figure className="photo-modal">
22 <Image
23 src={photo.url}
24 alt={photo.title}
25 width={photo.width}
26 height={photo.height}
27 sizes="(max-width: 768px) 100vw, 768px"
28 priority
29 />
30 <figcaption>
31 <h2>{photo.title}</h2>
32 <p>by {photo.author}</p>
33 </figcaption>
34 </figure>
35 </Modal>
36 )
37}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Async server components can await params and fetch data directly before rendering.
- 2generateMetadata and the page component share the same data-fetching logic to stay consistent.
- 3Calling notFound() short-circuits rendering into the framework's 404 handling for missing records.
Related explainers
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
Intermediate
8 steps
javascript
const SWIPE_THRESHOLD = 80; const MAX_TRANSLATE = 120; export function attachSwipeToDismiss(element, onDismiss) {
Building a swipe-to-dismiss gesture in JS
touch-events
gesture-detection
dom-manipulation
Intermediate
10 steps
javascript
const { pool } = require('./db'); function withTransaction() { return async (req, res, next) => {
A per-request transaction middleware in Express
middleware
database-transactions
connection-pooling
Advanced
7 steps
javascript
function collapseConsecutiveLogs(lines, { keyFn = (l) => l.message } = {}) { const groups = []; for (const line of lines) {
Collapsing consecutive log lines in JavaScript
grouping
run-length-encoding
data-transformation
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/an-intercepted-photo-modal-route-in-next-js-explained-javascript-473c/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.