-
Notifications
You must be signed in to change notification settings - Fork 887
fix: stop metadata queries from short-circuiting #10312
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
Conversation
@@ -6,7 +6,8 @@ import { getMetadataAsJSON } from "utils/metadata"; | |||
// so you can just set this to true. | |||
export const experimentalTheme = | |||
typeof document !== "undefined" && | |||
getMetadataAsJSON("experiments")?.includes("dashboard_theme"); | |||
(getMetadataAsJSON<string[]>("experiments")?.includes("dashboard_theme") ?? | |||
false); |
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.
Ugly, but had to do this to make sure that the code is always type boolean
and not type any
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.
yeah I hate this lol. but we can fix it later.
@@ -6,7 +6,8 @@ import { getMetadataAsJSON } from "utils/metadata"; | |||
// so you can just set this to true. | |||
export const experimentalTheme = | |||
typeof document !== "undefined" && | |||
getMetadataAsJSON("experiments")?.includes("dashboard_theme"); | |||
(getMetadataAsJSON<string[]>("experiments")?.includes("dashboard_theme") ?? | |||
false); |
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.
yeah I hate this lol. but we can fix it later.
Doesn't close any open issues, but does address some problems noticed from the company Slack yesterday.
Changes made
useQueryOptions
factory functions that reference embedded HTML metadata, and makes sure that they can't short-circuit with stale data.getMetadataAsJSON
Other notes
Going to be copy-pasting the thread from the company Slack, just for documentation:
The only way for a server request to get made is if the metadata tags in the
index.html
file aren't set when the server delivers the initial response. Otherwise, thegetMetadataAsJSON
call will always short-circuit the expression with local dataAnother potential bug I'm worried about is background refetches:
updateAppearance
gets called, and theonSuccess
callback mutates the query client cache with a value different from what was provided via the HTML's meta tagI'm wondering if it might better to leverage the
initialData
property to use the metadata tag only while the initial query is loading, but then when we get something back from the server, the initial data will be ignored for all other renders. initialData is never put in the query cache itself, and the only time it could pop up again is if the query cache for that key gets completely vacated.The
getMetadataAsJSON
function would need some more massaging to make things have the right types, but I was thinking something like this:So, uh, famous last words – this is not simple
Basically, React Query has two properties that it lets you define on the query object:
placeholderData
– This is the property I was actually thinking of. It acts as initial data for the query, but it's never stored in the cacheinitialData
– This works like placeholderData, but it is stored in the cache. By default, this data is always stale, so the query function still runs, even on the first renderBoth of these can be actual data or functions that lazy-evaluate to data.
The problem is that the
UseQueryOptions
type has four type parameter slots:TQueryFnData
– The data we get back from the query function. Defaults tounknown
TError
– The error that gets thrown if a query function fails. Defaults tounknown
TData
– Used to define the type of initialData (if it's different fromTQueryFnData
). Defaults toTQueryFnData
TQueryKey
– The type of the query key. Defaults to an unknown arrayinitialData
has flexibility – it's defined viaTData
, and it can default toTQueryFnData
if it's not specified.placeholderData
doesn't get that privilege, though. It always has to be of typeTQueryFnData
. And becausegetMetadataAsJSON
can also returnundefined
, that extra possibility has a risk of "polluting" all the other types.placeholderData
, then the our useQuery object has to be updated to returnData | undefined
, meaning the query object's data property will always have anundefined
case, even if the query has succeededinitialData
, we have the ability to split hairs and say that the query function returnsData
, while the initial data isData | undefined
. But because useQuery's data property isTQueryFnData | TData
, we're still polluting the query object