-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve preload and suspense integration (#2658)
- Loading branch information
Showing
34 changed files
with
540 additions
and
90 deletions.
There are no files selected for viewing
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,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 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 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 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 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 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 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.