Skip to content

Commit 32e5b61

Browse files
dieulotaddyosmani
authored andcommitted
Fail silently, don’t throw an error, when IntersectionObserver isn’t available (#113)
1 parent b01e5bb commit 32e5b61

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/index.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import requestIdleCallback from './request-idle-callback.mjs';
1919

2020
const toPrefetch = new Set();
2121

22-
const observer = new IntersectionObserver(entries => {
22+
const observer = window.IntersectionObserver && new IntersectionObserver(entries => {
2323
entries.forEach(entry => {
2424
if (entry.isIntersecting) {
2525
const link = entry.target;
@@ -74,7 +74,7 @@ function isIgnored(node, filter) {
7474
export default function (options) {
7575
if (!options) options = {};
7676

77-
observer.priority = options.priority || false;
77+
observer && (observer.priority = options.priority || false);
7878

7979
const allowed = options.origins || [location.hostname];
8080
const ignores = options.ignores || [];
@@ -86,7 +86,7 @@ export default function (options) {
8686
// If URLs are given, prefetch them.
8787
if (options.urls) {
8888
options.urls.forEach(prefetcher);
89-
} else {
89+
} else if (observer) {
9090
// If not, find all links and use IntersectionObserver.
9191
Array.from((options.el || document).querySelectorAll('a'), link => {
9292
observer.observe(link);

0 commit comments

Comments
 (0)