-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: improve preload and suspense integration #2658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
64ecddc
refactor: improve preload and suspense integrate
promer94 ad85d4e
fix: preload return type
promer94 59e1093
examples: add suspense error retry example
promer94 9704af7
test: add suspense retry e2e test
promer94 9f5b60f
test: add mutate retry e2e test
promer94 2a5491f
chore: fix lint error
promer94 17785cd
test: remove profiler since `next build --profile` does not work
promer94 202de85
refactor: use promise directly
promer94 d69091a
refactor
promer94 8b27d89
refactor: use for of since the build target is es2018 now
promer94 3b42c6f
chore: fix build error
promer94 80b6856
chore: remove comment
promer94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,14 @@ | ||
import { Suspense } from 'react' | ||
import dynamic from 'next/dynamic' | ||
|
||
const RemoteData = dynamic(() => import('./remote-data'), { | ||
ssr: false | ||
}) | ||
|
||
export default function HomePage() { | ||
return ( | ||
<Suspense fallback={<div>loading component</div>}> | ||
<RemoteData></RemoteData> | ||
</Suspense> | ||
) | ||
} |
This file contains hidden or 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,44 @@ | ||
'use client' | ||
import { Suspense, useState } from 'react' | ||
import useSWR from 'swr' | ||
import { preload } from 'swr' | ||
|
||
const fetcher = ([key, delay]: [key: string, delay: number]) => | ||
new Promise<string>(r => { | ||
setTimeout(r, delay, key) | ||
}) | ||
|
||
const key = ['suspense-after-preload', 300] as const | ||
const useRemoteData = () => | ||
useSWR(key, fetcher, { | ||
suspense: true | ||
}) | ||
|
||
const Demo = () => { | ||
const { data } = useRemoteData() | ||
return <div>{data}</div> | ||
} | ||
|
||
function Comp() { | ||
const [show, toggle] = useState(false) | ||
|
||
return ( | ||
<div className="App"> | ||
<button | ||
onClick={async () => { | ||
preload(key, fetcher) | ||
toggle(!show) | ||
}} | ||
> | ||
preload | ||
</button> | ||
{show ? ( | ||
<Suspense fallback={<div>loading</div>}> | ||
<Demo /> | ||
</Suspense> | ||
) : null} | ||
</div> | ||
) | ||
} | ||
|
||
export default Comp |
This file contains hidden or 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,43 @@ | ||
'use client' | ||
import { Suspense } from 'react' | ||
import { ErrorBoundary } from 'react-error-boundary' | ||
import { useRemoteData, preloadRemote } from './use-remote-data' | ||
|
||
const Demo = () => { | ||
const { data } = useRemoteData() | ||
return <div>data: {data}</div> | ||
} | ||
|
||
function Fallback({ resetErrorBoundary }: any) { | ||
return ( | ||
<div role="alert"> | ||
<p>Something went wrong</p> | ||
<button | ||
onClick={() => { | ||
resetErrorBoundary() | ||
}} | ||
> | ||
retry | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
function RemoteData() { | ||
return ( | ||
<div className="App"> | ||
<ErrorBoundary | ||
FallbackComponent={Fallback} | ||
onReset={() => { | ||
preloadRemote() | ||
}} | ||
> | ||
<Suspense fallback={<div>loading</div>}> | ||
<Demo /> | ||
</Suspense> | ||
</ErrorBoundary> | ||
</div> | ||
) | ||
} | ||
|
||
export default RemoteData |
This file contains hidden or 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,14 @@ | ||
import { Suspense } from 'react' | ||
import dynamic from 'next/dynamic' | ||
|
||
const RemoteData = dynamic(() => import('./manual-retry'), { | ||
ssr: false | ||
}) | ||
|
||
export default function HomePage() { | ||
return ( | ||
<Suspense fallback={<div>loading component</div>}> | ||
<RemoteData></RemoteData> | ||
</Suspense> | ||
) | ||
} |
This file contains hidden or 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,21 @@ | ||
'use client' | ||
import useSWR from 'swr' | ||
import { preload } from 'swr' | ||
|
||
let count = 0 | ||
const fetcher = () => { | ||
count++ | ||
if (count === 1) return Promise.reject('wrong') | ||
return fetch('/api/retry') | ||
.then(r => r.json()) | ||
.then(r => r.name) | ||
} | ||
|
||
const key = 'manual-retry-18-3' | ||
|
||
export const useRemoteData = () => | ||
useSWR(key, fetcher, { | ||
suspense: true | ||
}) | ||
|
||
export const preloadRemote = () => preload(key, fetcher) |
This file contains hidden or 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,54 @@ | ||
import { Suspense } from 'react' | ||
import { ErrorBoundary } from 'react-error-boundary' | ||
import useSWR from 'swr' | ||
import { mutate } from 'swr' | ||
|
||
let count = 0 | ||
export const fetcher = () => { | ||
count++ | ||
if (count === 1) return Promise.reject('wrong') | ||
return fetch('/api/retry') | ||
.then(r => r.json()) | ||
.then(r => r.name) | ||
} | ||
|
||
const key = 'manual-retry-mutate' | ||
|
||
export const useRemoteData = () => | ||
useSWR(key, fetcher, { | ||
suspense: true | ||
}) | ||
const Demo = () => { | ||
const { data } = useRemoteData() | ||
return <div>data: {data}</div> | ||
} | ||
|
||
function Fallback({ resetErrorBoundary }: any) { | ||
return ( | ||
<div role="alert"> | ||
<p>Something went wrong</p> | ||
<button | ||
onClick={async () => { | ||
await mutate(key, fetcher) | ||
resetErrorBoundary() | ||
}} | ||
> | ||
retry | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
function RemoteData() { | ||
return ( | ||
<div className="App"> | ||
<ErrorBoundary FallbackComponent={Fallback}> | ||
<Suspense fallback={<div>loading</div>}> | ||
<Demo /> | ||
</Suspense> | ||
</ErrorBoundary> | ||
</div> | ||
) | ||
} | ||
|
||
export default RemoteData |
This file contains hidden or 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,42 @@ | ||
import { Suspense } from 'react' | ||
import { ErrorBoundary } from 'react-error-boundary' | ||
import { useRemoteData, preloadRemote } from './use-remote-data' | ||
|
||
const Demo = () => { | ||
const { data } = useRemoteData() | ||
return <div>data: {data}</div> | ||
} | ||
|
||
function Fallback({ resetErrorBoundary }: any) { | ||
return ( | ||
<div role="alert"> | ||
<p>Something went wrong</p> | ||
<button | ||
onClick={() => { | ||
resetErrorBoundary() | ||
}} | ||
> | ||
retry | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
function RemoteData() { | ||
return ( | ||
<div className="App"> | ||
<ErrorBoundary | ||
FallbackComponent={Fallback} | ||
onReset={() => { | ||
preloadRemote() | ||
}} | ||
> | ||
<Suspense fallback={<div>loading</div>}> | ||
<Demo /> | ||
</Suspense> | ||
</ErrorBoundary> | ||
</div> | ||
) | ||
} | ||
|
||
export default RemoteData |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.