0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Next.jsのSSR・SSG・CSRの挙動

Posted at

はじめに

プログラミング初学者です。
アプリ開発会社で短期インターンをしながらエンジニア転職を目指して日々勉強しています。
間違いなどありましたらご指摘いただけますと幸いです🙏

Next.jsでポートフォリオを作成する中で、
レンダリング手法の違いがよくわからなかったため、
実際の挙動の違いを実験してみたメモ。

環境・ツール

  • Next.js
  • Express
  • Typescript
  • mySQL
  • Prisma
  • DBearver

前提

以下のようにSampleUserテーブルを作成して、データ(SampleUser)を1件追加。
スクリーンショット 2023-03-13 21.43.03.png
スクリーンショット 2023-03-13 21.44.09.png
Expressにて簡易的にAPIサーバーを作成。

index.ts
// sample api
// SampleUserテーブルからデータを取得
app.get("/sample_user", async (rea: express.Request, res: express.Response) => {
  const user = await prisma.sampleUser.findUnique({
    where: { id: 1 },
  });
  return res.json(user);
});

SampleUserの情報を変更した際の、
各レンダリング手法の挙動の違いを確認してみる。

SSRの場合

sample_ssr.tsx
export default function SampleSSR(props: Props) {
  const user = props.user;
  return (
    <div style={{ padding: 10 }}>
      <h1>SSR:サンプルユーザーのプロフィール</h1>
      <p>firstname: {user.firstname}</p>
      <p>lastname: {user.lastname}</p>
      <p>email: {user.email}</p>
      <p>age: {user.age}</p>
    </div>
  );
}

export const getServerSideProps = async () => {
  const res = await axios.get("http://localhost:3333/sample_user");
  const data = res.data;
  return {
    props: {
      user: data,
    },
  };
};

ビルドしてサーバーを起動し、ブラウザを確認する。

$ npm run build
$ npm run start

スクリーンショット 2023-03-13 21.51.16.png
DBeaverでSampleUserのlastnameとageを変更してみる。
スクリーンショット 2023-03-13 21.54.09.png
sample_ssr.tsxのコードは何も変更せずにブラウザを更新。
スクリーンショット 2023-03-13 21.57.36.png
firstnameとageが変更後の値に更新されている。

SSGの場合

SmapleUserの値は初期(25歳の山田さん)に戻す。

sample_ssg.tsx
export default function SampleSSG(props: Props) {
  const user = props.user;
  return (
    <div style={{ padding: 10 }}>
      <h1>SSR:サンプルユーザーのプロフィール</h1>
      <p>firstname: {user.firstname}</p>
      <p>lastname: {user.lastname}</p>
      <p>email: {user.email}</p>
      <p>age: {user.age}</p>
    </div>
  );
}

export const getStaticProps = async () => {
  const res = await axios.get("http://localhost:3333/sample_user");
  const data = res.data;
  return {
    props: {
      user: data,
    },
  };
};

ビルドしてサーバーを起動、ブラウザを確認。
※ビルドせずにnpm run devで起動すると、SSGでもSSRと同じ挙動になるため注意
スクリーンショット 2023-03-13 22.05.39.png
SSRの場合と同様にDBeaverでSampleUserのlastnameとageを変更。
sample_ssg.tsxのコードは変更せずにブラウザを更新。
スクリーンショット 2023-03-13 22.05.39.png
firstnameとageの値は変わらず。。。
ここがSSRとSSGの挙動の違い!

再度ビルドしてサーバーを再起動しブラウザを確認。
スクリーンショット 2023-03-13 22.09.19.png
firstnameとageが変更後の値に更新された。

SSG + CSRの場合

SmapleUserの値は初期(25歳の山田さん)に戻す。
sample_ssg.tsxを一部変更。

sample_ssg.tsx
export default function SampleSSG(props: Props) {
  const [user, setUser] = useState(props.user);

  useEffect(() => {
    const getSample = async () => {
      console.log("effect");
      const res = await axios.get("http://localhost:3333/sample_user");
      setUser(res.data);
    };
    getSample();
  }, []);

  return (
    <div style={{ padding: 10 }}>
      <h1>SSG+CSR:サンプルユーザーのプロフィール</h1>
      <p>firstname: {user.firstname}</p>
      <p>lastname: {user.lastname}</p>
      <p>email: {user.email}</p>
      <p>age: {user.age}</p>
    </div>
  );
}

export const getStaticProps = async () => {
  const res = await axios.get("http://localhost:3333/sample_user");
  const data = res.data;
  return {
    props: {
      user: data,
      enableCSR: true,
    },
  };
};

ビルドしてサーバーを起動、ブラウザを確認。
スクリーンショット 2023-03-13 22.20.30.png
SSRの場合と同様にDBeaverでSampleUserのlastnameとageを変更。
sample_ssg.tsxのコードは変更せずにブラウザを更新。
スクリーンショット 2023-03-13 22.35.28.png
firstnameとageが変更後の値に更新!

まとめ

SSRとSSGの違いの理解に苦しんだが、
実際に挙動を比べることで理解ができた気がする。
SSGのページでCSRを実行する方法は改めて時間をとって確認する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?