Skip to content

Commit 8964c83

Browse files
committed
End of part 7
1 parent ce88da1 commit 8964c83

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/app/(SSR)/dynamic/page.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { UnsplashImage } from "@/models/unsplash-image";
2+
import Image from "next/image";
3+
import Link from "next/link";
4+
import { Alert } from "@/components/bootstrap";
5+
6+
export const metadata = {
7+
title: "Dynamic Fetching - NextJS 13.4 Image Gallery",
8+
}
9+
10+
export const revalidate = 0;
11+
12+
export default async function Page() {
13+
const response = await fetch("https://api.unsplash.com/photos/random?client_id=" + process.env.UNSPLASH_ACCESS_KEY,
14+
{
15+
// cache: "no-cache"/"no-store"
16+
// next: { revalidate: 0 }
17+
}
18+
);
19+
const image: UnsplashImage = await response.json();
20+
21+
const width = Math.min(500, image.width);
22+
const height = (width / image.width) * image.height;
23+
24+
return (
25+
<div className="d-flex flex-column align-items-center">
26+
<Alert>
27+
This page <strong>fetches data dynamically</strong>.
28+
Every time you refresh the page, you get a new image from the Unsplash API.
29+
</Alert>
30+
31+
<Image
32+
src={image.urls.raw}
33+
width={width}
34+
height={height}
35+
alt={image.description}
36+
className="rounded shadow mw-100 h-100"
37+
/>
38+
by <Link href={"/users/" + image.user.username}>{image.user.username}</Link>
39+
</div>
40+
);
41+
}

src/app/NavBar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default function NavBar() {
1717
<Navbar.Collapse id="main-navbar">
1818
<Nav>
1919
<Nav.Link as={Link} href="/static" active={pathname === "/static"}>Static</Nav.Link>
20+
<Nav.Link as={Link} href="/dynamic" active={pathname === "/dynamic"}>Dynamic</Nav.Link>
2021
</Nav>
2122
</Navbar.Collapse>
2223
</Container>

0 commit comments

Comments
 (0)