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

Adds nextjs specific dynamic import map #2030

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zomars
Copy link

@zomars zomars commented Apr 2, 2024

closes #1725 #1576

What is the purpose of this pull request?

  • New Icon
  • Bug fix
  • New Feature
  • Documentation update
  • Other:

Description

As specified by the Next.js team we should not use dynamic inside a React component. This uses the recommended approach with a map with dynamic imports.

Before Submitting

@github-actions github-actions bot added 📖 documentation Improvements or additions to documentation 🌍 site Has to do something with the Lucide website ⚛️ react package Lucide React Package labels Apr 2, 2024
@zomars
Copy link
Author

zomars commented Apr 3, 2024

On further inspection, would it make sense to publish this map as a separate lucide-next package?

@ericfennis
Copy link
Member

Hmm, yess, we shouldn't have next as a dependency in lucide-react.
We should move this to a separate package, and maybe the current dynamicImport file should be in there as well.
We could maybe generate all "dynamicImports" when installing the package, It would be nice if we could remove the dynamicImports from the lucide-react itself, currently, it is often used wrong.
We should scope this a bit more for specific use-cases.

@zomars
Copy link
Author

zomars commented Apr 5, 2024

Hmm, yess, we shouldn't have next as a dependency in lucide-react. We should move this to a separate package, and maybe the current dynamicImport file should be in there as well. We could maybe generate all "dynamicImports" when installing the package, It would be nice if we could remove the dynamicImports from the lucide-react itself, currently, it is often used wrong. We should scope this a bit more for specific use-cases.

Agreed. Please let me know how I can help in here. RN we have it hard coded in our open source repo but it make sense as you said to have it generated on install.

Copy link

github-actions bot commented May 6, 2024

This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label May 6, 2024
@vsnthdev
Copy link

Not stale, we need this PR because the issue hasn't been solved yet.

@github-actions github-actions bot removed the Stale label Aug 12, 2024
@michaelschufi
Copy link

Hi @zomars
Thank you for this PR and the link to the hardcoded file!
This is working well so far. But I wonder how you are handling this at the moment. I've seen that in your main branch you have stopped using this dynamic import map. If I may ask - What is your strategy now and what's been your experience regarding performance with lucide-react and Next.js?

@dsbrianwebster
Copy link

dsbrianwebster commented Sep 7, 2024

@michaelschufi been banging my head against the wall wondering why my development server was sooooo slow until I found this of this thread and this one (#1576). Definitely feel like this needs more attention either by way of a fix, or improved documentation on best practices.

Sharing my current workaround for now below...

import { type LucideProps, icons } from 'lucide-react';

type IconComponentName = keyof typeof icons;

interface IconProps extends LucideProps {
  name: string; // because this is coming from the CMS
}

// 👮‍♀️ guard
function isValidIconComponent(componentName: string): componentName is IconComponentName {
  return componentName in icons;
}

export function DynamicIcon({ name, ...props }: IconProps) {
  // we need to convert kebab-case to PascalCase because we formerly relied on
  // lucide-react/dynamicIconImports and the icon names (not the component names) are what are stored in the CMS.
  const kebabToPascal = (str: string) =>
    str
      .split('-')
      .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
      .join('');

  const componentName = kebabToPascal(name);

  // ensure what is in the CMS is a valid icon component
  if (!isValidIconComponent(componentName)) {
    return null;
  }

  // lucide-react/dynamicIconImports makes makes NextJS development server very slow
  // https://github.com/lucide-icons/lucide/issues/1576
  const Icon = icons[componentName];

  return <Icon {...props} />;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📖 documentation Improvements or additions to documentation ⚛️ react package Lucide React Package 🌍 site Has to do something with the Lucide website
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Next.js 13+ Dynamic imports cause client side re-render.
5 participants