Skip to content

Commit ab82853

Browse files
committed
End of part 9
1 parent 8e64ba7 commit ab82853

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.image {
2+
object-fit: cover;
3+
margin: 0.25rem;
4+
border-radius: 4px;
5+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { UnsplashImage } from "@/models/unsplash-image";
2+
import Image from "next/image";
3+
import styles from "./TopicPage.module.css";
4+
import { Alert } from "@/components/bootstrap";
5+
import { Metadata } from "next";
6+
7+
// export const revalidate = 0;
8+
9+
// export const dynamicParams = false;
10+
11+
interface PageProps {
12+
params: { topic: string },
13+
// searchParams: { [key: string]: string | string[] | undefined },
14+
}
15+
16+
export function generateMetadata({ params: { topic } }: PageProps): Metadata {
17+
return {
18+
title: topic + " - NextJS 13.4 Image Gallery",
19+
}
20+
}
21+
22+
export function generateStaticParams() {
23+
return ["health", "fitness", "coding"].map(topic => ({ topic }));
24+
}
25+
26+
export default async function Page({ params: { topic } }: PageProps) {
27+
const response = await fetch(`https://api.unsplash.com/photos/random?query=${topic}&count=30&client_id=${process.env.UNSPLASH_ACCESS_KEY}`);
28+
const images: UnsplashImage[] = await response.json();
29+
30+
return (
31+
<div>
32+
<Alert>
33+
This page uses <strong>generateStaticParams</strong> to render and cache static pages at build time, even though the URL has a dynamic parameter.
34+
Pages that are not included in generateStaticParams will be fetched & rendered on first access and then <strong>cached for subsequent requests</strong> (this can be disabled).
35+
</Alert>
36+
37+
<h1>{topic}</h1>
38+
{
39+
images.map(image => (
40+
<Image
41+
src={image.urls.raw}
42+
width={250}
43+
height={250}
44+
alt={image.description}
45+
key={image.urls.raw}
46+
className={styles.image}
47+
/>
48+
))
49+
}
50+
</div>
51+
);
52+
}

src/app/NavBar.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import Link from "next/link";
4-
import { Navbar, Nav, Container } from "react-bootstrap";
4+
import { Navbar, Nav, Container, NavDropdown } from "react-bootstrap";
55
import { usePathname } from "next/navigation";
66

77
export default function NavBar() {
@@ -19,6 +19,11 @@ export default function NavBar() {
1919
<Nav.Link as={Link} href="/static" active={pathname === "/static"}>Static</Nav.Link>
2020
<Nav.Link as={Link} href="/dynamic" active={pathname === "/dynamic"}>Dynamic</Nav.Link>
2121
<Nav.Link as={Link} href="/isr" active={pathname === "/isr"}>ISR</Nav.Link>
22+
<NavDropdown title="Topics" id="topics-dropdown">
23+
<NavDropdown.Item as={Link} href="/topics/health">Health</NavDropdown.Item>
24+
<NavDropdown.Item as={Link} href="/topics/fitness">Fitness</NavDropdown.Item>
25+
<NavDropdown.Item as={Link} href="/topics/coding">Coding</NavDropdown.Item>
26+
</NavDropdown>
2227
</Nav>
2328
</Navbar.Collapse>
2429
</Container>

0 commit comments

Comments
 (0)