-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
Automatic Static Optimization causes hydration warnings for pages with query string parameters #2395
Comments
Imho the described behaviour isn't a bug, but intended and expected. Nextjs explicitly states "It [query] will be an empty object during prerendering if the page doesn't have data fetching requirements." (See https://nextjs.org/docs/api-reference/next/router). If there is a bug, it's that Which opens the question why the edit: I totally did miss the point as the "edge-case" is the intended usage scenario (see blitz-js/legacy-framework#433) as it allows to parse something like Coming from a nextjs-background it seems broken to me and I personally don't think this edge-case should be supported at all. |
I get what you're saying, but Blitz.js isn't Next.js and the goals of the projects aren't necessarily the same. Blitz is, at least partially, defined by what it builds on top of the foundation laid by Next. In particular, the promises of No-API and eliminating the friction between the client-side and server-side portions of your application. Those promises are broken if something as pedestrian as a query string parameter can shatter the illusion that Blitz is smart enough to determine whether your code needs to run client-side or server-side. But I don't see any reason why it should stay that way. At compile-time, Blitz can detect if a page is importing |
To be honest, I don't think thats a good idea. It would introduce "magic" instead of "convention over configuration". One would also need to parse the request-object somewhere to extract the querystring. If you already have Besides implementation-problems, I have to say that I don't see the benefit of the (arbitrary & synthetical) distinction between "path-based" & "url-based" query-params. So why all the hassle and the additional code? Just for hiding the fact, that internally "path-based" query-params are already simple query-params inside next/router? |
I agree with @benbender that we shouldn't automatically opt into SSR. That's a big change to I feel devs should make themselves. As for the hydration warning, the proper fix is to initialize
This is a fair point. Personally I don't have a strong feeling either way. Feel free to post an RFC in our GH discussions to revert to same behavior as Next.js to see if others agree. |
@MrLeebo we still have the hydration warning issue, right? |
Yes. It's always going to be a facet of automatic static optimization. I can see why nextjs couldn't do it, but blitz can assume users will be running a web server (can't have queries and mutations otherwise) so automatically switching doesn't seem as drastic a move as it would be for nextjs |
Unless I'm missing something, having a web server or not has zero affect on React hydration on the client. We should never cause potential hydration issues as it could cause unexpected bugs. So we need to fix this. |
We removed useRouterQuery |
Simple Repro: https://codesandbox.io/s/misty-lake-dwvg7
What is the problem?
If a page invokes the
useRouterQuery()
hook, it will generate a hydration warning when you visit the page. This is because Automatic Static Optimization is performed on all pages in Blitz by default, unless the page performs Server Side Rendering.Say we have the query string
?name=John+Doe
and we render<h1>{name}</h1>
in some JSX.- Warning: Text content did not match. Server: "" Client: "John Doe"
Most of the time, something like this is not a big deal because it's just a warning and React already makes a correction. But these mismatches, if left untreated, can introduce more dangerous bugs because, though React attempts to correct mismatches, React may not always make the right call if a query param is part of a conditional expression that renders lots of "similar but not identical" DOM elements between the server and client versions. A mismatch that isn't fixed correctly can lead to a DOM Exception at reconciliation time, and at that point your React app is basically dead in the water until the user manually reloads the page.
Is it possible for Blitz to detect that the page has
useRouterQuery()
imported and automatically switch to server-side rendering? Seems like that would eliminate this entire category of potential bugs.Workarounds
For simple text content mismatches, you can add the
suppressHydrationWarning
prop:And if your query param is part of some page logic or a more complex render expression, you can add a
getServerSideProps
function to disable the static optimization entirely:Paste all your error logs here:
- Warning: Text content did not match. Server: "" Client: "John Doe"
Paste all relevant code snippets here:
What are detailed steps to reproduce this?
Run
blitz -v
and paste the output here:From the Blitz.js CodeSandbox Template:
Please include below any other applicable logs and screenshots that show your problem:
The text was updated successfully, but these errors were encountered: