1
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でクライアントコンポーネントから呼び出せない関数を作成する

Posted at

はじめに

この記事は server-only挙動理解使い方 の備忘録です。

インストール

npm install server-only

server-only とは

サーバコンポーネントからのみ呼び出したい関数クライアントコンポーネント から
呼び出せなくするライブラリ です。

import "server-only"

と記述することで 適用 出来ます。

挙動理解

クライアントコンポーネント から server-onlyを使った関数 を呼び出すが
エラーがスロー されて呼び出せない 実装例 を下記に示します。

app/page.tsx
"use client";

import { serverSideExecution } from "@/datas/serverSideExecution";

export default function Home() {
  const temp = serverSideExecution();

  return (
    <div>{temp}</div>
  );
}
datas/serverSideExecution.ts
import "server-only"

export const serverSideExecution = () => {
    return "サーバ側でのみ実行されます。"
}

image.png

このような エラーをスロー します。

さいごに

学習させていただいた先・ライブラリの公式ドキュメント・リンクです。

1
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
1
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?