-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.js
More file actions
32 lines (28 loc) · 1.04 KB
/
Copy pathdetector.js
File metadata and controls
32 lines (28 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// detector.js
// Runs in the MAIN world, has access to window.next
(function () {
// console.log('Next.js Detector: Injected into MAIN world.');
function check() {
if (window.next && window.next.version) {
// console.log('Next.js Detector: Found window.next.version', window.next.version);
window.postMessage({ type: 'NEXT_JS_VERSION_DETECTED', version: window.next.version }, '*');
return true;
}
if (window.__NEXT_DATA__) {
// console.log('Next.js Detector: Found window.__NEXT_DATA__');
window.postMessage({ type: 'NEXT_JS_VERSION_DETECTED', version: null }, '*');
return true;
}
return false;
}
// Check immediately
if (check()) return;
// Poll for a few seconds in case of hydration delay
let attempts = 0;
const interval = setInterval(() => {
attempts++;
if (check() || attempts > 20) { // Poll for ~10 seconds (20 * 500ms)
clearInterval(interval);
}
}, 500);
})();