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?

NextJSで、サーバーサイド、クライアントサイドを問わずにOriginを取得する関数がこちら

Last updated at Posted at 2024-12-23

この関数を使えば、Server side, Client sideを問わず、Originを取得できる。

export const getOrigin = () => {
  if (isServerEnvironment()) {
    import('next/headers').then((module) => {
      const headersList = module.headers();
      return headersList.get('origin');
    });
  } else {
    return window.location.origin;
  }
};

const isServerEnvironment = () => typeof window === 'undefined';

使用例

const origin = getOrigin()
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?