Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reproduce issue vercel/next.js#44083
- Loading branch information
Showing
11 changed files
with
138 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import styles from "@/styles/Home.module.css"; | ||
|
||
import Image from "next/image"; | ||
import { atom, useRecoilValue } from "recoil"; | ||
|
||
const componentAtom = atom<void>({ | ||
key: "component", | ||
default: new Promise((resolve) => setTimeout(resolve, 5000)), // Example data fetching or async operation | ||
}); | ||
|
||
export default function Component() { | ||
// This will suspend the component | ||
// Recoil is being used just to demonstrate the issue happen when this component being suspended | ||
// If you use React Developer Tools to suspend this component manually, the issue will still happen | ||
useRecoilValue(componentAtom); | ||
return ( | ||
<div className={styles.center}> | ||
<Image | ||
className={styles.logo} | ||
src="/next.svg" | ||
alt="Next.js Logo" | ||
width={180} | ||
height={37} | ||
priority | ||
/> | ||
<div className={styles.thirteen}> | ||
<Image src="/thirteen.svg" alt="13" width={40} height={31} priority /> | ||
</div> | ||
</div> | ||
); | ||
} |
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,9 @@ | ||
import styles from "@/styles/Home.module.css"; | ||
|
||
export function Fallback() { | ||
return ( | ||
<div className={`${styles.center} ${styles.description}`}> | ||
<p>Loading…</p> | ||
</div> | ||
); | ||
} |
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,22 @@ | ||
import dynamic from "next/dynamic"; | ||
import { Suspense, useContext } from "react"; | ||
|
||
import { Context } from "../provider"; | ||
import { Fallback } from "./fallback"; | ||
|
||
const Component = dynamic(() => import("./component")); | ||
|
||
// Example component that depends on Context (see ../provider/context.ts) | ||
export function Example() { | ||
const context = useContext(Context); | ||
if (!context) return <Fallback />; // Render fallback when context is not available (dynamic import/async data fetching is still in progress) | ||
// [ISSUE] Everything works fine until this line | ||
// <Component /> (see ./component.tsx) will suspend for 5 seconds (e.g. fetch data, etc.) | ||
// Within those 5 seconds, <Fallback /> SHOULD be rendered, but IT IS NOT, nothing is rendered | ||
// Only after 5 seconds, <Component /> is render | ||
return ( | ||
<Suspense fallback={<Fallback />}> | ||
<Component /> | ||
</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,16 @@ | ||
import { PropsWithChildren, useEffect, useState } from "react"; | ||
import { RecoilRoot } from "recoil"; | ||
|
||
import { Context } from "./context"; | ||
|
||
export default function Component({ children }: PropsWithChildren) { | ||
// Provide example context after being mounted (client-side only). In real-world, some dynamic/asynchronous data fetching would be done here; | ||
const [context, setContext] = useState<object>(); | ||
useEffect(() => setContext({}), [setContext]); | ||
return ( | ||
<Context.Provider value={context}> | ||
{/* Example heavy dependencies */} | ||
<RecoilRoot>{children}</RecoilRoot> | ||
</Context.Provider> | ||
); | ||
} |
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,4 @@ | ||
import { createContext } from "react"; | ||
|
||
// Example context whose value will be provided dynamically/asynchronously in by Provider component (see ./index.tsx) | ||
export const Context = createContext<object | undefined>(undefined); |
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,15 @@ | ||
import dynamic from "next/dynamic"; | ||
import { Fragment, PropsWithChildren, Suspense } from "react"; | ||
|
||
// Dynamically import the actual Provider component because it contains some heavy dependencies | ||
const Component = dynamic(() => import("./component")); | ||
|
||
export * from "./context"; | ||
|
||
export function Provider(props: PropsWithChildren) { | ||
return ( | ||
<Suspense fallback={<Fragment {...props} />}> | ||
<Component {...props} /> | ||
</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
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
import '@/styles/globals.css' | ||
import type { AppProps } from 'next/app' | ||
import "@/styles/globals.css"; | ||
|
||
import type { AppProps } from "next/app"; | ||
|
||
import { Provider } from "@/components/provider"; | ||
|
||
export default function App({ Component, pageProps }: AppProps) { | ||
return <Component {...pageProps} /> | ||
return ( | ||
<Provider> | ||
<Component {...pageProps} /> | ||
</Provider> | ||
); | ||
} |
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
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