Skip to content

Commit 5ff22c5

Browse files
committed
chore(upload-widget): use stable id for script element (React 18+ fallback)
1 parent 5662382 commit 5ff22c5

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

next-cloudinary/src/components/CldUploadWidget/CldUploadWidget.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ import {
2323

2424
import { getCloudinaryConfig } from "../../lib/cloudinary";
2525

26+
// Stable ID hook supporting React < 18 fallback
27+
const useStableId = () => {
28+
if (typeof (React as any).useId === 'function') {
29+
return (React as any).useId().replace(/:/g, '');
30+
}
31+
return React.useRef(`id-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`).current;
32+
};
33+
2634
const CldUploadWidget = ({
2735
children,
2836
config,
@@ -34,6 +42,7 @@ const CldUploadWidget = ({
3442
uploadPreset,
3543
...props
3644
}: CldUploadWidgetProps) => {
45+
const uniqueId = useStableId();
3746
const cloudinary: CldUploadWidgetCloudinaryInstance = useRef();
3847
const widget: CldUploadWidgetWidgetInstance = useRef();
3948

@@ -239,7 +248,7 @@ const CldUploadWidget = ({
239248
...instanceMethods,
240249
})}
241250
<Script
242-
id={`cloudinary-uploadwidget-${Math.floor(Math.random() * 100)}`}
251+
id={`cloudinary-uploadwidget-${uniqueId}`}
243252
src="https://upload-widget.cloudinary.com/global/all.js"
244253
onLoad={handleOnLoad}
245254
onError={(e) => console.error(`Failed to load Cloudinary Upload Widget: ${e.message}`)}

0 commit comments

Comments
 (0)