-
Notifications
You must be signed in to change notification settings - Fork 143
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
Hook to get the server context #737
Changes from 4 commits
eae5d77
2e37984
980a147
b6c8d53
d9aa427
4c33040
9014a0e
4ccae9c
3944f41
f258e9f
6e2e7bb
94fbc55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
/* | ||
* Copyright (c) 2021, salesforce.com, inc. | ||
* Copyright (c) 2022, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import React from 'react' | ||
export default React.createContext() | ||
|
||
export const DeviceContext = React.createContext() | ||
export const ServerContext = React.createContext() | ||
export const IsPrePassContext = React.createContext(false) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2022, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import {useContext} from 'react' | ||
import {IsPrePassContext, ServerContext} from './contexts' | ||
|
||
/** | ||
* Server context | ||
* @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) } | ||
*/ | ||
export const useServerContext = () => { | ||
const serverContext = useContext(ServerContext) | ||
const isPrePass = useIsPrePass() | ||
|
||
return { | ||
...serverContext, | ||
isServerSide: Boolean(serverContext.req) && !isPrePass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another interesting thing to comment on, is that this hook kind of exposes the notion that we are doing or able to do a prepass on the server. Since the pre-pass logic for the with react query hoc is contained into one file, its probably not ideal to expose that information here. That being said, we are also relying on the context being undefined in the true render to have that conditional logic. Have you maybe thought about using the For example you could potentially do something like this:
Then in your hook you can access that value and run the callback if you plan on reverting how things currently work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't thought of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: after further discussion, we decided to not use |
||
} | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
export const useIsPrePass = () => useContext(IsPrePassContext) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you are doing here. But I think this condition is better suited to your old callback implementation. For example, I read this as "isServerSide is true, if I'm on the server, but I'm not prepassing on the server". If this same condition was used with the callback method, then it would read something like "The callback is called only on the server but not during prepass".. It sounds a little easier to understand.