Discover how a simple folder can save you hours of debugging broken image links!
Why Public directory for static files in NextJS? - Purpose & Use Cases
Imagine you have images, icons, or fonts that your website needs to show. You try to put them in your project folders and write code to load each file manually with complex paths.
Manually managing static files is confusing and error-prone. Paths can break easily, files might not load, and you waste time fixing broken links instead of building features.
Next.js provides a public directory where you can simply drop static files. These files are served automatically at the root URL, making access easy and reliable.
const logo = require('../assets/images/logo.png'); <img src={logo} alt="Logo" />
<img src="/logo.png" alt="Logo" />
This lets you focus on building your app without worrying about file paths or server setup for static assets.
When building a company website, you can put your logo, favicon, and background images in the public folder and reference them easily in your pages.
Managing static files manually is tricky and breaks easily.
The public directory in Next.js serves files automatically at the root URL.
This simplifies file access and speeds up development.
Practice
public directory in a Next.js project?Solution
Step 1: Understand the role of the public directory
Thepublicfolder is designed to hold static files such as images, icons, and documents.Step 2: Recognize how files in public are accessed
Files insidepublicare accessible directly via URL paths starting with/.Final Answer:
To store static files like images and icons accessible via URL paths -> Option CQuick Check:
public folder = static files access [OK]
- Confusing public with components folder
- Thinking public holds API routes
- Assuming public stores config files
logo.png stored inside the public folder in a Next.js component?Solution
Step 1: Understand URL referencing for public files
Files in thepublicfolder are referenced by their path starting with/, without including the folder name.Step 2: Apply correct src attribute
To referencelogo.pnginpublic, use/logo.pngas the src value.Final Answer:
<img src="/logo.png" alt="Logo" /> -> Option AQuick Check:
Use leading slash and omit 'public' folder name [OK]
- Including 'public' in the path
- Using relative paths like './' or '../'
- Forgetting the leading slash
export default function Logo() {
return <img src="/images/avatar.png" alt="User Avatar" />
}Assuming
avatar.png is located inside public/images folder.Solution
Step 1: Understand public folder URL mapping
Files insidepublicare served from the root URL path, so/images/avatar.pngmaps topublic/images/avatar.png.Step 2: Determine the full URL
When running locally, the base URL ishttp://localhost:3000, so the full URL ishttp://localhost:3000/images/avatar.png.Final Answer:
http://localhost:3000/images/avatar.png -> Option DQuick Check:
public/images/avatar.png = /images/avatar.png URL [OK]
- Adding 'public' to URL path
- Using relative or src folder paths
- Confusing file location with URL
favicon.ico inside the public folder but your browser shows a 404 error when accessing /favicon.ico. What is the most likely cause?Solution
Step 1: Check file location and URL path
If the file is directly insidepublic, accessing/favicon.icoshould work unless the file name or extension is incorrect.Step 2: Verify file name and extension
A common cause of 404 is a misspelled file name or wrong extension likefavicon.ico.pngorfav-icon.ico.Final Answer:
The file name is misspelled or has wrong extension -> Option BQuick Check:
Check exact file name and extension for 404 errors [OK]
- Assuming Next.js needs imports for public files
- Forgetting to include subfolder in URL
- Restarting server unnecessarily
guide.pdf stored in public/docs so users can download it by clicking a link. Which is the correct way to create the link in a Next.js component?Solution
Step 1: Reference public files with leading slash and no 'public' folder name
Files insidepublicare accessed by URL paths starting with/and omit thepublicfolder name.Step 2: Use anchor tag with download attribute for file download
To allow users to download the PDF, use an<a>tag withhref="/docs/guide.pdf"and thedownloadattribute.Final Answer:
<a href="/docs/guide.pdf" download>Download Guide</a> -> Option AQuick Check:
Use leading slash and download attribute on anchor tag [OK]
- Omitting leading slash in href
- Including 'public' in URL path
- Using Next.js Link for static file downloads
Ready to prove it?
Pass the test and this pattern moves unknown
