Skip to content

Commit 6b1f027

Browse files
elroypedrokameoelroypedrokameoelroypedrokameo10
authored
fix: add promise caching and error handling in loadNavermapsScript (#128)
* fix: add promise caching and error handling in loadNavermapsScript - Implement promise caching to prevent duplicate script loading on concurrent calls - Add null check for window.naver.maps with descriptive error message - Reset cached promise on failure to allow retry - Add .catch() in LoadNavermapsScript component to handle rejected promises * fix: use URL-keyed Map for promise caching instead of single global variable Different option sets (e.g., different client IDs or submodules) now each get their own cached promise instead of sharing a single global one. --------- Co-authored-by: elroypedrokameo <infinitygramexray@gmail.com> Co-authored-by: elroypedrokameo <elroypedrokameo@unnispick.com>
1 parent e545f39 commit 6b1f027

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

packages/react-naver-maps/src/load-navermaps-script.tsx

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,42 @@ import type { ReactElement } from 'react';
44
import type { ClientOptions } from './types/client';
55
import { loadScript } from './utils/load-script';
66

7+
const cachedPromises = new Map<string, Promise<typeof naver.maps>>();
8+
79
export function loadNavermapsScript(options: ClientOptions) {
810
const url = makeUrl(options);
911

10-
// TODO: Caching Promise
11-
12-
const promise = loadScript(url).then(() => {
13-
const navermaps = window.naver.maps;
14-
15-
if (navermaps.jsContentLoaded) {
16-
return navermaps;
17-
}
12+
const cached = cachedPromises.get(url);
13+
if (cached) {
14+
return cached;
15+
}
1816

19-
return new Promise<typeof naver.maps>(resolve => {
20-
navermaps.onJSContentLoaded = () => {
21-
resolve(navermaps);
22-
};
17+
const promise = loadScript(url)
18+
.then(() => {
19+
const navermaps = window.naver?.maps;
20+
21+
if (!navermaps) {
22+
throw new Error('react-naver-maps: Failed to load Naver Maps script. '
23+
+ 'window.naver.maps is not available. '
24+
+ 'Please check your client ID and network connection.');
25+
}
26+
27+
if (navermaps.jsContentLoaded) {
28+
return navermaps;
29+
}
30+
31+
return new Promise<typeof naver.maps>(resolve => {
32+
navermaps.onJSContentLoaded = () => {
33+
resolve(navermaps);
34+
};
35+
});
36+
})
37+
.catch((err) => {
38+
cachedPromises.delete(url);
39+
throw err;
2340
});
24-
});
41+
42+
cachedPromises.set(url, promise);
2543

2644
return promise;
2745
}
@@ -63,12 +81,16 @@ export function LoadNavermapsScript({
6381
const [navermaps, setNavermaps] = useState<typeof naver.maps>();
6482

6583
useEffect(() => {
66-
loadNavermapsScript(options).then((maps) => {
67-
setNavermaps(maps);
68-
});
84+
loadNavermapsScript(options)
85+
.then((maps) => {
86+
setNavermaps(maps);
87+
})
88+
.catch((err) => {
89+
console.error(err);
90+
});
6991
}, []);
7092

7193
return (
72-
(navermaps && Children) ? <Children/> : null
94+
(navermaps && Children) ? <Children /> : null
7395
);
7496
}

0 commit comments

Comments
 (0)