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

14.2.0 throws errors with Turbopack when importing client components in route handlers that use context #64412

Closed
jmikrut opened this issue Apr 12, 2024 · 7 comments · Fixed by #64520 or #64751
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked Turbopack Related to Turbopack with Next.js.

Comments

@jmikrut
Copy link
Contributor

jmikrut commented Apr 12, 2024

Link to the code that reproduces this issue

https://github.com/jmikrut/next-turbo-client-components

To Reproduce

  1. Start up the app with Webpack - npm run dev - note that it works
  2. Start up the app with Turbopack - npm run dev:turbo - note that it breaks

Current vs. Expected behavior

Turbopack throws the following error:

TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/context-in-server-component

But, the component that uses createContext indeed has the 'use client' directive at the top of the file, so this error should not be thrown.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.16.0
  pnpm: 8.15.1
Relevant Packages:
  next: 14.2.0 // Latest available version is detected (14.2.0).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

Turbopack (--turbo)

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

next dev (local)

Additional context

Up until 14.2.0-canary.34, Turbopack was working properly for us at Payload. After that, Turbopack now throws an error.

Release that breaks it:
https://github.com/vercel/next.js/releases/tag/v14.2.0-canary.34

It might seem strange that we are importing client components in a request handler, BUT, the Payload config contains both server-side and client-side code, including custom React components. We need the Payload config within API routes in order to function, so, safely being able to disregard client components within route handlers is paramount to us.

Also of note, useState works fine in the ClientComponent included in this repro. It's just createContext that breaks Turbopack.

PACK-3014

@jmikrut jmikrut added the bug Issue was opened via the bug report template. label Apr 12, 2024
@github-actions github-actions bot added the Turbopack Related to Turbopack with Next.js. label Apr 12, 2024
@dkokotov
Copy link

dkokotov commented Apr 12, 2024

I've run into a similar error but with a slightly different repro scenario.

in my case, I have a server component like this:

import { Center } from '@chakra-ui/react';

export default function MyServerComponent() {
    return (
        <Center>
            <MyClientComponent />
        </Center>
    );
}

Even though I am using version 2.8.2 of @chakra-ui/react which has all of its components marked with use client, this throws off the same TypeError: createContext only works in Client Components ... error as in OP. Moving the <Center/> into MyClientComponent, which is defined in my own project and also has use client, fixes it. So it seems like the behavior (in my case, different from OP) is related to importing a client component from a different package.

wbinnssmith added a commit that referenced this issue Apr 15, 2024
Resolves #64412

This adds a client transition to the app route `ModuleAssetContext` and the corresponding transforms so that client components can be safely imported and referenced (as their proxies) in app routes.

Test Plan: Added an integration test
wbinnssmith added a commit that referenced this issue Apr 15, 2024
Resolves #64412

This adds a client transition to the app route `ModuleAssetContext` and
the corresponding transforms so that client components can be safely
imported and referenced (as their proxies) in app routes.

Test Plan: Added an integration test


Closes PACK-2964
ztanner pushed a commit that referenced this issue Apr 17, 2024
Resolves #64412

This adds a client transition to the app route `ModuleAssetContext` and
the corresponding transforms so that client components can be safely
imported and referenced (as their proxies) in app routes.

Test Plan: Added an integration test


Closes PACK-2964
@jmikrut
Copy link
Contributor Author

jmikrut commented Apr 22, 2024

Hey @wbinnssmith I just did some testing with this on 14.3.0-canary.15 and we are still seeing the same error.

Might be worth noting that we have components within node_modules that are using context. Did your fix / test address context usage coming from Node modules as well? Or only local files?

I have updated the reproduction in this initial post to demonstrate that local client files with useContext are now working since the fix referencing this issue, but context usage from within node_modules is still broken.

Take a look at this file for a working route:
https://github.com/jmikrut/next-turbo-client-components/blob/main/app/my-route-working/route.ts

And this one for a broken route:
https://github.com/jmikrut/next-turbo-client-components/blob/main/app/my-route-broken/route.ts

Any ideas?

@wbinnssmith
Copy link
Member

@jmikrut ah yeah, I'm working on resolving that in #64751. Will re-open.

@wbinnssmith wbinnssmith reopened this Apr 23, 2024
@jmikrut
Copy link
Contributor Author

jmikrut commented Apr 23, 2024

Amazing - thank you!

@ForsakenHarmony ForsakenHarmony added the linear: turbopack Confirmed issue that is tracked by the Turbopack team. label Apr 25, 2024
wbinnssmith added a commit that referenced this issue Apr 26, 2024
…64751)

This extends #64520 to cover cases where client components originate
from node_modules (foreign code).

Test Plan: Extended the integration test to cover this

Closes #64412
Fixes PACK-3014

---------

Co-authored-by: OJ Kwon <1210596+kwonoj@users.noreply.github.com>
@jmikrut
Copy link
Contributor Author

jmikrut commented Apr 27, 2024

Epic - thanks everyone. Confirmed everything works in Payload again. 👍

@timneutkens
Copy link
Member

Thanks for checking James!

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked Turbopack Related to Turbopack with Next.js.
Projects
None yet
5 participants