Skip to content

[Bug]: Uncaught ReferenceError: __name is not defined in Injected Theme Script #370

@aeejaz

Description

@aeejaz

What happened?

When loading a page in our Next.js application, a critical JavaScript error occurs, visible in the browser's developer console. The error is: Uncaught ReferenceError: __name is not defined.

This error originates from the inline script that next-themes injects into the of the document to prevent the "flash of incorrect theme" (FOIT). The problematic script contains a function call to __name, which is a helper function likely injected by Next.js's SWC compiler for debugging purposes. However, in the final build, the function call is present but the function's definition is missing, causing the script to fail and break client-side rendering.

A snippet of the problematic generated code looks like this:

<script>
    (
        () => {
            // ... function logic for setting the theme
            function k2(theme) { 
                /* ... */ 
            }
            __name(k2, "k2"); // <-- ERROR IS THROWN HERE
            
            // ... logic to call the function
        }
    )()
</script>

This issue breaks the theme initialization and can potentially impact other client-side scripts from running correctly.

I expected the page to load without any client-side JavaScript errors. The theme-switching script should execute silently and correctly apply the stored or system-preferred theme to the element before the page hydrates, ensuring a smooth user experience with no theme flashing.

Version

0.4.6

What browsers are you seeing the problem on?

Microsoft Edge, Chrome

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions