Quantcast
Channel: プログラミング
Viewing all articles
Browse latest Browse all 8502

Failed to parse URL when fetching localhost using Server Components - しがないエンジニア頑張る

$
0
0

このエラーに遭遇した。まんまこのisuueなのでこちらを読めばOK。

Failed to parse URL when fetching localhost using Server Components · Issue #48344 · vercel/next.js · GitHub

クライアント側の処理とサーバー側の処理の違い

クライアントコンポーネントではfetch時にURLにホストをつけてくれるが、サーバーコンポーネントから呼び出した処理にはつけてはくれない。
その違いでエラーが発生していた。

対策

便利関数を作って処理に挟むことにした。

exportfunctiongetBaseUrl(){if (typeofwindow !== 'undefind') return'';
  const host = headers().get('host');
  const protcol = process?.env.NODE_ENV === 'development' ? 'http' : 'https';
  return`${protcol}://${host}`;
}

Viewing all articles
Browse latest Browse all 8502

Trending Articles