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?

nextjs 16でビルドしたら2 [unknown] During prerendering, `headers()`…というwarningが出た

Posted at

nextjs 16にアップデートしてビルドしたら下記エラー

2 [unknown] During prerendering, `headers()` rejects when the prerender is complete. 
Typically these errors are handled by React but if you move `headers()` to a different context by using `setTimeout`, `after`, or similar functions you may observe this error and you should handle it in that context. 
This occurred at route "/sample/path".
codeHandling_error {"route":"/sample/path","expression":"`headers()`","digest":"HANGING_PROMISE_REJECTION"}

/sample/pathにしています

該当エラーをnext-devtoolsに聞いたら、下記回答が得られました。

問題の詳細
Next.js 16の動作:
headers() はプリレンダリング中に呼ばれると、プリレンダリング完了後にrejectされる
Promise.all や setTimeout、after などの非同期コンテキスト内で headers() を呼び出すと、Promiseが適切にハンドリングされない(HANGING_PROMISE_REJECTION)

とのことですが、headerやcookiesからjwtを取り出してログインユーザーの情報を取り出すような処理は変えることが難しいです。だけどビルド時にwarningうざい。。。

結論: warningを消すには"use cache: private";を使うと消える

"use cache: private"とは

公式ドキュメント:https://nextjs.org/docs/app/api-reference/directives/use-cache-private

headersやcookiesを使っていても、使えるディレクティブとしてv16から追加されています

注意

まだv16はリリースされたばかりで、"use cache: private";の使い所と実際に挙動を確かめてください。
キャッシュ戦略に関わるところなので、プロジェクト・データの性質に合わせて使ったほうよいです。あくまでwarning対処の参考に。

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?