Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(react): replace static KaTeX CSS import with dynamic runtime injection#3314

Open
giulio-leone wants to merge 2 commits intoCopilotKit:mainfrom
giulio-leone:fix/katex-css-next-js-build
Open

fix(react): replace static KaTeX CSS import with dynamic runtime injection#3314
giulio-leone wants to merge 2 commits intoCopilotKit:mainfrom
giulio-leone:fix/katex-css-next-js-build

Conversation

@giulio-leone
Copy link

Summary

Fixes #2851

Problem

Next.js rejects global CSS imports from within node_modules, causing build failures:

Global CSS cannot be imported from within node_modules.
Location: node_modules/@copilotkitnext/react/dist/index.mjs

Import trace:
  katex/dist/katex.min.css
  @copilotkitnext/react/dist/index.mjs
  @copilotkit/react-core/dist/v2/index.mjs

This affects all Next.js consumers of CopilotKit (versions 1.50.0+).

Root Cause

The static import 'katex/dist/katex.min.css' in CopilotChatAssistantMessage.tsx (line 22) is preserved in the dist bundle by tsdown's unbundle mode. Next.js webpack statically analyzes imports and rejects global CSS from node_modules.

Fix

Replace the static CSS import with a useKatexStyles() hook that dynamically loads the stylesheet at runtime via useEffect:

  • Created useKatexStyles.ts — a singleton hook that uses import() to lazy-load KaTeX CSS
  • Dynamic imports bypass Next.js static analysis (the css-npm check only applies to static imports)
  • Uses a module-level flag to ensure the CSS is only injected once

Testing

  • 2 consecutive clean test passes: 748/748 tests (38 test files)
  • Type-check shows only pre-existing errors from @copilotkitnext/web-inspector (unrelated)

…ction

Next.js rejects global CSS imports from within node_modules, causing
build failures when consumers use @copilotkitnext/react:

  Global CSS cannot be imported from within node_modules.
  Location: node_modules/@copilotkitnext/react/dist/index.mjs

The static `import 'katex/dist/katex.min.css'` in
CopilotChatAssistantMessage.tsx gets preserved in the dist bundle
(tsdown unbundle mode), triggering the Next.js css-npm restriction.

Replace with a useKatexStyles() hook that dynamically imports the CSS
at runtime via useEffect. Dynamic imports bypass Next.js static
analysis and load the stylesheet lazily when the component mounts.

Fixes CopilotKit#2851
@vercel
Copy link
Contributor

vercel bot commented Feb 28, 2026

@giulio-leone is attempting to deploy a commit to the CopilotKit Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot
Copy link

changeset-bot bot commented Feb 28, 2026

🦋 Changeset detected

Latest commit: 7d75f3c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@copilotkitnext/react Patch
@copilotkit/react-core Patch
@copilotkitnext/next-pages-router Patch
@copilotkit/a2ui-renderer Patch
@copilotkit/react-textarea Patch
@copilotkit/react-ui Patch
@copilotkit/runtime-client-gql Patch
@copilotkit/runtime Patch
@copilotkit/sdk-js Patch
@copilotkit/shared Patch
@copilotkit/voice Patch
@copilotkitnext/agent Patch
@copilotkitnext/angular Patch
@copilotkitnext/core Patch
@copilotkitnext/demo-agents Patch
@copilotkitnext/eslint-config Patch
@copilotkitnext/runtime Patch
@copilotkitnext/shared Patch
@copilotkitnext/sqlite-runner Patch
@copilotkitnext/typescript-config Patch
@copilotkitnext/web-inspector Patch
@copilotkitnext/node-express Patch
@copilotkit-storybook/angular Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@giulio-leone giulio-leone force-pushed the fix/katex-css-next-js-build branch 2 times, most recently from 29affe1 to 7d75f3c Compare February 28, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug: Next.js build fails due to global CSS import from transitive dependency (@copilotkitnext/react)

1 participant