-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed a bug where loading is true when data already exist fr… (#23)
fix: fixed a bug where loading is true when data already exist from SSR
- Loading branch information
Showing
2 changed files
with
45 additions
and
21 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/react-isomorphic-data/src/hooks/utils/createFetchRequirements.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { DataClient } from '../../common/types'; | ||
import { DataHookOptions } from '../types'; | ||
|
||
// returns an array of FetchOptions and string of fetchPolicy | ||
const createFetchOptions = ( | ||
fetchOptions: RequestInit, | ||
client: DataClient, | ||
dataOpts: DataHookOptions, | ||
lazy = false, | ||
): [RequestInit, string] => { | ||
const finalMethod = fetchOptions.method && lazy ? fetchOptions.method : 'GET'; | ||
let fetchPolicy = dataOpts.fetchPolicy !== undefined ? dataOpts.fetchPolicy : 'cache-first'; | ||
const ssrOpt = dataOpts.ssr !== undefined ? dataOpts.ssr : true; | ||
const isSSR = client.ssr && ssrOpt && typeof window === 'undefined'; | ||
|
||
if (finalMethod !== 'GET') fetchPolicy = 'network-only'; | ||
if (isSSR) fetchPolicy = 'cache-first'; | ||
|
||
return [ | ||
{ | ||
...fetchOptions, | ||
// only allow non-GET request for lazy requests, because non-GET request can be not idempotent | ||
method: finalMethod, | ||
headers: { | ||
...client.headers, // add the base headers added when creating the DataClient | ||
...fetchOptions.headers, // append other headers specific to this fetch | ||
}, | ||
}, | ||
fetchPolicy, | ||
]; | ||
}; | ||
|
||
export default createFetchOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters