You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I brought this idea up on Discord, but want to move the discussion here to go more in depth & for better visibility.
To optimize hydration by removing double data caused by hydrating model signals, the DOM can be queried to hydrate the data.
Currently, createResource has onHydrated, which I am using to hydrate model signals which has reduced the initial html payload by megabytes in my current project.
const[client__d_,client__d__set]=createSignal()constd_=d__()consthtml_id_=createResource(()=>'unique_html_id',{onHydrated: ((_,{ value })=>client__d__set(document.getElementById(value).getAttribute('d')))})return<pathid={html_id_()}d={d_()}/>functiond__(){if(!isServer)returnclient__d_// ssr data dependency sourcesreturncreateMemo(()=>/* load data from ssr data sources */)}
If onHydrated is added to JSX.Element, this boilerplate is reduced a bit...
const[client__d_,client__d__set]=createSignal()constd_=d__()return<pathd={d_()}onHydrated={el=>client__d__set(el.getAttribute('d'))}/>functiond__(){if(!isServer)returnclient__d_// ssr data dependency sourcesreturncreateMemo(()=>/* load data from ssr data sources */)}
New primitive functionality can further reduce the boilerplate to support this optimization. Off the top of my head it could look something like:
const[d_,client__d__set]=createHydrator(()=>{// ssr data dependency sourcesreturncreateMemo(()=>/* load data from ssr data sources */)})return<pathd={d_()}onHydrated={el=>client__d__set(el.getAttribute('d'))}/>
Note: If createResource were used to store & hydrate the d attribute, then the d value would have the double data issue, both in the HTML & in the createResource hydration. This optimization fixes the double data problem for the d attribute by querying the DOM to hydrate the model.
Note2: If // ssr data dependency sources were in the client code, it would be easy to accidentally trigger loading the full data model on the client side. It is more optimal to avoid loading the full data model & instead only hydrating the presented data on the client. This is why there is an isServer guard to only run // ssr data dependency sources on the server.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I brought this idea up on Discord, but want to move the discussion here to go more in depth & for better visibility.
To optimize hydration by removing double data caused by hydrating model signals, the DOM can be queried to hydrate the data.
Currently,
createResource
hasonHydrated
, which I am using to hydrate model signals which has reduced the initial html payload by megabytes in my current project.If
onHydrated
is added toJSX.Element
, this boilerplate is reduced a bit...New primitive functionality can further reduce the boilerplate to support this optimization. Off the top of my head it could look something like:
Note: If
createResource
were used to store & hydrate thed
attribute, then thed
value would have the double data issue, both in the HTML & in thecreateResource
hydration. This optimization fixes the double data problem for thed
attribute by querying the DOM to hydrate the model.Note2: If
// ssr data dependency sources
were in the client code, it would be easy to accidentally trigger loading the full data model on the client side. It is more optimal to avoid loading the full data model & instead only hydrating the presented data on the client. This is why there is anisServer
guard to only run// ssr data dependency sources
on the server.Beta Was this translation helpful? Give feedback.
All reactions