DEVLOG

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

frontend/next.js

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

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

     

    ๊ธฐ์กด์— ํฌํŠธํด๋ฆฌ์˜ค ์‚ฌ์ดํŠธ๋ฅผ Next.js๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง„ํ–‰ํ–ˆ๋Š”๋ฐ api ๋ถ™์ผ๊ฒƒ๋„ ์—†๊ณ ,, ์•ˆ์— ๋‚ด์šฉ๋„ ๋„ˆ๋ฌด ๋‹จ์กฐ๋กœ์›Œ์„œ Next.js ๊ธฐ์ˆ ์„ ์กฐ๊ธˆ ๋” ์ตํžˆ๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค!

     

    ๐ŸŽˆ ์ฃผ์ œ์ •ํ•˜๊ธฐ

    api๋ฅผ ๋ถ™์ผ๋งŒํ•œ ํ”„๋กœ์ ํŠธ๋กœ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์ด์ „์— ์ž ๊น ์‚ฌ์šฉํ•ด๋ณด์•˜๋˜ TMDB API์— tving์‚ฌ์ดํŠธ ๋””์ž์ธ์„ ๋”ํ•ด ์˜ํ™” ์ถ”์ฒœ ์‚ฌ์ดํŠธ๋ฅผ ์ œ์ž‘ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค!



    ๐ŸŽˆ ์‚ฌ์šฉ๊ธฐ์ˆ 

    next.js + typescript ๊ธฐ๋ฐ˜์œผ๋กœ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ์Šคํƒ€์ผ ์ž‘์—…์€ styled-components๋ฅผ ์ด์šฉํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

     "next": "13.0.7",
     "react": "18.2.0",
     "react-dom": "18.2.0",
     "styled-components": "^5.3.6"

     

    ๐ŸŽˆ ๊ธฐ๋ณธ์„ธํŒ…ํ•˜๊ธฐ

    _app / _document

    _app์€ ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ ํŽ˜์ด์ง€์— ์ ์šฉํ•  ๊ณตํ†ต ๋ ˆ์ด์•„์›ƒ์˜ ์—ญํ• ์„ ํ•œ๋‹ค. ์ฃผ๋กœ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์— ๊ณตํ†ต์œผ๋กœ ์ ์šฉํ•  ์†์„ฑ์„ ๊ด€๋ฆฌํ•œ๋‹ค.

    _app.tsx

    import type { AppProps } from "next/app";
    import Head from "next/head";
    import "../styles/globals.css";
    
    function MyApp({ Component, pageProps }: AppProps) {
      return (
        <>
          <Head>
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <title>meroriiving</title>
            <link rel="icon" href="/favicon.ico" />
          </Head>
          <Component {...pageProps} />
        </>
      );
    }
    
    export default MyApp;

    _document๋Š” _app ๋‹ค์Œ์— ์‹คํ–‰๋˜๋ฉฐ, ๊ณตํ†ต์œผ๋กœ ํ™œ์šฉํ•  <head>๋‚˜ <body>ํƒœ๊ทธ ์•ˆ์— ๋“ค์–ด๊ฐˆ ๋‚ด์šฉ์„ ์ปค์Šคํ…€ํ•  ๋•Œ ํ™œ์šฉํ•œ๋‹ค.
    styled-components๋Š” ํŽ˜์ด์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ HTML๋งŒ ๊ฐ€์ ธ์˜ค๊ณ , ์Šคํƒ€์ผ์ด ์ ์šฉ๋˜์–ด ์žˆ์ง€ ์•Š์•„ ์„œ๋ฒ„์—์„œ ๋ฏธ๋ฆฌ HTML์„ ๋งˆํฌ์—…ํ• ๋•Œ ์Šคํƒ€์ผ๊นŒ์ง€ ์ž…ํžˆ๋ ค๋ฉด _document์— ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

    _document.tsx

    import Document, {DocumentContext} from 'next/document'
    import {ServerStyleSheet} from 'styled-components'
    
    class MyDocument extends Document {
      static async getInitialProps(ctx: DocumentContext) {
        const sheet = new ServerStyleSheet()
        const originalRenderPage = ctx.renderPage
    
        try {
          ctx.renderPage = () =>
            originalRenderPage({
              enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />)
            })
    
          const initialProps = await Document.getInitialProps(ctx)
    
          return {
            ...initialProps,
            styles: [
              <>
                {initialProps.styles}
                {sheet.getStyleElement()}
              </>
            ]
          }
        } catch (error) {
          throw error
        } finally {
          sheet.seal()
        }
      }
    }
    
    export default MyDocument

     

    next.config.js

    ์ตœ์ดˆ ์„œ๋ฒ„ ์‚ฌ์ด๋“œ์—์„œ ๋ Œ๋”๋ง ์ดํ›„ ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง์œผ๋กœ ๋ผ์šฐํŒ…์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด ๋•Œ ์„œ๋ฒ„์—์„œ ์ƒ์„ฑํ•˜๋Š” ํ•ด์‹œ๊ฐ’๊ณผ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ƒ์„ฑํ•˜๋Š” ํ•ด์‹œ๊ฐ’์ด ์„œ๋กœ ๋‹ฌ๋ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค. (Prop className did not match) ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ด์ „์—๋Š” styled-components ๋ฐ”๋ฒจ ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์„ค์น˜ํ•ด์•ผํ–ˆ๋‹ค.

    ๊ทธ๋Ÿฌ๋‚˜ nextjs 12.1๋ฒ„์ „ ์ด์ƒ๋ถ€ํ„ฐ๋Š” babel-plugin-styled-components๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ , next.config.js์— ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

    compiler: {
      styledComponents: true
    },

     

    ๐ŸŽˆ ๊ตฌ์กฐ

    โ”œโ”€โ”€public
    โ”œโ”€โ”€src
    โ”‚  โ”œโ”€โ”€ common
    โ”‚  โ”‚   โ”œโ”€โ”€ api.ts
    โ”‚  โ”‚   โ””โ”€โ”€ util.ts
    โ”‚  โ”œโ”€โ”€ components
    โ”‚  โ””โ”€โ”€ pages
    โ”‚      โ”œโ”€โ”€ detail
    โ”‚      โ”‚   โ””โ”€โ”€ [id].tsx
    โ”‚      โ”œโ”€โ”€ _app.tsx
    โ”‚      โ”œโ”€โ”€ _document.tsx
    โ”‚      โ””โ”€โ”€ index.tsx
    โ””โ”€โ”€styles

    ๋ฉ”์ธํŽ˜์ด์ง€์™€ ์ƒ์„ธํŽ˜์ด์ง€, ๋ ˆ์ด์•„์›ƒ์„ ์ ์šฉํ•ด์„œ ๊ธฐ๋ณธ๊ตฌ์กฐ๋ฅผ ์„ธํŒ…ํ–ˆ๋‹ค.

     

    ๐Ÿ‘‰ Next?

    ์ดํ›„์—๋Š” TMDB๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ์˜ˆ์ •์ด๋‹ค!

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