Skip to content
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

chore(next): warn for missing suspense boundary #3449

Merged
merged 1 commit into from
Dec 6, 2023
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
5 changes: 5 additions & 0 deletions .changeset/small-rings-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/next': patch
---

Show a warning when the `Suspense` boundary is missing under the UrqlProvider
19 changes: 18 additions & 1 deletion packages/next-urql/src/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export const SSRContext = React.createContext<SSRExchange | undefined>(
undefined
);

const SuspenseWarning = () => {
if (process.env.NODE_ENV !== 'production') {
console.warn(
'urql suspended but there was no boundary to catch it, add a <Suspense> component under your urql Provider.'
);
}
return null;
};

/** Provider for `@urql/next` during non-rsc interactions.
*
* @remarks
Expand Down Expand Up @@ -59,7 +68,15 @@ export function UrqlProvider({
React.createElement(
SSRContext.Provider,
{ value: ssr },
React.createElement(DataHydrationContextProvider, { nonce }, children)
React.createElement(
DataHydrationContextProvider,
{ nonce },
React.createElement(
React.Suspense,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, my only concern here is that there could be legitimate uses for this. I'm imagining that some router systems may use suspense in unexpected ways, so not sure if we can somehow make detection work without a custom boundary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That feels counter-intuitive as for the Provider to work you would need a boundary either way so even-if something higher up is a Suspense boundary that would render the urql provider pointless

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this Suspense is inserted on the server side, the Promise resolution in the React component will change to streaming instead of waiting.

I'm sorry, but SSR in urql without Suspense will be impossible.

https://next-blog.croud.jp/contents/7m4dShGLIpFkc0GKHIrW

Can you please adjust the server side so that Suspense is not interrupted?

{ fallback: React.createElement(SuspenseWarning) },
children
)
)
)
);
}