✏️ Next.js

[pages] 서버 사이드 렌더링만 가능하게 하기

category
✏️ Next.js
date
thumbnail
slug
next-js-getServerSideProps
author
status
Public
tags
pages router
summary
type
Post

getServerSideProps

로딩 없이 html 먼저 보여주는 것 가능
 
예시 코드
export async function getServerSideProps() {
  const { results } = await (
    await fetch('http://localhost:3000/api/movies')
  ).json();
  return {
    props: {
      results,
    },
  };
}
 
서버를 다루는 코드이기에 주소는 절대경로여야 한다. 프론트에서 다루는 URL을 모른다.
⇒ 프론트에서 /api/movies 라고 사용했어도 해당 함수 내에서는 http부터 적어주어야 한다.
 
 

참고