Skip to content

Commit ea839f4

Browse files
committed
fix(SSR): show console error instead of throw errors for Maximum executing times while fetching axio
Provide some debug info as well
1 parent 036a8c9 commit ea839f4

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ _Each property is optional_
451451
| ------------------------------- | ----------------------------------------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
452452
| cache | LRU<String, ReactUseApi.CacheData \| any> | new LRU() | The cache instance based on lru-cache |
453453
| axios | AxiosStatic \| AxiosInstance | axios | axios instance (http client) |
454-
| maxRequests | number | 50 | The maximum of API requests when SSR |
454+
| maxRequests | number | 100 | The maximum of API requests when SSR |
455455
| useCacheData | boolean | true | Set true to inject JS cache data into html when calling `injectSSRHtml()` |
456456
| alwaysUseCache | boolean | false | Set true to use cache data always (equivalent to `options.useCache = true`) |
457457
| clearLastCacheWhenConfigChanges | boolean | true | This is default behavior that the cached data will be removed once the url config of useApi has been changed. Set false to remain the cached data |

src/__tests__/ssr.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ jest.mock('../common', () => {
1414

1515
const { feedRequests, injectSSRHtml, loadApiCache } = ssrModule
1616
const originalLog = console.log
17+
const originalErrorLog = console.error
1718

1819
beforeEach(() => {
1920
jest.resetModules()
2021
})
2122

2223
afterEach(() => {
2324
console.log = originalLog
25+
console.error = originalErrorLog
2426
})
2527

2628
const copySettings = () => ({
@@ -130,7 +132,7 @@ describe('feedRequests tests', () => {
130132
0
131133
)
132134
expect(console.log).toHaveBeenCalledWith(
133-
'[ReactUseApi][Executed times] =',
135+
'[ReactUseApi][Executed Times] =',
134136
1
135137
)
136138
expect(ssrHtml).toBe('<div>Hello World!</div>')
@@ -184,7 +186,7 @@ describe('feedRequests tests', () => {
184186
} = context
185187
const ssrHtml = await feedRequests(context, '')
186188
expect(console.log).toHaveBeenCalledWith(
187-
'[ReactUseApi][Executed times] =',
189+
'[ReactUseApi][Executed Times] =',
188190
0
189191
)
190192
expect(ssrConfigs.length).toBe(0)
@@ -206,8 +208,10 @@ describe('feedRequests tests', () => {
206208
cacheKey: '',
207209
},
208210
]
209-
await expect(feedRequests(context, '', 0)).rejects.toThrow(
210-
/Maximum executing times while fetching axios requests/
211+
console.error = jest.fn()
212+
await feedRequests(context, '', 0)
213+
expect(console.error.mock.calls[0][0]).toEqual(
214+
'[ReactUseApi][ERROR] - Maximum executing times while fetching axios requests'
211215
)
212216
})
213217
})

src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import LRU from 'lru-cache'
55
export const defaultSettings = {
66
cache: new LRU<string, ReactUseApi.CacheData | any>(),
77
axios: axios as AxiosStatic | AxiosInstance,
8-
maxRequests: 50, // max requests count when running ReactDom.renderToString in SSR
8+
maxRequests: 100, // max requests count when running ReactDom.renderToString in SSR
99
useCacheData: true, // whether to use the cached api data come from server
1010
alwaysUseCache: false, // whether to use the cached api data always for each api call
1111
clearLastCacheWhenConfigChanges: true, // clear the last cache data with the last config when the config changes

src/ssr.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const feedRequests = async (
1414
if (debug) {
1515
console.log('[ReactUseApi][Requests Count] =', cacheKeys.size)
1616
console.log(
17-
'[ReactUseApi][Executed times] =',
17+
'[ReactUseApi][Executed Times] =',
1818
settings.maxRequests - maxRequests
1919
)
2020
}
@@ -23,7 +23,15 @@ export const feedRequests = async (
2323
}
2424
debug && console.log('[ReactUseApi][Collecting Requests]')
2525
if (maxRequests === 0) {
26-
throw new Error('Maximum executing times while fetching axios requests')
26+
console.error(
27+
'[ReactUseApi][ERROR] - Maximum executing times while fetching axios requests',
28+
'[congis]:',
29+
ssrConfigs,
30+
'[Executed Times]:',
31+
settings.maxRequests - maxRequests
32+
)
33+
cacheKeys.clear()
34+
return ssrHtml // done
2735
}
2836

2937
// The algorithm is collecting the unobtained API request config from the previous renderSSR()
@@ -44,7 +52,7 @@ export const feedRequests = async (
4452
})
4553
} catch (error) {
4654
// is an axios error
47-
if (error.response && error.response.data) {
55+
if (error?.response?.data) {
4856
cache.set(cacheKey, {
4957
// should not be error (Error) object in SSR, it will lead an error: Converting circular structure to JSON
5058
error: error.response,

0 commit comments

Comments
 (0)