In Next.js 15 handling assets (images, fonts, icons) and metadata (SEO, Open Graph, structured data) is crucial for a well-optimized project. Here’s how to do both effectively:
Next.js provides several ways to handle assets, including the public directory and static imports.
public
Directory (Recommended)Store static assets like images, fonts, and favicons in the public/
folder. You can reference them directly in your components:
export default function Profile() {return <img src="/images/profile.png" alt="Profile Image" className="w-32 h-32" />;}
next/image
(Best for Optimization)Next.js provides an automatic image optimization feature using the next/image
component.
npm install next
import Image from "next/image";export default function Profile() {return (<Imagesrc="/images/profile.png"alt="Profile Image"width={128}height={128}className="rounded-full"/>);}
Why use next/image
?
For simple SVG icons, you can use inline SVG inside your JSX:
export default function Icon() {return (<svg className="w-6 h-6 text-blue-500" fill="none" stroke="currentColor" strokeWidth={2}><path d="M5 13l4 4L19 7"></path></svg>);}
@svgr/webpack
)For large SVG files, you can install SVGR to import them as React components:
npm install @svgr/webpack
Then, update your next.config.js
:
module.exports = {webpack(config) {config.module.rules.push({test: /\.svg$/,use: ["@svgr/webpack"],});return config;},};
Now, you can import SVGs as components:
import Logo from "../public/logo.svg";export default function Header() {return <Logo className="w-24 h-24 text-red-500" />;}
Metadata is essential for SEO and social sharing. Next.js 15 provides built-in support for metadata via the metadata
object.
layout.js
(for App Router)If using the App Router (app/layout.js
), define metadata like this:
export const metadata = {title: "My Awesome Website",description: "The best site for web development tips.",keywords: "Next.js, Tailwind CSS, Web Development",openGraph: {title: "My Awesome Website",description: "The best site for web development tips.",images: "/images/og-image.jpg",},twitter: {card: "summary_large_image",site: "@mywebsite",title: "My Awesome Website",description: "The best site for web development tips.",images: "/images/twitter-card.jpg",},};
This automatically sets:
<title>
)<meta name="description">
)page.js
)For dynamic pages, use the generateMetadata function:
export async function generateMetadata({ params }) {return {title: `Article: ${params.slug}`,description: `Read about ${params.slug} in detail.`,};}
_app.js
+ next/head
)If using the Pages Router (pages/_app.js
), manually set metadata using next/head
:
import Head from "next/head";export default function Home() {return (<><Head><title>My Website</title><meta name="description" content="The best site for web development tips." /><meta property="og:image" content="/images/og-image.jpg" /></Head><h1>Welcome to My Website</h1></>);}
Add your favicon.ico
inside the public/
folder and reference it in layout.js
:
export const metadata = {title: "My Website",icons: {icon: "/favicon.ico",shortcut: "/favicon.ico",apple: "/apple-touch-icon.png",},};
With these steps, you can:
✅ Optimize asset handling (images, fonts, SVGs)
✅ Improve SEO with metadata (titles, descriptions, Open Graph, Twitter Cards)
✅ Enhance user experience with fast-loading assets
Now, your Next.js 15 + Tailwind CSS 4 project is well-optimized! 🚀
Let me know if you need further refinements.
Quick Links
Legal Stuff
Social Media