RFC: Partial Hydration #36608
-
SummaryWe’re happy to announce Partial Hydration in Gatsby 5. You gain the ability to mark specific parts of your site/application as “interactive,” while the rest of your site is static by default. See the design section for more information on how this works. Partial Hydration is one of the major features of Gatsby 5. See its release notes to learn more. Getting started
Using the Partial Hydration Starter Clone the existing starter gatsby-partial-hydration-starter cd gatsby-partial-hydration-starter
npm install --legacy-peer-deps
npm run build
npm run serve Using the Partial Hydration with an existing project You can get started by enabling the // gatsby-config.js
module.exports = {
flags: {
PARTIAL_HYDRATION: true
}
} npm install –-save-exact --legacy-peer-deps gatsby@next react@experimental react-dom@experimental Using an online IDE MotivationMost Gatsby sites are content-based and require only a few sections to be interactive. However, to ensure click events, effects, and state changes work correctly, we must download the JavaScript that handles these events. Unfortunately, this behavior can lead to a lot of unused JavaScript being downloaded, making your website slower and more expensive. We want to give you the best of both worlds with Partial Hydration. Using React’s great DX/UX, we can produce smaller JavaScript bundles and increased Lighthouse scores! Here's a short demo to show why it's so cool! Known issues (during Beta)
DesignPartial hydration is powered by “React Server Components”. We implemented the latest draft. Therefore, the implementation of Partial Hydration might slightly change throughout the beta. When you enable partial hydration, we start generating server components output starting from the page level. All top level components that have interactivity need to be marked as interactive by applying "use client". "use client"
export function MyInteractiveComponent() {
const [myState, setState] = useState(null);
useEffect(() => { setTimeout(() => setState(‘interactive’) }, 3000)
return <div>{myState}</div>
} Interactive componentsAny component that contains “useEffect”, “useState”, “createContext” or event handlers are considered interactive components. Also class components are not compatible with server components and should be marked as interactive as well. How do I know it’s working?When running Gatsby build you’ll see a couple of new activities: ...
success Building Partial Hydration renderer - 1.885s
...
success Building Partial HTML for pages - 0.113s - 6/6 52.96/s
… Run How can you help?
|
Beta Was this translation helpful? Give feedback.
Replies: 18 comments 32 replies
-
This is so cool! Very excited to check it out. Some early thoughts before trying this out:
|
Beta Was this translation helpful? Give feedback.
-
Hi team! Partial hydration sounds great! I'm the author and maintainer of Prismic's Gatsby plugins and will be testing them out with Gatsby 5. The source plugin ( The following errors appears when running
I created a minimal reproduction including a page using a HOC (it does nothing except forward the component) and "client export". https://github.com/angeloashmore/gatsby-5-partial-hydration-hoc-issue/
Are there special steps involved to get HOCs working with partial hydration? |
Beta Was this translation helpful? Give feedback.
-
Any idea what is wrong?
|
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
The current implementation of Partial Hydration is going to make it difficult to use CSS-in-JS isn't it? I'm trying it now on one of my client's projects that is styled with Emotion and it's bug city. Astro's isolated bundling works far better - even if it doesn't support CSS-in-JS either yet officially. As I said in their RFCs as well (withastro/roadmap#202) - one of the key promises of partial hydration is non-client rendering of CSS-in-JS components for the multitude of instances where an org can't/won't/shouldn't use other styling options. Static CSS/Tailwind/etc companies saving a few KB and boosting an already good PSI score is nothing compared to the big TBT improvements that support for CSS-in-JS would be able to deliver for other companies. Without this RFC is DOA. |
Beta Was this translation helpful? Give feedback.
-
Since Partial Hydration is using React Server Components, does that mean it is SSR only? Or am I not understanding Server Components correctly and they will work with static sites. |
Beta Was this translation helpful? Give feedback.
-
A new spec is out (https://github.com/reactjs/rfcs/blob/main/text/0227-server-module-conventions.md) and we've updated our implementation. You have to use |
Beta Was this translation helpful? Give feedback.
-
Hi! |
Beta Was this translation helpful? Give feedback.
-
First of all! Thank you for this great feature! I`ve been playing around with this this morning and I would like to give some comments on it:
/**
* To mark a component as client side, you add the "use client" directive.
*/
import React from "react"
export function Demo() {
return (
<div>
<div>{1}</div>
<div>{2}</div>
<div>{3}</div>
</div>
)
}
Thank you very much! |
Beta Was this translation helpful? Give feedback.
-
I seem to have an issue where I can't pull in functions from a separate JS file ie utility functions and use them on client components, in the network tab I get 404 files for |
Beta Was this translation helpful? Give feedback.
-
I have a problem using this along with
Do you have some ideas how can I overcome this? I set all gatsby and it's plugins ecosystem to |
Beta Was this translation helpful? Give feedback.
-
Hi! How would this work with styled components? |
Beta Was this translation helpful? Give feedback.
-
This is now available in 5.0.0: https://www.gatsbyjs.com/docs/reference/release-notes/v5.0/#partial-hydration-beta |
Beta Was this translation helpful? Give feedback.
-
does this work with .mdx files ? where should I put the |
Beta Was this translation helpful? Give feedback.
-
I just noticed this in the guide:
Is this just a current limitation or will it always be the case? |
Beta Was this translation helpful? Give feedback.
-
Checked out the preview yesterday for the first time! A few notes that I'm sure you're aware of — but curious to hear how they fit in the roadmap.
Thanks! Again, really cool to see this first iteration, but I'm curious about what's the long term plan and where this will be going. |
Beta Was this translation helpful? Give feedback.
-
Server Components in our codebase is wired into html generation, in our dev mode we don't run this step. The goal is to also do SSR in dev mode so it behaves more similarly to production. I think we can at least pull the RSC part out of that step and run it seperatly to at least unblock that part. It would be 1000x easier to iterate.
What Next is doing is great but when props changes, we can't re-fetch new data from our datalayer. Next can. As the app will go to public API. Our datalayer is only available during build. That would mean data will stay stale even if props changes which could be hard for people to understand. When we have Content-Cloud ready, we can add this kind of behavior where you can run I know even in Next when RSC is used the data stays stale and only ISR can change it but we only allow you to use data-fetching at the root of a page so |
Beta Was this translation helpful? Give feedback.
-
Hi all, I'm trying partial hydration on a small blog, but I'm facing an error apparently related to the In particular, the build fails with error I added detailed info here: #37948 |
Beta Was this translation helpful? Give feedback.
This is now available in 5.0.0: https://www.gatsbyjs.com/docs/reference/release-notes/v5.0/#partial-hydration-beta