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

Top-level await causes the client component to report an error. Element type is invalid. Received a promise that resolves to: undefined. Lazy element type must resolve to a class or function. #62763

Open
Innei opened this issue Mar 2, 2024 · 4 comments
Labels
bug Issue was opened via the bug report template.

Comments

@Innei
Copy link

Innei commented Mar 2, 2024

Link to the code that reproduces this issue

https://github.com/Innei/nextjs-top-level-reproduction

To Reproduce

  1. clone this repo
  2. run dev
  3. open dev url

Write a client component like this.

'use client'

import type { FC } from 'react'

const highlighter = await Promise.resolve([]) // with a top level await any value


export const ShikiHighLighter: FC<{}> = (props) => {
  console.log(highlighter)
  return null
}

and import this in root component.

// page.tsx
import Image from 'next/image'
import { ShikiHighLighter } from '../../components/shiki' // only import also lead to all app crash

export default function Home() {
  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      <ShikiHighLighter />
    </main>
  )
}

CleanShot 2024-03-03 at 12  56 57@2x

Current vs. Expected behavior

current:
Error: Element type is invalid. Received a promise that resolves to: undefined. Lazy element type must resolve to a class or function.

Expected: DONT crash

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:31:00 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6020
Binaries:
  Node: 20.8.1
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: 8.14.1
Relevant Packages:
  next: 14.1.1
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

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

App Router

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

next dev (local)

Additional context

No response

@Innei Innei added the bug Issue was opened via the bug report template. label Mar 2, 2024
@maxratajczak
Copy link

maxratajczak commented Mar 16, 2024

Yep, have this issue too. I'm using top level await to lazy import an object from another file and I get the same error.

@maxratajczak
Copy link

FYI this issue seems to only happen in the app router with server and client components. When I switched to the pages router the issue doesn't happen. Looks like I won't be using app router any time soon.

@Innei
Copy link
Author

Innei commented Mar 17, 2024

There are two ways to fix this issue.

  1. Only use top-level await in server-only component.
  2. use await in async component.

@ezequiel
Copy link

It was mentioned in #64508 that @gnoff is looking into this error? 😮

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.
Projects
None yet
Development

No branches or pull requests

3 participants