6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nuxtのバージョンを3.17以上にアップグレードする際はAPI周りの仕様変更に気をつけろ (Consistent Data Across Components)

Last updated at Posted at 2025-10-01

概要

弊社公式サイトのNuxtのバージョンを脆弱性対応のために3.16.2から3.19.0にアップグレードしたらAPIからのフェッチをしている部分でエラーにはならないが、何も表示されない現象が発生した。

原因

結論から言うと、Nuxt 3.17で追加された"Consistent Data Across Components"という仕組みが原因で本事象が発生していた。

これはどういう仕組みかというと、useAsyncDatauseFetchを使う際に同じキーを与えられた場合、refを共有するようになり、アプリケーション全体でデータの一貫性が担保されるようになるものである。

問題が発生した実装

複数のuseFetchでキーを指定しない実装をしていた。
(下記のような実装が複数あった)

export const fetchNewsContents = () => {
  return useFetch('/api/blog', {
    method: 'GET',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
    })
  })
}

これだと無名のキーが同じキーとして扱われてしまい、たとえば/api/getBlog/api/getNewsが同じデータであると認識されてしまい、不整合が起きていた。

改善

キーを入れよう

export const fetchNewsContents = () => {
  return useFetch('/api/blog', {
    key: 'blog'
    method: 'GET',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
    })
  })
}
6
2
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
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?