What is the purpose of the public directory in a Next.js project?
The public directory holds static files like images, icons, and other assets that can be accessed directly by the browser via a URL path starting with /. It is used to serve files without processing by Next.js.
Click to reveal answer
beginner
How do you reference an image stored in the public directory inside a Next.js component?
You reference it by using the root-relative path starting with /. For example, if the image is at public/logo.png, use <img src="/logo.png" alt="Logo" />.
Click to reveal answer
intermediate
Can files inside the public directory be processed by Next.js (like transpiled or bundled)?
No, files in the public directory are served as-is without any processing or bundling by Next.js. They are static assets accessible directly by URL.
Click to reveal answer
intermediate
Why should you use the public directory for static assets instead of importing them in Next.js components?
Using the public directory is best for assets that do not need to be bundled or optimized by Next.js, such as favicon, robots.txt, or large media files. Importing assets triggers bundling and optimization, which is better for smaller or dynamic assets.
Click to reveal answer
beginner
What URL path would you use to access a file named robots.txt placed inside the public directory?
You would access it directly at /robots.txt in the browser, because files in public map to the root URL path.
Click to reveal answer
Where should you place a static image file to serve it directly in Next.js?
AInside the <code>pages</code> directory
BInside the <code>public</code> directory
CInside the <code>components</code> directory
DInside the <code>api</code> directory
✗ Incorrect
Static files like images should be placed in the public directory to be served directly.
How do you reference a file named banner.jpg stored in public/images inside an <img> tag?
Hint: Files in public map directly to root URL paths [OK]
Common Mistakes:
Adding 'public' to URL path
Using relative or src folder paths
Confusing file location with URL
4. You added a file favicon.ico inside the public folder but your browser shows a 404 error when accessing /favicon.ico. What is the most likely cause?
medium
A. The file is inside a subfolder of public but URL does not include it
B. The file name is misspelled or has wrong extension
C. Next.js requires importing favicon in _app.js to serve it
D. You need to restart the Next.js server after adding files to public
Solution
Step 1: Check file location and URL path
If the file is directly inside public, accessing /favicon.ico should 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 like favicon.ico.png or fav-icon.ico.
Final Answer:
The file name is misspelled or has wrong extension -> Option B
Quick Check:
Check exact file name and extension for 404 errors [OK]
Hint: Check exact file name and extension if 404 occurs [OK]
Common Mistakes:
Assuming Next.js needs imports for public files
Forgetting to include subfolder in URL
Restarting server unnecessarily
5. You want to serve a PDF file named 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?
hard
A. <a href="/docs/guide.pdf" download>Download Guide</a>
B. <a href="docs/guide.pdf" download>Download Guide</a>
C. <Link href="/docs/guide.pdf">Download Guide</Link>
D. <a href="/public/docs/guide.pdf" download>Download Guide</a>
Solution
Step 1: Reference public files with leading slash and no 'public' folder name
Files inside public are accessed by URL paths starting with / and omit the public folder name.
Step 2: Use anchor tag with download attribute for file download
To allow users to download the PDF, use an <a> tag with href="/docs/guide.pdf" and the download attribute.
Final Answer:
<a href="/docs/guide.pdf" download>Download Guide</a> -> Option A
Quick Check:
Use leading slash and download attribute on anchor tag [OK]
Hint: Use <a> with href starting '/' and download attribute [OK]
Common Mistakes:
Omitting leading slash in href
Including 'public' in URL path
Using Next.js Link for static file downloads
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.