๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Next.js

[๋‚ด๋ณด๋‚ด๋ฒˆ] Next.js docs - Static File Serving

๋‚ด๋ณด๋‚ด๋ฒˆ(๋‚ด๊ฐ€ ๋ณด๋ ค๊ณ  ๋‚ด๊ฐ€ ๋ฒˆ์—ญํ•œ...) Next.js docs

2021๋…„ 4์›” 1์ผ ๊ธฐ์ค€ Next.js ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ฒˆ์—ญํ–ˆ๋‹ค.

โ€ป ์˜์–ด ์ „๊ณต์ž๋„ ํ•ด์™ธ ์œ ํ•™ํŒŒ๋„ ์•„๋‹ˆ๊ธฐ์— ๋ฒˆ์—ญ์—๋Š” ์˜์—ญ, ์˜ค์—ญ, ๊ตฌ๊ธ€ ๋ฒˆ์—ญ์ด ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ˜ผ์ž ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•ด๊ฐ€๋ฉฐ ๋ฒˆ์—ญํ•˜๋‹ค ๋ณด๋‹ˆ ์˜คํƒ€๋„ ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค. ์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ง์ ‘ ์‚ดํŽด๋ณด๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ •๋ณด๋“ค์„ ๋” ์ฐพ์•„๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

(ํ•˜์ง€๋งŒ ๋Œ“๊ธ€ ํ”ผ๋“œ๋ฐฑ๋„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ƒ )

Next.js ๊ณต์‹๋ฌธ์„œ ํ™•์ธํ•˜๊ธฐ>>

 

Basic Features: Static File Serving | Next.js

Next.js allows you to serve static files, like images, in the public directory. You can learn how it works here.

nextjs.org

Next.js can serve static files, like images, under a folder called public in the root directory. Files inside public can then be referenced by your code starting from the base URL (/).

Next.js๋Š” public์ด๋ผ๋Š” ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ ์•„๋ž˜์— ์žˆ๋Š” ํด๋” ๋‚ด์˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ์ •์  ํŒŒ์ผ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด public ํด๋” ๋‚ด์— ํŒŒ์ผ๋“ค์€ base URL(/)๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ฝ”๋“œ์—์„œ ์ฐธ์กฐ ๋  ์ˆ˜ ์žˆ๋‹ค. 

For example, if you add an image to public/me.png, the following code will access the image:

์˜ˆ๋ฅผ ๋“ค์–ด public/me.png ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋ฉด ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ด๋ฏธ์ง€์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 

Note: next/image requires Next.js 10 or later.
๋…ธํŠธ: next/image๋Š” Next.js 10์ด๋‚˜ ๊ทธ ์ด์ƒ์˜ ๋ฒ„์ „์ด ์š”๊ตฌ๋œ๋‹ค.

This folder is also useful for robots.txt, favicon.ico, Google Site Verification, and any other static files (including .html)!

์ด ํด๋”๋Š”(๋ฃจํŠธ ์•„๋ž˜ public ํด๋”) robots.txt, favicon.ico, ๊ตฌ๊ธ€ ์‚ฌ์ดํŠธ ์ธ์ฆ, ๊ทธ ๋ฐ–์˜ ์ •์  ํŒŒ์ผ๋“ค( .html์„ ํฌํ•จํ•œ)์—๋„ ์œ ์šฉํ•˜๋‹ค!

Note: Don't name the public directory anything else. The name cannot be changed and is the only directory used to serve static assets.
๋…ธํŠธ: public ํด๋”๋ฅผ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ง€์ •ํ•˜์ง€ ๋ง๋ผ. ์ด ์ด๋ฆ„์€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์—†๊ณ  ์ •์  ์ž์‚ฐ๋“ค์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์œ ์ผํ•œ ๋””๋ ‰ํ† ๋ฆฌ์ด๋‹ค. 
Note: Be sure to not have a static file with the same name as a file in the pages/ directory, as this will result in an error.
Read more: https://nextjs.org/docs/messages/conflicting-public-file-page
๋…ธํŠธ: pages/ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ํŒŒ์ผ๊ณผ ์ด๋ฆ„์ด ๊ฐ™์€ ์ •์  ํŒŒ์ผ์˜ ์ด๋ฆ„์ด ์žˆ์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 
Note: Only assets that are in the public directory at build time will be served by Next.js. Files added at runtime won't be available. We recommend using a third party service like AWS S3 for persistent file storage.
๋…ธํŠธ: public ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ์ž์‚ฐ๋“ค๋งŒ์ด ๋นŒ๋“œ ์‹œ๊ฐ„์— Next.js์— ์˜ํ•ด ์ œ๊ณต๋  ๊ฒƒ์ด๋‹ค. ๋Ÿฐํƒ€์ž„์— ์ถ”๊ฐ€๋˜๋Š” ํŒŒ์ผ๋“ค์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
์˜๊ตฌ ํŒŒ์ผ ์Šคํ† ๋ฆฌ์ง€๋ฅผ ์œ„ํ•ด AWS S3์™€ ๊ฐ™์€ ์„œ๋“œ ํŒŒํ‹ฐ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.