Skip to content

Commit 20dc1d9

Browse files
Next: Image Optimisation
1 parent ac3e41e commit 20dc1d9

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

Projects/event-mgmt-nextjs/components/event-detail/event-logistics.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
import AddressIcon from '../icons/address-icon';
2-
import DateIcon from '../icons/date-icon';
3-
import LogisticsItem from './logistics-item';
4-
import classes from './event-logistics.module.css';
1+
import AddressIcon from "../icons/address-icon";
2+
import DateIcon from "../icons/date-icon";
3+
import LogisticsItem from "./logistics-item";
4+
import classes from "./event-logistics.module.css";
5+
import Image from "next/image";
6+
7+
// IMAGE OPTIMISATION:
8+
// Next.js has built-in Image optimisation
9+
// It automatically generates suitable image on request
10+
// And caches the image on server for future use
11+
// Next.js re-fetches the image on window resize
12+
// It needs height and width property
13+
// It'll fetch the image based on the height and width passed to it
14+
// But the image will be displayed based on the CSS
515

616
function EventLogistics(props) {
717
const { date, address, image, imageAlt } = props;
818

9-
const humanReadableDate = new Date(date).toLocaleDateString('en-US', {
10-
day: 'numeric',
11-
month: 'long',
12-
year: 'numeric',
19+
const humanReadableDate = new Date(date).toLocaleDateString("en-US", {
20+
day: "numeric",
21+
month: "long",
22+
year: "numeric",
1323
});
14-
const addressText = address.replace(', ', '\n');
24+
const addressText = address.replace(", ", "\n");
1525

1626
return (
1727
<section className={classes.logistics}>
1828
<div className={classes.image}>
19-
<img src={`/${image}`} alt={imageAlt} />
29+
<Image src={`/${image}`} alt={imageAlt} width={400} height={400} />
30+
{/* <img src={`/${image}`} alt={imageAlt} /> */}
2031
</div>
2132
<ul className={classes.list}>
2233
<LogisticsItem icon={DateIcon}>

Projects/event-mgmt-nextjs/components/event/event-item.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import DateIcon from "../icons/date-icon";
33
import AddressIcon from "../icons/address-icon";
44
import ArrowIcon from "../icons/arrow-right-icon";
55
import classes from "./event-item.module.css";
6+
import Image from "next/image";
67

78
const EventItem = ({ item }) => {
89
const { id, title, description, location, date, image, isFeatured } = item;
@@ -18,7 +19,7 @@ const EventItem = ({ item }) => {
1819

1920
return (
2021
<li className={classes.item}>
21-
<img src={"/" + image} alt={title} />
22+
<Image src={"/" + image} alt={title} width={200} height={200} />
2223

2324
<div className={classes.content}>
2425
<div className={classes.summary}>

0 commit comments

Comments
 (0)