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

Fix useServerContext returning isServerSide=false when on server. #782

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ jest.mock('../universal/routes', () => {
}

const GetServerContext = () => {
const {res, isServerSide} = useServerContext()
if (isServerSide) {
const {res} = useServerContext()
if (res) {
console.log('--- isServerSide')
res.status(404)
}
Expand Down Expand Up @@ -716,9 +716,8 @@ describe('The Node SSR Environment', () => {
assertions: (res) => {
expect(res.statusCode).toBe(404)

// Expect the console.log to be called only once, even though the component
// is going to be rendered twice on the server (on prepass and then 2nd pass)
expect(console.log).toHaveBeenCalledTimes(1)
// Because of the prepass step we'll expect that this method is called twice.
expect(console.log).toHaveBeenCalledTimes(2)
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ export const withReactQuery = (Wrapped, options = {}) => {
const queryClient = (res.locals.__queryClient =
res.locals.__queryClient || new QueryClient(queryClientConfig))

// Without the request object, our useServerContext hook would be able tell whether on prepass
const withoutReq = React.cloneElement(appJSX, {
req: undefined
})
await ssrPrepass(withoutReq)
// Use `ssrPrepass` to collect all uses of `useQuery`.
await ssrPrepass(appJSX)

const queryCache = queryClient.getQueryCache()
const queries = queryCache.getAll().filter((q) => q.options.enabled !== false)
Expand Down
8 changes: 3 additions & 5 deletions packages/pwa-kit-react-sdk/src/ssr/universal/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,20 @@ export const useCorrelationId = () => {
* @typedef {Object} ServerContext
* @property {Object} req - Request object
* @property {Object} res - Response object
* @property {boolean} isServerSide
*/

/**
* Get the server context
* @returns {ServerContext} ServerContext object
*
* @example
* const {res, isServerSide} = useServerContext()
* if (isServerSide && query.error) { res.status(404) }
* const {res} = useServerContext()
* if (res && query.error) { res.status(404) }
*/
export const useServerContext = () => {
const serverContext = useContext(ServerContext)

return {
...serverContext,
isServerSide: Boolean(serverContext.req)
...serverContext
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: now that we're no longer extending the object, we can simply return the context directly like...

return useContext(ServerContext)

}
}