Static Assets and Images
⏱ Estimated reading time: 2 min
Next.js provides an efficient way to manage static assets like images, fonts, icons, and videos, along with powerful built-in image optimization for better performance.
1. Static Assets in Next.js
Static assets are stored in the public/ folder.
Files inside this folder can be accessed directly via the browser.
Example Structure
Usage
-
No import required
-
Path starts from
/public -
Best for static files
2. Using the Next.js Image Component
Next.js provides the Image component from next/image for automatic image optimization.
Basic Example
Benefits
-
Automatic image optimization
-
Responsive images
-
Lazy loading by default
-
Improved performance and Core Web Vitals
3. Remote Images
You can also load images from external URLs.
External domains must be allowed in
next.config.js.
4. Image Layout Options
Responsive Image
The parent container must have position: relative.
5. Priority Images
Use priority for above-the-fold images.
6. Static Files Other Than Images
You can serve:
-
PDFs
-
Videos
-
Audio files
-
JSON files
Example:
7. When to Use
</> data-start="1967" data-end="1976">
| Use Case | Recommended | ||
|---|---|---|---|
| Optimized images | next/image | ||
| Simple icons | | ||
| SVG icons | Inline or |