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

Ability to run code before hydration #110

Closed
crisberrios opened this issue May 4, 2024 · 3 comments
Closed

Ability to run code before hydration #110

crisberrios opened this issue May 4, 2024 · 3 comments

Comments

@crisberrios
Copy link
Contributor

crisberrios commented May 4, 2024

Currently, we have access to data and onBeforeRender on the SSR pass, but we don't have a way to execute code before the hydration pass. This limits the ability to hydrate stores and forces us to do additional render passes.

example:

onBeforeRender(pageContext) {

const { data } = pageContext;
let initialStore = createStore(initialData);

if (!pageContext.isClientSideNavigation) {
	 hydrateStore(mapData(data));

    return {
      initialStore: serializeStore(initialStore)
    }
  }
}

Then on the client side:

function Page() {
  const { initialStore, isHydration } = usePageContext()

  if(isHydration) {
    hydrateStore(store, initialStore)
  }

  return (
    <>
    {store.someValue}
    </>
   )
 } 

Ideally, the store would be already hydrated before the hydration pass. This could be achieved by adding a custom hook such as +onBeforeHydration.

For my use case, making this a simple hook with a signature like `(pageContext) => void) would suffice, but for ergonomics, I think this could/should also work as a middleware, support async, and return pageContext.

For my example, this would look like:

import { store } from '../stores/store';
onBeforeHydration(pageContext) {
  hydrateStore(store, pageContext.initialStore)
}

and an async use case

async onBeforeHydration(pageContext) {
  const asyncData = await someAsyncPreRenderFunction();
  return {
     someProp: asyncData
   }
}

This would be similar, if not the same as #96

It may also help solve #87 and #57.

@brillout
Copy link
Member

brillout commented May 4, 2024

How about onBeforeRenderClient()? (Note pageContext.isHydration.)

PR welcome, but I can also do it once I'm done with the head management redesign.

@crisberrios
Copy link
Contributor Author

That works, I'll give it a shot, thank you.

@brillout
Copy link
Member

brillout commented May 6, 2024

Pre-released as 0.4.8-commit-68be831

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants