• hassan

    (@sangshenas)


    Environment

    • WordPress version: (6.9.4)
    • Plugin version: (3.0.2)
    • Browser: (chrome lastest)
    • Conflicting plugins: (WP Rocket)

    Description

    When opening the Block Editor (Gutenberg) to edit a post, the following JavaScript error appears in the browser console:

    Uncaught SyntaxError: Identifier 'jtoc_data_all' has already been declared
    (at post.php?post=XXXXX&action=edit:41204:11)

    Root Cause

    The variable jtoc_data_all is declared using let or const, which does not allow re-declaration in the same scope. When the plugin’s script is enqueued or rendered more than once on the editor page — likely due to a caching/optimization plugin combining or deferring scripts — the browser throws a SyntaxError on the duplicate declaration.

    Steps to Reproduce

    1. Install and activate Joli Table of Contents (latest version).
    2. Open any post in the Block Editor (post.php?action=edit).
    3. Open the browser DevTools console.
    4. Observe the SyntaxError.

    Expected Behavior

    No JavaScript errors in the console. The script should be loaded only once, and the variable declaration should be guarded against duplicate execution.

    Actual Behavior

    A SyntaxError is thrown because jtoc_data_all is declared twice with let/const in the same scope.

    Impact

    • The error only appears in the admin editor; the front-end TOC rendering is not affected.
    • However, the duplicate declaration could potentially block subsequent scripts from executing in certain environments.

    Suggested Fix

    Guard the variable declaration to prevent re-declaration:

    // Instead of:
    let jtoc_data_all = ...;
    
    // Use:
    if (typeof jtoc_data_all === 'undefined') {
        var jtoc_data_all = ...;
    }

    Alternatively, ensure the script is registered and enqueued only once on the editor page using wp_enqueue_script() with a unique handle and proper dependency management.

    Thank you for looking into this!

You must be logged in to reply to this topic.