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

Add serverSidePostQuery hook to access query data + NextContext during SSR #74

Merged
merged 2 commits into from
Sep 7, 2022
Merged

Add serverSidePostQuery hook to access query data + NextContext during SSR #74

merged 2 commits into from
Sep 7, 2022

Conversation

FINDarkside
Copy link
Contributor

@FINDarkside FINDarkside commented Aug 26, 2022

Adds serverSidePostQuery function option to relay-nextjs which allows to access returned query data and NextContext after the query has finished during SSR. This allows to handle stuff like 404 status codes for non-existing pages. Related: #15

Example usage:

serverSidePostQuery: (queryResponse: any, ctx) => {
  if (!queryResponse?.data?.company && ctx.res) {
    ctx.res.statusCode = 404;
  }
},

Any feedback welcome.

@FINDarkside FINDarkside changed the title Add serverSidePostQuery hook Add serverSidePostQuery hook to access query data + NextContext during SSR Aug 26, 2022
Copy link
Member

@rrdelaney rrdelaney left a comment

Choose a reason for hiding this comment

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

This is awesome, thank you so much for the PR! I'm going to be out a lot of this week, so will get to it when I can.

@@ -175,6 +183,11 @@ async function getServerInitialProps<Props extends WiredProps, ServerSideProps>(

await ensureQueryFlushed(preloadedQuery);

if (serverSidePostQuery) {
const queryResult = await preloadedQuery.source?.toPromise();
Copy link
Member

Choose a reason for hiding this comment

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

Will this cause a double network request? I'm not sure if the observables returned from Relay are hot or cold.

Copy link
Contributor Author

@FINDarkside FINDarkside Aug 30, 2022

Choose a reason for hiding this comment

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

No, call to preloadedQuery.source?.toPromise() here results in a already resolved promise. If you have better name suggestion for serverSidePostQuery let me know 😄

Copy link
Member

@rrdelaney rrdelaney left a comment

Choose a reason for hiding this comment

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

Can you add a blurb about this to the documentation in /website? Other than that everything looks good to me!

@FINDarkside
Copy link
Contributor Author

Added it to docs!

Copy link
Member

@rrdelaney rrdelaney left a comment

Choose a reason for hiding this comment

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

Thanks!!

@rrdelaney rrdelaney merged commit 1b3bdde into RevereCRE:main Sep 7, 2022
@rrdelaney
Copy link
Member

This is now published in v0.10.0 🥳

@FINDarkside FINDarkside deleted the feature/post-query branch September 7, 2022 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants