1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| import { SWRConfig } from 'swr'
function App() {
return (
<SWRConfig
value={{
fetcher: (url) => fetch(url).then((res) => res.json()),
onError: (error, key) => {
if (error.status !== 403 && error.status !== 404) {
// エラーログを送信
console.error(`SWR Error for ${key}:`, error)
}
},
onErrorRetry: (error, key, config, revalidate, { retryCount }) => {
// 404では再試行しない
if (error.status === 404) return
// 特定のキーでは再試行しない
if (key === '/api/user') return
// 最大10回まで再試行
if (retryCount >= 10) return
// 5秒後に再試行
setTimeout(() => revalidate({ retryCount }), 5000)
},
}}
>
<YourApp />
</SWRConfig>
)
}
|