Next.js - optimizations
import Image from "next/image";
import mountains from "@/../public/images/mountains-unsplash.jpg";
export default function Home() {
return (
<main>
<Image src={mountains} alt="image of a beautiful mountain" />
</main>
);
}import Image from "next/image";
export default function Home() {
return (
<main>
<Image src="https://ahscdn.be/sites/default/files/styles/coursefinder_lg_2x/public/2024-03/ahs_headernew_hires.png?itok=BHQfd4rD" alt="image of a beautiful mountain" />
</main>
);
}
// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
images: {
remotePatterns: [
{
protocol: "https",
hostname: "ahscdn.be",
},
],
},
};
export default nextConfig;
Next.js - optimizations
// Example for using Google Fonts with Next.js
import { Geist, Geist_Mono } from "next/font/google";
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
{children}
</body>
</html>
);
} return (
<html lang="en">
<body
className={`${roboto.variable} antialiased`}
>
{children}
</body>
</html>
);To use fonts imported from Google with Tailwind:
/* globals.css */
@import "tailwindcss";
@theme inline {
--font-roboto: var(--font-roboto);
}
export default function Home() {
return <p className="font-roboto">Hello there</p>;
}If you only use a single font, you can also use the className property directly
return (
<html lang="en">
<body
className={`${roboto.className} antialiased`}
>
{children}
</body>
</html>
);Next.js - optimizations
<html>
<head>
<meta name="description" content="Some relevant description of your website, preferably around 150 characters long" />
<title>My super awesome website</title>
</head>
<body>
<p>Super awesome paragraph!</p>
</body>
</html>// Use either one or the other, you can NOT export both
export const metadata: Metadata = {
title: "My awesome app",
description: "Generated by create-next-app"
};
export async function generateMetadata(): Promise<Metadata> {
// ...
}When using fetch inside generateMetadata,
the data is automatically cached
import type { Metadata, ResolvingMetadata } from 'next'
type Props = {
params: Promise<{ id: string }>
searchParams: Promise<{ [key: string]: string | string[] | undefined }>
}
export async function generateMetadata(
{ params, searchParams }: Props,
parent: ResolvingMetadata
): Promise<Metadata> {
// fetch data
const product = await fetch(`https://.../${id}`).then((res) => res.json());
}
export default function Page({ params, searchParams }: Props) {}