Skip to content
New issue

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

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

Already on GitHub? # to your account

Cannot serialize using "unstable_serialize" in Server Components #2594

Closed
ariesclark opened this issue Apr 30, 2023 · 2 comments · Fixed by #2673
Closed

Cannot serialize using "unstable_serialize" in Server Components #2594

ariesclark opened this issue Apr 30, 2023 · 2 comments · Fixed by #2673

Comments

@ariesclark
Copy link

Bug report

Description / Observed Behavior

import { unstable_serialize } from "swr/infinite" or "swr"; throws an error in SSR.
Which makes it difficult to fill the fallback cache to satisfy Error: Fallback data is required when using suspense in SSR.

You're importing a component that needs useRef. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.

   ,-[/<...>/node_modules/.pnpm/swr@2.1.5_react@18.2.0/node_modules/swr/infinite/dist/index.mjs:1:1]
 1 | import { useRef, useCallback } from 'react';
   :          ^^^^^^
 2 | import useSWR from 'swr';
 3 | import { withMiddleware, createCacheHelper, isUndefined, useIsomorphicLayoutEffect, serialize, UNDEFINED, isFunction } from 'swr/_internal';
 4 | import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
   `----

The error was caused by importing 'swr/infinite/dist/index.mjs' in './src/app/<...>/layout.tsx'.

A hacky solution is to use the result of the serialize function.

<SWRConfig
  value={{
    fallback: {
      [`$inf$@"conversations",0,`]: [conversations]
    }
  }}
>...</SWRConfig>

Expected Behavior

Should be able to serialize the key on the server.

Repro Steps / Code Example

Additional Context

SWR version: 2.1.5
Next.js version: 13.3.1

SWRConfig is a wrapper around the provider using "use client", as mentioned in my previous issue: #2464

@koba04 koba04 changed the title Cannot serialize using "unstable_serialize" in SSR Cannot serialize using "unstable_serialize" in Server Components May 8, 2023
@koba04
Copy link
Collaborator

koba04 commented Jun 17, 2023

closed via #2664

@koba04 koba04 closed this as completed Jun 17, 2023
@huozhi
Copy link
Member

huozhi commented Jun 17, 2023

Reopen as it's still having a bundling issue in beta version

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

Successfully merging a pull request may close this issue.

3 participants