DEVLOG

Next.js ํ”„๋กœ์ ํŠธ :: getServersideProps ๋ณธ๋ฌธ

frontend/next.js

Next.js ํ”„๋กœ์ ํŠธ :: getServersideProps

meroriiDev 2023. 6. 22. 15:16
728x90
๋ฐ˜์‘ํ˜•
๐Ÿ“–  ๋ชฉ์ฐจ

     

    ๐ŸŽˆ ์™œ SSR์„ ๋„์ž…ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๊ฐ€?

    ์ด์ „๊ธ€์—์„œ react-query๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€ ํŽ˜์ด์ง€์— ๊ทธ๋ ค์ฃผ์—ˆ๋‹ค.
    ํ•˜์ง€๋งŒ ํด๋ผ์ด์–ธํŠธ๋‹จ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ ์ง„์ž…์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด getServerSideProps๋ฅผ ์ด์šฉํ•˜์—ฌ react-query์˜ initialData๋ฅผ ์ฑ„์›Œ์ฃผ์—ˆ๋‹ค.

    ๊ทธ๋Ÿฐ๋ฐ ์ž‘์—…์„ ํ•˜๋‹ค๋ณด๋‹ˆ,,, ๋‚ด๊ฐ€ ์ง€๊ธˆ ์“ฐ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€ํ• ์ผ์ด ์–ผ๋งˆ๋‚˜ ์žˆ๋‹ค๊ณ  ๊ตณ์ด react-query๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์†ํ•ด์„œ ํŽ˜์ด์ง€๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์•ผํ•  ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ? ์žฌํฌ์ปค์‹ฑ์ด ๋ ๋•Œ๋งˆ๋‹ค ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ„์† ์ƒˆ๋กœ ๋ถˆ๋Ÿฌ์™€์ฃผ๋Š”๊ฒŒ ๋งž์„๊นŒ?ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

    ๊ทธ๋ž˜์„œ ์žฌ๋ Œ๋”๋ง๋˜๋Š” ๋ฌธ์ œ๋ฅผ ์—†์• ๊ณ , SEO๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด ์„œ๋ฒ„์‚ฌ์ด๋“œ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ํŽ˜์ด์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋‚ด๋ถ€์—์„œ ์—…๋ฐ์ดํŠธ ๋˜๋˜ react-query๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค.

    ๊ทธ๋žฌ๋”๋‹ˆ ์•„๋ฌด ๋ฌธ์ œ๋„ ์—†์ด ์ž˜ ๊ตฌํ˜„๋˜์—ˆ๊ณ , ์‹ค์ œ๋กœ ๋Š๋ผ๋Š” ํŽ˜์ด์ง€ ์ „ํ™˜ ์†๋„๋„ ํ›จ์”ฌ ๋” ๋นจ๋ผ์ง„ ๊ฒƒ ๊ฐ™์•˜๋‹ค!

     

    ๐ŸŽˆ ์˜ํ™”๋ฆฌ์ŠคํŠธ์กฐํšŒ(๋ฉ”์ธ)

    const Home: NextPage<HomePageProps> = (props) => {
      const { popular, topRated, nowPlaying, upcoming } = props;
          ...
    
    };
    export default Home;
    
    ...
    
    //getServerSideProps์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ด ์ปดํฌ๋„ŒํŠธ์— props๋กœ ๋„˜๊น€
    export const getServerSideProps: GetServerSideProps = async () => {
      const popular = await getMovieList("popular");
      const topRated = await getMovieList("top_rated");
      const nowPlaying = await getMovieList("now_playing");
      const upcoming = await getMovieList("upcoming");
    
      return {
        props: {
          popular: popular.results,
          topRated: topRated.results,
          nowPlaying: nowPlaying.results,
          upcoming: upcoming.results,
        },
      };
    };

    ๐ŸŽˆ ์˜ํ™”์ •๋ณด์กฐํšŒ(์ƒ์„ธ)

    const Contents: NextPage<ContentPageProps> = (props) => {
      const { content, similar, recommend } = props;
      ...
    } 
    export default Contents;
    
    ...
    
    //getServerSideProps์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ด ์ปดํฌ๋„ŒํŠธ์— props๋กœ ๋„˜๊น€
    export const getServerSideProps: GetServerSideProps = async ({
      params,
    }: any) => {
      const contentData = await getMovieInfo(params.id);
      const creditData = await getCredits(params.id);
      const similarData = await getSimilar(params.id);
      const recommendData = await getRecommendations(params.id);
    
      return {
        props: {
          params,
          content: { ...contentData, ...creditData },
          similar: similarData.results,
          recommend: recommendData.results,
        },
      };
    };

     

    โ— unexpected end of file ์˜ค๋ฅ˜

    ๋ฌธ์ œ์—†์ด ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋˜ ์ค‘ ๊ฐ‘์ž๊ธฐ ์•„๋ž˜์™€ ๊ฐ™์ด axios์ธก์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

    Server Error
    Error: unexpected end of file
    
    This error happened while generating the page. Any console logs will be displayed in the terminal window.

    unexpected end of file ์˜ค๋ฅ˜

     

    AxiosError: unexpected end of file · Issue #5346 · axios/axios

    Describe the bug AxiosError: unexpected end of file Was not present before 1.2.1 To Reproduce No response Code snippet No response Expected behavior No response Axios Version 1.2.1 Adapter Version ...

    github.com

    ์œ„ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•ด๋‹น ์˜ค๋ฅ˜๊ฐ€ axios ๋ฒ„์ „ 1.2์ด์ƒ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋ผ๋Š” ๊ฒƒ์„ ์•Œ์•„๋‚ด์—ˆ๊ณ , axios๋ฅผ 1.1.3์œผ๋กœ ๋‹ค์šด๊ทธ๋ ˆ์ด๋“œํ•จ์œผ๋กœ์จ ํ•ด๊ฒฐํ–ˆ๋‹ค.


    ๐Ÿ‘‰ Next?

    ๋ฉ”์ธํŽ˜์ด์ง€์—์„œ ์กฐํšŒํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ carousel๋กœ ์ œ์ž‘ํ•  ์˜ˆ์ •์ด๋‹ค.

    728x90
    ๋ฐ˜์‘ํ˜•
    Comments