๋ค์๊ณผ ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๊ธฐ๋ฅผ ์ํฉ๋๋ค.
import image from './image.png; // image variable should have static url
๋ด ๋ก๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค( next.config.js
):
module.exports = {
webpack: config => {
// handle images
config.module.rules.push({
test: /\.(ico|gif|png|jpg|jpeg|svg|webp)$/,
use: [
{
loader: 'emit-file-loader',
options: {
name: 'dist/[path][name].[ext]',
},
},
{
loader: 'file-loader',
options: {
context: 'public',
name: '/static/images/[name]-[hash].[ext]',
publicPath: '/',
},
},
],
});
return config;
},
};
๊ฒฐ๊ณผ๋ ๊ฑฐ์ ์ข์ต๋๋ค.
์ด๋ฏธ์ง ํ์ผ์ .next/static/images/image-ef89065b898bb1e3e802c62e4b264417.png
image
๋ณ์๋ /static/images/image-ef89065b898bb1e3e802c62e4b264417.png
์ด์ง๋ง http://localhost:3000/static/images/image-ef89065b898bb1e3e802c62e4b264417.png
์ด๋ฏธ์ง๋ฅผ ๋ก๋ํ๋ ค๊ณ ํ๋ฉด 404 ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ด๋ค ์์ด๋์ด?
์ถ์ . ์๋์ผ๋ก ํ์ผ์ static
ํด๋๋ก ์ด๋ํ๊ณ ์ถ์ง ์์ต๋๋ค. ์ผ๋ถ ๊ตฌ์ฑ ์์์ ๊ด๋ จ๋ ์ด๋ฏธ์ง๊ฐ ๊ตฌ์ฑ ์์ ์์ฒด์ ๋์ผํ ํด๋์ ์๊ธฐ๋ฅผ ์ํฉ๋๋ค.
ํ์ฌ ์ด๊ฒ์ ๋ถ๊ฐ๋ฅํฉ๋๋ค.
์ด๋ฅผ ์ํด ๋ ๋นจ๋ฆฌ ์กฐ์น๋ฅผ ์ทจํ ์ ์์ต๋๋ค.
์ด๊ฒ์ ๋ํ ์์์ด ์์ต๋๊น?
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
ํ์ฌ ์ด๊ฒ์ ๋ถ๊ฐ๋ฅํฉ๋๋ค.
์ด๋ฅผ ์ํด ๋ ๋นจ๋ฆฌ ์กฐ์น๋ฅผ ์ทจํ ์ ์์ต๋๋ค.