๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ“บ Front End/- React, Next.js

[Next.js] Next.js์—์„œ ์ด๋ฏธ์ง€์™€ ๋™์˜์ƒ์„ ์ €์žฅํ•˜๊ณ  ์‚ฌ์šฉํ•ด๋ณด์ž.

by Wonit 2020. 12. 30.

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Next JS๊ฐ€ ์–ด๋–ป๊ฒŒ static Assets๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š”์ง€ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค.
Static Assets ์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ Jpeg, png, svg์™€ ๊ฐ™์€ ์ด๋ฏธ์ง€๋‚˜ mp4, mov์™€ ๊ฐ™์€ Video ๋ฐ์ดํ„ฐ์ด๋‹ค.

assets

next๋Š” ์ด๋ฏธ์ง€๊ฐ™์€ static assets๋“ค์„ public ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค.
๋งŒ์•ฝ ์ด๋Ÿฐ asset๋“ค์„ ์‚ฌ์šฉํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด pages ๋””๋ ‰ํ† ๋ฆฌ์—์„œ public ์•„๋ž˜์— ์žˆ๋Š” asset๋“ค์„ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์˜ˆ์ œ ํ•ด๋ณด์ž

 

์šฐ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฏธ์ง€์™€ ์˜์ƒ์„ ๋ถˆ๋Ÿฌ๋ณด๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด๋ณผ ๊ฒƒ์ด๋‹ค.

 

npm ๋ชจ๋“ˆ ์„ค์น˜

์šฐ์„  styled-components๋ฅผ npm ๋ชจ๋“ˆ์— ์ถ”๊ฐ€์‹œ์ผœ๋ณด์ž.

// npm ์‚ฌ์šฉ์ž์ผ ๊ฒฝ์šฐ
> $ npm install styled-components

// yarn ์‚ฌ์šฉ์ž์ผ ๊ฒฝ์šฐ
> $ yarn add styled-components

๋ฅผ ์ž…๋ ฅํ•ด์„œ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€์‹œ์ผœ์ฃผ์ž.

asset ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ

์ด์ œ asset๋“ค์„ ์ €์žฅ์‹œํ‚ฌ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค ๊ฒƒ์ธ๋ฐ, public ์•„๋ž˜์— 2๊ฐ€์ง€ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด๋‹ค.

  • public/iamges ๋””๋ ‰ํ† ๋ฆฌ
  • public/movies ๋””๋ ‰ํ† ๋ฆฌ

๊ฑฐ๊ธฐ์— assets๋“ค์„ ๋„ฃ๊ณ  ์‚ฌ์šฉํ•  ๋•Œ๋Š” src="/images/..." ์ด๋Ÿฐ์‹์œผ๋กœ public ์•„๋ž˜์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

ํ˜น์‹œ ๊ทธ๋Ÿด์ผ ์—†๊ฒ ์ง€๋งŒ public ์ด๋ผ๊ณ  ๋‹ค๋ฅธ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ด๋ฆ„์ง€์œผ๋ฉด ์ค‘๋ณต์œผ๋กœ ๊ฒ€์ƒ‰๋˜๋‹ˆ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

 

์ด์ œ ์ฝ”๋”ฉ์„ ํ•ด๋ณด์ž.

 

index.jsx

import Link from "next/Link";
import styled from "styled-components";

const RootContainer = styled.div`
  display: flex;
  align-items: center;
  justify-content: center;
  background: wheat;
`;

const ImageContainer = styled.div`
  width: 600px;
  height: 600px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid;
  border-radius: 10px;
`;

const ImageText = styled.p``;
const ImageBox = styled.img`
  width: 400px;
  height: 400px;
  display: block;
`;

const MovieContainer = styled.div`
  width: 600px;
  height: 600px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid;
  border-radius: 10px;
`;

const MovieText = styled.p``;

const MovieBox = styled.video`
  width: 400px;
  height: 400px;
  display: block;
`;

export default function Home() {
  return (
    <RootContainer>
      <ImageContainer>
        <ImageBox src="/images/xd.png" />
        <ImageText>์ด๋ฏธ์ง€</ImageText>
      </ImageContainer>
      <MovieContainer>
        <MovieBox src="/videos/video.mov" />
        <MovieText>์˜์ƒ</MovieText>
      </MovieContainer>
    </RootContainer>
  );
}

 

styled-components๋ฅผ ์ด์šฉํ•ด์„œ ์ฝ”๋”ฉ์„ ํ–ˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ styled-components๊ฐ€ ์•„๋‹ˆ๋ผ src="/images/xd.png" ์™€ ๊ฐ™์€ ๊ฒฝ๋กœ์ด๋‹ค.

next๋Š” public ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์žก๊ณ  ์žˆ์œผ๋‹ˆ path์— ์ฃผ์˜ํ•˜์ž.

 

๋ฒˆ์™ธ๋กœ robots.txt๋„ public ์•„๋ž˜์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค!

๊ทธ๋ฆฌ๊ณ  public ํด๋” ์•„๋ž˜์—๋Š” robots.txt ํŒŒ์ผ๋„ ์œ„์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
robots.txt ํŒŒ์ผ์€ Google์ด Site๋ฅผ ๊ฒ€์ฆํ•  ๋•Œ ํ•ด๋‹น ํŒŒ์ผ์„ ๊ฒ€์ฆํ•˜๋Š”๋ฐ ์ด๊ฑด ์œ„ํ‚ค ๋ฐฑ๊ณผ robot.txt์„ ์ฝ์–ด๋ณด๋ฉด ๋œ๋‹ค.

๋Œ“๊ธ€