Skip to content

Commit

Permalink
feat: add
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeUni committed Apr 5, 2023
1 parent b92f683 commit 659bd74
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 67 deletions.
4 changes: 2 additions & 2 deletions fixtures/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"concurrently": "^5.3.0",
"express": "^4.17.1",
"nodemon": "^2.0.6",
"react": "^18.0.0-beta-24dd07bd2-20211208",
"react-dom": "^18.0.0-beta-24dd07bd2-20211208",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-error-boundary": "^3.1.3",
"react-fetch": "0.0.0-experimental-44cdfd6b7",
"resolve": "1.12.0",
Expand Down
28 changes: 15 additions & 13 deletions fixtures/ssr/server/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ module.exports = function render(url, res) {
})
let didError = false
const data = createServerData()
const method = createSuspenseFetch()
// const method = createSuspenseFetch()
let str = 'window.xxx = 11'
const { pipe, abort } = renderToPipeableStream(
<DataProvider data={data}>
<SuspenseFetchProvider method={method}>
<App assets={assets} />
</SuspenseFetchProvider>
{/* <SuspenseFetchProvider method={method}> */}
<App assets={assets} />
{/* </SuspenseFetchProvider> */}
</DataProvider>,
{
onCompleteShell() {
onShellReady() {
// If something errored before we started streaming, we set the error code appropriately.
res.statusCode = didError ? 500 : 200
res.setHeader('Content-type', 'text/html')
Expand All @@ -61,14 +62,14 @@ module.exports = function render(url, res) {
didError = true
console.error(x)
},
onCompleteAll() {
const cache = method.getCache()
const str = renderScriptHtml(cache)
res.write(str)
console.log(str)
console.log('------- complete ---------')
},
bootstrapScriptContent: 'xxx'
onAllReady() {
// const cache = method.getCache()
// str = renderScriptHtml(cache)
// console.log(str)
// console.log('------- complete ---------')
}
// bootstrapScriptContent: str
// bootstrapScripts: ['1.js']
}
)
// Abandon and switch to client rendering if enough time passes.
Expand All @@ -94,6 +95,7 @@ function createServerData() {
setTimeout(() => {
done = true
promise = null
console.log('请求 in server------')
resolve()
}, API_DELAY)
})
Expand Down
4 changes: 2 additions & 2 deletions fixtures/ssr/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ function Content() {
<Comments />
</Suspense>
</section>
<section className="comments">
{/* <section className="comments">
<h2>Comments for suspense fetch</h2>
<Suspense fallback={<Spinner />}>
<Comments2 subreddit="react" />
</Suspense>
</section>
</section> */}
{/* <section className="comments">
<h2>Comments for react fetch</h2>
<Suspense fallback={<Spinner />}>
Expand Down
1 change: 1 addition & 0 deletions fixtures/ssr/src/Comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useData } from './data'

export default function Comments() {
const comments = useData()
console.log('in server---', comments)
return (
<>
{comments.map((comment, i) => (
Expand Down
2 changes: 1 addition & 1 deletion fixtures/ssr/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import { hydrateRoot } from 'react-dom'
import { hydrateRoot } from 'react-dom/client'
import App from './App'
import { SuspenseFetchProvider, getServerInitialData } from 'use-suspense-fetch'

Expand Down
29 changes: 13 additions & 16 deletions fixtures/ssr/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4098,14 +4098,13 @@ rc@^1.2.8:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

react-dom@^18.0.0-beta-24dd07bd2-20211208:
version "18.0.0-beta-24dd07bd2-20211208"
resolved "https://registry.npmmirror.com/react-dom/download/react-dom-18.0.0-beta-24dd07bd2-20211208.tgz#d2fbcc9a080f9983945b4e53db33bba7f7a560f6"
integrity sha512-B6pxB3+lI+Tct6WKs+Db5JKSfQUqcfAXz706EMdRlx4qs+PcMpftgicRNO7j6F6zqMMn8bqV1tjHQaqw00UcGg==
react-dom@^18.1.0:
version "18.1.0"
resolved "https://registry.npmmirror.com/react-dom/-/react-dom-18.1.0.tgz#7f6dd84b706408adde05e1df575b3a024d7e8a2f"
integrity sha512-fU1Txz7Budmvamp7bshe4Zi32d0ll7ect+ccxNu9FlObT605GOEB8BfO4tmRJ39R5Zj831VCpvQ05QPBW5yb+w==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
scheduler "0.21.0-beta-24dd07bd2-20211208"
scheduler "^0.22.0"

react-error-boundary@^3.1.3:
version "3.1.3"
Expand All @@ -4119,13 +4118,12 @@ [email protected]:
resolved "https://registry.nlark.com/react-fetch/download/react-fetch-0.0.0-experimental-44cdfd6b7.tgz#c2d6a3a43842c7125587843ff35fd0c2fbf156fc"
integrity sha1-wtajpDhCxxJVh4Q/81/QwvvxVvw=

react@^18.0.0-beta-24dd07bd2-20211208:
version "18.0.0-beta-24dd07bd2-20211208"
resolved "https://registry.npmmirror.com/react/download/react-18.0.0-beta-24dd07bd2-20211208.tgz#6bfe825f1253390136f1723c03dfea5bae5c5eec"
integrity sha512-XyDoCS4UisFJGfiBDnVeOUbJoyAyxwM/YET8hExf9gFLMJX2bRR+v2OvO7k5jhoUamEQ9xCuvoqs9Kf1QfA3Jg==
react@^18.1.0:
version "18.1.0"
resolved "https://registry.npmmirror.com/react/-/react-18.1.0.tgz#6f8620382decb17fdc5cc223a115e2adbf104890"
integrity sha512-4oL8ivCz5ZEPyclFQXaNksK3adutVS8l2xzZU0cqEFrE9Sb7fC0EFK5uEk74wIreL1DERyjvsU915j1pcT2uEQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

read-pkg@^4.0.1:
version "4.0.1"
Expand Down Expand Up @@ -4383,13 +4381,12 @@ safe-regex@^1.1.0:
resolved "https://registry.nlark.com/safer-buffer/download/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha1-RPoWGwGHuVSd2Eu5GAL5vYOFzWo=

scheduler@0.21.0-beta-24dd07bd2-20211208:
version "0.21.0-beta-24dd07bd2-20211208"
resolved "https://registry.npmmirror.com/scheduler/download/scheduler-0.21.0-beta-24dd07bd2-20211208.tgz#c2564d3f3b6cef16a58f340435f32f70a03998ed"
integrity sha512-h2+U1Cg6w2k3mrURC2769U3L116ZaeMSVRYJdm6R4n99z1XJq5Hus7+JeM5fO0djBKPO3kykilTw/16tp+cOjA==
scheduler@^0.22.0:
version "0.22.0"
resolved "https://registry.npmmirror.com/scheduler/-/scheduler-0.22.0.tgz#83a5d63594edf074add9a7198b1bae76c3db01b8"
integrity sha512-6QAm1BgQI88NPYymgGQLCZgvep4FyePDWFpXVK+zNSUgHwlqpJy8VEh8Et0KxTACS4VWwMousBElAZOH9nkkoQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

schema-utils@^1.0.0:
version "1.0.0"
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-suspense-fetch",
"version": "1.5.0",
"version": "1.6.0",
"main": "lib/index.js",
"repository": "https://github.com/snakeUni/use-suspense-fetch.git",
"author": "fox <[email protected]>",
Expand All @@ -17,11 +17,11 @@
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/lru-cache": "^5.1.0",
"@types/react": "^17.0.11",
"@types/react": "^18.0.9",
"@types/serialize-javascript": "^5.0.0",
"jest": "^27.0.6",
"react": "^18.0.0-beta-24dd07bd2-20211208",
"react-dom": "^18.0.0-beta-24dd07bd2-20211208",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"ts-jest": "^27.0.3",
"typescript": "^4.3.2",
"yalc": "^1.0.0-pre.53"
Expand Down
8 changes: 7 additions & 1 deletion src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ export function SuspenseFetchProvider<Response = any>({
return <context.Provider value={innerMethod}>{children}</context.Provider>
}

const suspenseFetch = createSuspenseFetch(0, {}, true)

/**
* 如果使用 Provider 则支持自定义的缓存,否则使用全局缓存
* @returns
*/
export function useSuspenseFetch<Response = any>(): ReturnMethod<Response> {
const value = useContext(context)
const value = useContext(context) || suspenseFetch

return value as any
}
Expand Down
8 changes: 5 additions & 3 deletions src/suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ interface FetchOptions {
export default function suspenseFetch<Response = any>(
key: string,
fn: PromiseFn<Response>,
option: FetchOptions = { }
option: FetchOptions = {}
): Response {
return handleSuspenseFetch({
promiseFn: fn,
Expand Down Expand Up @@ -149,14 +149,16 @@ function clearInner<Response>(cache: Cache<Response>, key?: string) {
* 创建自己的 LRU 缓存,支持配置选项
* @param lifeSpan 过期时间
* @param option 配置选项
* @param useGlobalCache 是否使用全局缓存,使用全局缓存的时候,option 将会失效
* @returns
*/
export function createSuspenseFetch<Response = any>(
lifeSpan = 0,
option: Options<any, any> = {}
option: Options<any, any> = {},
useGlobalCache = false
): ReturnMethod<Response> {
const innerOption = { ...defaultOption, ...option }
const cache = new LRU(innerOption)
const cache = useGlobalCache ? globalCache : new LRU(innerOption)

return {
fetch: (key: string, fn: PromiseFn<Response>) =>
Expand Down
42 changes: 17 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,10 @@
resolved "https://registry.nlark.com/@types/prop-types/download/@types/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7"
integrity sha1-KrDV2i5YFflLC51LldHl8kOrLKc=

"@types/react@^17.0.11":
version "17.0.11"
resolved "https://registry.nlark.com/@types/react/download/@types/react-17.0.11.tgz#67fcd0ddbf5a0b083a0f94e926c7d63f3b836451"
integrity sha1-Z/zQ3b9aCwg6D5TpJsfWPzuDZFE=
"@types/react@^18.0.9":
version "18.0.9"
resolved "https://registry.npmmirror.com/@types/react/-/react-18.0.9.tgz#d6712a38bd6cd83469603e7359511126f122e878"
integrity sha512-9bjbg1hJHUm4De19L1cHiW0Jvx3geel6Qczhjd0qY5VKVE2X5+x77YxAepuCwVh4vrgZJdgEJw48zrhRIeF4Nw==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
Expand Down Expand Up @@ -2117,11 +2117,6 @@ nwsapi@^2.2.0:
resolved "https://registry.npm.taobao.org/nwsapi/download/nwsapi-2.2.0.tgz#204879a9e3d068ff2a55139c2c772780681a38b7"
integrity sha1-IEh5qePQaP8qVROcLHcngGgaOLc=

object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.nlark.com/object-assign/download/object-assign-4.1.1.tgz?cache=0&sync_timestamp=1618847198142&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fobject-assign%2Fdownload%2Fobject-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=

once@^1.3.0:
version "1.4.0"
resolved "https://registry.nlark.com/once/download/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
Expand Down Expand Up @@ -2266,27 +2261,25 @@ randombytes@^2.1.0:
dependencies:
safe-buffer "^5.1.0"

react-dom@^18.0.0-beta-24dd07bd2-20211208:
version "18.0.0-beta-24dd07bd2-20211208"
resolved "https://registry.npmmirror.com/react-dom/download/react-dom-18.0.0-beta-24dd07bd2-20211208.tgz#d2fbcc9a080f9983945b4e53db33bba7f7a560f6"
integrity sha512-B6pxB3+lI+Tct6WKs+Db5JKSfQUqcfAXz706EMdRlx4qs+PcMpftgicRNO7j6F6zqMMn8bqV1tjHQaqw00UcGg==
react-dom@^18.1.0:
version "18.1.0"
resolved "https://registry.npmmirror.com/react-dom/-/react-dom-18.1.0.tgz#7f6dd84b706408adde05e1df575b3a024d7e8a2f"
integrity sha512-fU1Txz7Budmvamp7bshe4Zi32d0ll7ect+ccxNu9FlObT605GOEB8BfO4tmRJ39R5Zj831VCpvQ05QPBW5yb+w==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
scheduler "0.21.0-beta-24dd07bd2-20211208"
scheduler "^0.22.0"

react-is@^17.0.1:
version "17.0.2"
resolved "https://registry.nlark.com/react-is/download/react-is-17.0.2.tgz?cache=0&sync_timestamp=1624897053623&other_urls=https%3A%2F%2Fregistry.nlark.com%2Freact-is%2Fdownload%2Freact-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha1-5pHUqOnHiTZWVVOas3J2Kw77VPA=

react@^18.0.0-beta-24dd07bd2-20211208:
version "18.0.0-beta-24dd07bd2-20211208"
resolved "https://registry.npmmirror.com/react/download/react-18.0.0-beta-24dd07bd2-20211208.tgz#6bfe825f1253390136f1723c03dfea5bae5c5eec"
integrity sha512-XyDoCS4UisFJGfiBDnVeOUbJoyAyxwM/YET8hExf9gFLMJX2bRR+v2OvO7k5jhoUamEQ9xCuvoqs9Kf1QfA3Jg==
react@^18.1.0:
version "18.1.0"
resolved "https://registry.npmmirror.com/react/-/react-18.1.0.tgz#6f8620382decb17fdc5cc223a115e2adbf104890"
integrity sha512-4oL8ivCz5ZEPyclFQXaNksK3adutVS8l2xzZU0cqEFrE9Sb7fC0EFK5uEk74wIreL1DERyjvsU915j1pcT2uEQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

require-directory@^2.1.1:
version "2.1.1"
Expand Down Expand Up @@ -2342,13 +2335,12 @@ saxes@^5.0.1:
dependencies:
xmlchars "^2.2.0"

scheduler@0.21.0-beta-24dd07bd2-20211208:
version "0.21.0-beta-24dd07bd2-20211208"
resolved "https://registry.npmmirror.com/scheduler/download/scheduler-0.21.0-beta-24dd07bd2-20211208.tgz#c2564d3f3b6cef16a58f340435f32f70a03998ed"
integrity sha512-h2+U1Cg6w2k3mrURC2769U3L116ZaeMSVRYJdm6R4n99z1XJq5Hus7+JeM5fO0djBKPO3kykilTw/16tp+cOjA==
scheduler@^0.22.0:
version "0.22.0"
resolved "https://registry.npmmirror.com/scheduler/-/scheduler-0.22.0.tgz#83a5d63594edf074add9a7198b1bae76c3db01b8"
integrity sha512-6QAm1BgQI88NPYymgGQLCZgvep4FyePDWFpXVK+zNSUgHwlqpJy8VEh8Et0KxTACS4VWwMousBElAZOH9nkkoQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

[email protected], semver@^7.3.2:
version "7.3.5"
Expand Down

0 comments on commit 659bd74

Please sign in to comment.