Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-react",
"version": "3.7.2",
"version": "3.7.3",
"description": "React hooks for data fetching",
"main": "dist/index.js",
"scripts": {
Expand Down
45 changes: 13 additions & 32 deletions src/components/FetchConfigAsync.tsx
Original file line number Diff line number Diff line change
@@ -1,66 +1,47 @@
import { isDefined, serialize } from '../client'
import {
defaultCache,
fetcherDefaults,
valuesMemory,
willSuspend
} from '../internal'
import { isDefined } from '../client'
import { $context } from '../internal/shared'
import { FetchContextType } from '../types'
import { FetchConfigSync } from './server'

export async function FetchConfigAsync(props: FetchContextType) {
const { children, defaults = {}, value = {}, suspense = [] } = props
const { children, defaults = {}, value = {} } = props

let $values = new Map()

const previousConfig = $context.value as any
const { cacheProvider = defaultCache } = previousConfig

for (let valueKey in value) {
const resolvedKey = serialize({
idString: serialize(valueKey)
})

const $value = await value[valueKey]

const $data = $value.data ?? $value

valuesMemory.set(resolvedKey, $data)
$values.set(valueKey, $data)

fetcherDefaults.set(resolvedKey, $data)
// fetcherDefaults.set(resolvedKey, $data)

cacheProvider.set(resolvedKey, $data)
// cacheProvider.set(resolvedKey, $data)
}

for (let defaultKey in defaults) {
const { id = defaultKey } = defaults[defaultKey]
const resolvedKey = serialize({
idString: serialize(id)
})

if (isDefined(id)) {
valuesMemory.set(resolvedKey, await defaults[defaultKey]?.value)
fetcherDefaults.set(resolvedKey, await defaults[defaultKey]?.value)
cacheProvider.set(resolvedKey, await defaults[defaultKey]?.value)
$values.set(defaultKey, await defaults[defaultKey]?.value)
// fetcherDefaults.set(resolvedKey, await defaults[defaultKey]?.value)
// cacheProvider.set(resolvedKey, await defaults[defaultKey]?.value)
}
}

for (let suspenseKey of suspense) {
const key = serialize({
idString: serialize(suspenseKey)
})
willSuspend.set(key, true)
}

let mergedConfig = {
...previousConfig,
...props,
headers: {
...previousConfig.headers,
...props.headers
},
value: Object.fromEntries($values.entries()),
children: undefined
}

$context.value = mergedConfig

return children
return <FetchConfigSync value={mergedConfig}>{children}</FetchConfigSync>
}
34 changes: 20 additions & 14 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

import { FetchContextType } from '../types'

import { isDefined, serialize } from '../utils/shared'
import { isDefined, serialize, windowExists } from '../utils/shared'

let isServer: boolean = true
/**
Expand All @@ -39,18 +39,16 @@ export function SSRSuspense({
}

export function FetchConfig(props: FetchContextType) {
const {
children,
defaults = {},
value = {},
suspense = [],
clientOnly
} = props
const { children, defaults = {}, value: $val = {}, suspense = [] } = props

const value = (windowExists ? $val : $val.value) ?? {}

const previousConfig = useHRFContext()

const { cacheProvider = defaultCache } = previousConfig

const $values = new Map()

for (let valueKey in value) {
const resolvedKey = serialize({
idString: serialize(valueKey)
Expand All @@ -77,11 +75,15 @@ export function FetchConfig(props: FetchContextType) {
parsedChunk = dataChunk
}

valuesMemory.set(resolvedKey, parsedChunk)
$values.set(resolvedKey, parsedChunk)

if (windowExists) {
valuesMemory.set(resolvedKey, parsedChunk)

fetcherDefaults.set(resolvedKey, parsedChunk)
fetcherDefaults.set(resolvedKey, parsedChunk)

cacheProvider.set(resolvedKey, parsedChunk)
cacheProvider.set(resolvedKey, parsedChunk)
}
}

for (let defaultKey in defaults) {
Expand All @@ -91,9 +93,12 @@ export function FetchConfig(props: FetchContextType) {
})

if (isDefined(id)) {
valuesMemory.set(resolvedKey, defaults[defaultKey]?.value)
fetcherDefaults.set(resolvedKey, defaults[defaultKey]?.value)
cacheProvider.set(resolvedKey, defaults[defaultKey]?.value)
$values.set(resolvedKey, defaults[defaultKey]?.value)
if (windowExists) {
valuesMemory.set(resolvedKey, defaults[defaultKey]?.value)
fetcherDefaults.set(resolvedKey, defaults[defaultKey]?.value)
cacheProvider.set(resolvedKey, defaults[defaultKey]?.value)
}
}
}

Expand All @@ -111,6 +116,7 @@ export function FetchConfig(props: FetchContextType) {
...previousConfig.headers,
...props.headers
},
value: Object.fromEntries($values.entries()),
children: undefined
}

Expand Down
10 changes: 5 additions & 5 deletions src/components/server/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'
"use client"

import { FetchConfigAsync } from '../FetchConfigAsync'
import { FetchConfigAsync } from "../FetchConfigAsync"

import { FetchConfig as FConfig, SSRSuspense } from '../index'
import { FetchConfig as FConfig, SSRSuspense } from "../index"

const FetchConfig = typeof window === 'undefined' ? FetchConfigAsync : FConfig
const FetchConfig = typeof window === "undefined" ? FetchConfigAsync : FConfig

export { FetchConfig, SSRSuspense }
export { FetchConfig, SSRSuspense, FConfig as FetchConfigSync }
Loading
Loading