Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support async getClientInitialProps w backwards compabitible typing #77

Merged
merged 1 commit into from
Nov 29, 2022
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
6 changes: 6 additions & 0 deletions example/src/pages/film/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ export default withRelay(Film, FilmQuery, {

return createServerEnvironment();
},
// clientSideProps with arbitrary sleep,
// to show async or async capability
clientSideProps: async () => {
await new Promise(resolve => setTimeout(resolve, 200));
return {}
}
});
6 changes: 3 additions & 3 deletions src/wired/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface WiredOptions<Props extends WiredProps, ServerSideProps = {}> {
/** Props passed to the component when rendering on the client. */
clientSideProps?: (
ctx: NextPageContext
) => OrRedirect<Partial<ServerSideProps>>;
) => OrRedirect<Partial<ServerSideProps>> | Promise<OrRedirect<Partial<ServerSideProps>>>;
/** Called when creating a Relay environment on the server. */
createServerEnvironment: (
ctx: NextPageContext,
Expand Down Expand Up @@ -211,14 +211,14 @@ async function getServerInitialProps<Props extends WiredProps, ServerSideProps>(
};
}

function getClientInitialProps<Props extends WiredProps, ClientSideProps>(
async function getClientInitialProps<Props extends WiredProps, ClientSideProps>(
ctx: NextPageContext,
query: GraphQLTaggedNode,
opts: WiredOptions<Props, ClientSideProps>
) {
const { variablesFromContext = defaultVariablesFromContext } = opts;
const clientSideProps = opts.clientSideProps
? opts.clientSideProps(ctx)
? await opts.clientSideProps(ctx)
: ({} as ClientSideProps);

if ('redirect' in clientSideProps) {
Expand Down