File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed
Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ }
File renamed without changes.
Original file line number Diff line number Diff 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 >
You can’t perform that action at this time.
0 commit comments