Skip to content

Commit

Permalink
Fix requestIdleCallback logic (#112)
Browse files Browse the repository at this point in the history
* fix(#111): hydration failure in safari

* chore: update package lock

* fix(#111): use requestIdleCallback shim

* chore: changeset
  • Loading branch information
natemoo-re committed Jan 31, 2021
1 parent 33a7844 commit ddd48ea
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-pumas-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"microsite": patch
---

Include shim for `requestIdleCallback` for Safari
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions packages/microsite/assets/microsite-runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { h, hydrate as mount } from "preact";

if (!("requestIdleCallback" in window)) {
window.requestIdleCallback = function (cb) {
return setTimeout(function () {
var start = Date.now();
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
},
});
}, 1);
};

window.cancelIdleCallback = function (id) {
clearTimeout(id);
};
}

const createObserver = (hydrate) => {
if (!("IntersectionObserver" in window)) return null;

Expand Down Expand Up @@ -29,7 +47,6 @@ function attach(fragment, data, { key, name, source }) {
switch (method) {
case "idle": {
if (
!("requestIdleCallback" in window) ||
!("requestAnimationFrame" in window)
)
return setTimeout(hydrate, 0);
Expand Down Expand Up @@ -153,9 +170,6 @@ export default (manifest) => {
});
}
};
if ("requestIdleCallback" in window) {
requestIdleCallback(init, { timeout: 1000 });
} else {
init();
}

requestIdleCallback(init, { timeout: 1000 });
};

0 comments on commit ddd48ea

Please sign in to comment.