Skip to content

Improve error messaging for invalid HTML elements in next/head #80530

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

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

Conversation

xabierlameiro
Copy link

@xabierlameiro xabierlameiro commented Jun 14, 2025

For Contributors

Fixing a bug

What?

This PR improves the developer experience by providing clear warning messages when invalid HTML elements (<html> or <body>) are used inside next/head.

Why?

Currently, when developers mistakenly put <html> or <body> tags inside next/head, they receive a confusing error message: "next-head-count is missing". This doesn't explain what they did wrong or how to fix it.

How?

• Added validation logic in packages/next/src/shared/lib/head.tsx to detect <html> and <body> elements during development
• Show clear warning messages that explain the issue and link to documentation
• Added comprehensive tests to verify the warnings are displayed correctly

Changes:

• Modified: packages/next/src/shared/lib/head.tsx - Added validation and warning logic
• Added: Test case in test/development/pages-dir/client-navigation/head.test.ts
• Added: Test pages for reproducing the issue

Before:

• Confusing "next-head-count is missing" error
• No guidance on what caused the issue

After:

• Clear warning: "Do not use tags in next/head. You cannot use to set attributes on or tags. This will result in a 'next-head-count is missing' error."
• Includes link to documentation for more information

Fixes #20924

Test Plan

The implementation includes comprehensive tests that verify:

  1. Warning messages are displayed when <html> tags are used in next/head
  2. Warning messages are displayed when <body> tags are used in next/head
  3. Existing functionality remains unaffected

Tests pass with the new warning system in place.

- Add runtime warnings when <html> or <body> tags are used in next/head
- Helps developers understand why they get 'next-head-count is missing' error
- Provides clear error messages with documentation links
- Add comprehensive tests for the warning functionality

Fixes vercel#20924
@ijjk
Copy link
Member

ijjk commented Jun 14, 2025

Allow CI Workflow Run

  • approve CI run for commit: a15ec98

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Misleading "next-head-count is missing" error for invalid head tags
2 participants