From 10c7642a83e609f8e10d761e93e51e968c11e94c Mon Sep 17 00:00:00 2001 From: Max Artemov Date: Mon, 11 Nov 2024 11:06:54 +0200 Subject: [PATCH] use globalThis instead of window in HMR (#134) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * use globalThis instead of window in HMR * Update src/hooks/useHMR.ts Co-authored-by: antoniopresto * chore: adjust logic --------- Co-authored-by: antoniopresto Co-authored-by: 二货机器人 --- src/hooks/useHMR.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/hooks/useHMR.ts b/src/hooks/useHMR.ts index ff54518..d541906 100644 --- a/src/hooks/useHMR.ts +++ b/src/hooks/useHMR.ts @@ -19,8 +19,14 @@ if ( (module as any).hot && typeof window !== 'undefined' ) { - const win = window as any; - if (typeof win.webpackHotUpdate === 'function') { + // Use `globalThis` first, and `window` for older browsers + // const win = globalThis as any; + const win = + typeof globalThis !== 'undefined' + ? globalThis + : ((typeof window !== 'undefined' ? window : null) as any); + + if (win && typeof win.webpackHotUpdate === 'function') { const originWebpackHotUpdate = win.webpackHotUpdate; win.webpackHotUpdate = (...args: any[]) => {