question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

next/dynamic not recognizing referenced objects

See original GitHub issue

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

16.13.1

What browser are you using?

What operating system are you using?

Windows (Docker Node.js devcontainer)

How are you deploying your application?

Describe the Bug

When using next/dynamic this way:

const dynamicProps = {
  loading: () => <Loading />,
  ssr: false,
};

const LoginView = dynamic(
  () => import('@/components/auth/LoginView'),
  dynamicProps,
);

I get the following error:

Error: error: next/dynamic options must be an object literal.

But just using an spread operator solves the issue:

const dynamicProps = {
  loading: () => <Loading />,
  ssr: false,
};

const LoginView = dynamic(() => import('@/components/auth/LoginView'), {
  ...dynamicProps,
});

Seems that SWC is failing to recognize and transform the variable

Expected Behavior

I expect the app to build and parse correctly the dynamic options

To Reproduce

const dynamicProps = {
  loading: () => <Loading />,
  ssr: false,
};

const LoginView = dynamic(
  () => import('@/components/auth/LoginView'),
  dynamicProps,
);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
luisorbaicetacommented, Dec 17, 2021

I know, as I’ve said in the description you can trick the compiler by spreading the variable. But I don’t think this should be intended behavior

2reactions
MikeDupreecommented, Dec 17, 2021

https://nextjs.org/docs/messages/invalid-dynamic-options-type

You can get it working by updating the Layout.tsx files dynamic imports to be

const Loading = () => (
  <div className="w-80 h-80 flex items-center text-center justify-center p-3">
    <LoadingDots />
  </div>
)

const SignUpView = dynamic(
  () => import('@components/auth/SignUpView'),
  {
    loading: Loading
  }
)

const ForgotPassword = dynamic(
  () => import('@components/auth/ForgotPassword'),
  {
    loading: Loading
  }
)

const FeatureBar = dynamic(
  () => import('@components/common/FeatureBar'),
  {
    loading: Loading
  }
)

const Modal = dynamic(
  () => import('@components/ui/Modal'),
  {
    loading: Loading,
    ssr: false
  }
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Next js dynamic imports with object destucturing
UDFCompatibleDatafeed is a javascript class. I am getting an error. TypeError: UDFCompatibleDatafeed is not a constructor. Seems this is not ...
Read more >
How to Use Dynamic Imports in Next.js - WebDevStudios
At the time of writing this, dynamic imports in Next.js are only supported for ... Assign the dynamic function to reference your component....
Read more >
Master code splitting with dynamic imports in Next.js - Daily.dev
To import it using the dynamic import() from the src/components/hello. js file, we will use named exports. import dynamic from "next/dynamic"; ...
Read more >
Data Fetching: getStaticProps - Next.js
The context parameter is an object containing the following keys: params contains the route parameters for pages using dynamic routes. For example, if...
Read more >
Built-in reference types - C# reference - Microsoft Learn
The object type; The string type; The delegate type; The dynamic type ... defined to compare the values of string objects, not references....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found