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

Using masonic in nextjs #173

Open
Lantianyou opened this issue Dec 11, 2024 · 8 comments
Open

Using masonic in nextjs #173

Lantianyou opened this issue Dec 11, 2024 · 8 comments

Comments

@Lantianyou
Copy link

According to this reddit page, seems quite a lots of hook will cause issuse using nextjs, and there is no work around. https://www.reddit.com/r/nextjs/comments/1bhfikg/window_is_not_defined_error_in_nextjs_14_app/

@Lantianyou
Copy link
Author

Lantianyou commented Dec 11, 2024

In my case

ReferenceError: ResizeObserver is not defined
    at useResizeObserver (components/ui/useResizeObserver.ts:11:4)
    at Masonry (components/ui/masonry.tsx:103:47)
   9 |   const [forceUpdate] = useForceUpdate()
  10 |   const resizeObserver = createResizeObserver(
> 11 |     positioner,

@jaredLunde
Copy link
Owner

It doesn't make sense to render this component on the server. Simply put, it needs a browser window.

#123

@Lantianyou
Copy link
Author

Yes, sure, the component throws an error anyway despite using "use client" directive. Cause all client component code are still run in ssr. As per the reddit post

@jaredLunde
Copy link
Owner

Use a dynamic import

@Lantianyou
Copy link
Author

I understand, but dynamic import only work for component, not for hooks. So for hooks like useResizeObserver, still will throw error on server side

@jaredLunde
Copy link
Owner

Ponyfill it? Dynamically import the component using the resize observer hook? Use a framework that isn't Next.js? Many options available

@Lantianyou
Copy link
Author

Understood it. Also appreciate your answer. Just here to state the problem, it will cause error in nextjs server. Guess one day nextjs will fix it.

@biowaffeln
Copy link

You can wrap it in a ClientOnly component to make sure it's doesn't run during SSR. Here's one from remix-utils, should probably work in Next.js as well:
https://github.com/sergiodxa/remix-utils/blob/main/src/react/client-only.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants