Skip to content

Commit

Permalink
Adding timeslicing for hydrating :)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjaiyan-dev authored Aug 14, 2022
1 parent 16edf0c commit aeb71c1
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/integrations/react/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { createElement } from 'react';
import { createElement, startTransition } from 'react';
import { createRoot, hydrateRoot } from 'react-dom/client';
import StaticHtml from './static-html.js';

/**requestIdleCallback pollyfill https://developer.chrome.com/blog/using-requestidlecallback/#checking-for-requestidlecallback */
window?.requestIdleCallback = window?.requestIdleCallback || function (cb) {
var start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
}
});
}, 1);
}

function isAlreadyHydrated(element) {
for (const key in element) {
if (key.startsWith('__reactContainer')) {
Expand All @@ -27,7 +40,13 @@ export default (element) =>
delete element[rootKey];
}
if (client === 'only') {
return createRoot(element).render(componentEl);
return startTransition(() => {
createRoot(element).render(componentEl);
})
}
return hydrateRoot(element, componentEl);
return window?.requestIdleCallback(() => {
startTransition(() => {
hydrateRoot(element, componentEl);
})
})
};

0 comments on commit aeb71c1

Please sign in to comment.