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

Can't filter server-side based on client export #66604

Open
eric-burel opened this issue Jun 6, 2024 · 0 comments
Open

Can't filter server-side based on client export #66604

eric-burel opened this issue Jun 6, 2024 · 0 comments
Labels
bug Issue was opened via the bug report template. Internationalization (i18n) Related to Internationalization with Next.js. Module Resolution Module resolution (CJS / ESM, module resolving).

Comments

@eric-burel
Copy link
Contributor

eric-burel commented Jun 6, 2024

Link to the code that reproduces this issue

https://github.com/eric-burel/client-array-next-repro

To Reproduce

  1. npm install && npm run dev
  2. Open app

Current vs. Expected behavior

Current

Will trigger error Error: Attempted to call includes() from the server but includes is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.

I've also tried serializing the object, doesn't work.

Code sample

"use client"
// Can't be imported from a server component safely
export const i18nTokens = ["foobar"]
// Doesn't work either, in a RSC the value is an empty object
export const i18nTokensStr = JSON.stringify(["foobar"])

export function ClientComponent({ translations }: { translations: Array<[string, string]> }) {
    return <span>Show translated content for key "foo": {translations.find(([k, v]) => k === "foo")?.[1]}</span>
}

Expected

Filter tokens passed to the server.

This issue could be bypassed by exporting tokens from a separate ".tokens.js" file, imported from server and client components.
This makes development a bit more cumbersome though, in an already cumbersome system.

Since turbopack doesn't have plugins yet, automated extraction of i18n tokens is slightly more delicate, hence the choice of an explicit export.

I suspect the idea is to avoid mistakes from end user mutating array client-side and thinking the server value will update? If yes, this pattern should at least work with frozen objects.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May  7 09:00:52 UTC 2
  Available memory (MB): 27895
  Available CPU cores: 6
Binaries:
  Node: 18.18.2
  npm: 9.8.1
  Yarn: 4.0.2
  pnpm: 8.10.2
Relevant Packages:
  next: 15.0.0-canary.14 // Latest available version is detected (15.0.0-canary.14).
  eslint-config-next: N/A
  react: 19.0.0-rc-6f23540c7d-20240528
  react-dom: 19.0.0-rc-6f23540c7d-20240528
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Internationalization (i18n), Module Resolution

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

I am crafting an i18n system that allows filtering tokens during the data fetching step, so only relevant translation tokens hit the client. Full work is in the process of being published: https://www.ericburel.tech/blog/hybrid-i18n-next-astro-1

Related:

If this is issue is already documented somehow, adding a link in the error message would help, as googling the error message didn't help much.

@eric-burel eric-burel added the bug Issue was opened via the bug report template. label Jun 6, 2024
@github-actions github-actions bot added Internationalization (i18n) Related to Internationalization with Next.js. Module Resolution Module resolution (CJS / ESM, module resolving). labels Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Internationalization (i18n) Related to Internationalization with Next.js. Module Resolution Module resolution (CJS / ESM, module resolving).
Projects
None yet
Development

No branches or pull requests

1 participant