Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(use-image): image ReferenceError in SSR #4122

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-jobs-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/use-image": patch
---

fix Image ReferenceError in SSR
25 changes: 9 additions & 16 deletions packages/hooks/use-image/__tests__/use-image.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {renderHook} from "@testing-library/react-hooks";
import {renderHook, waitFor} from "@testing-library/react";
import {mocks} from "@nextui-org/test-utils";

import {useImage} from "../src";
Expand All @@ -14,31 +14,24 @@ describe("use-image hook", () => {
});

it("can handle missing src", () => {
const rendered = renderHook(() => useImage({}));
const {result} = renderHook(() => useImage({}));

expect(rendered.result.current).toEqual("pending");
expect(result.current).toEqual("pending");
});

it("can handle loading image", async () => {
const rendered = renderHook(() => useImage({src: "/test.png"}));
const {result} = renderHook(() => useImage({src: "/test.png"}));

expect(rendered.result.current).toEqual("loading");
expect(result.current).toEqual("loading");
mockImage.simulate("loaded");
await rendered.waitForValueToChange(() => rendered.result.current === "loaded");
await waitFor(() => expect(result.current).toBe("loaded"));
});

it("can handle error image", async () => {
mockImage.simulate("error");
const rendered = renderHook(() => useImage({src: "/test.png"}));
const {result} = renderHook(() => useImage({src: "/test.png"}));

expect(rendered.result.current).toEqual("loading");
await rendered.waitForValueToChange(() => rendered.result.current === "failed");
});

it("can handle cached image", async () => {
mockImage.simulate("loaded");
const rendered = renderHook(() => useImage({src: "/test.png"}));

expect(rendered.result.current).toEqual("loaded");
expect(result.current).toEqual("loading");
await waitFor(() => expect(result.current).toBe("failed"));
});
});
2 changes: 1 addition & 1 deletion packages/hooks/use-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function setImageAndGetInitialStatus(
if (!src) return "pending";
if (ignoreFallback) return "loaded";

const img = new Image();
const img = document.createElement("img");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add SSR safety check for document access

While using document.createElement("img") fixes the Image constructor ReferenceError, accessing document directly can still cause SSR issues. Consider adding a check for the browser environment.

-  const img = document.createElement("img");
+  const img = typeof document !== "undefined" 
+    ? document.createElement("img")
+    : null;
+
+  if (!img) return "pending";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const img = document.createElement("img");
const img = typeof document !== "undefined"
? document.createElement("img")
: null;
if (!img) return "pending";


img.src = src;
if (crossOrigin) img.crossOrigin = crossOrigin;
Expand Down
Loading