WAIT: Invalid or unexpected token "WpRestNonce" Error when using ESI Litespeed

Hey i have a bug when using the Litespeed Cache Plugin in combination with a Litespeed Server and enabling ESI.
The Error: Uncaught SyntaxError: Invalid or unexpected token
at the bricks-scripts-js-extra script on the frontend from the frontend.php wpRestNonce => wp_create_nonce ( ‘wp_rest’ ). This causing further issues with other js files down the line.

<script id="bricks-scripts-js-extra">var bricksData = {"debug":"","locale":"de_DE","ajaxUrl":"https:\/\/nookyyy.com\/wp-admin\/admin-ajax.php","restApiUrl":"https:\/\/nookyyy.com\/wp-json\/bricks\/v1\/","nonce":"4966c193d7","formNonce":"ecdf032e75","wpRestNonce":"<!-- Block cached by LiteSpeed Cache 6.5.0.2 on 2024-09-21 15:32:13 -->
<!-- X-LiteSpeed-Cache-Control: public,max-age=604800,esi=on -->
<!-- Full varies: guest_mode:1+webp -->
<!-- X-LiteSpeed-Tag: b79_HTTP.200,b79_HTTP.301,b79_ -->","postId":"2", ... rest here ... </script>

Im not sure if this is an issue that would need fixing on bricks side or on litespeeds.
A hotfix im using at this moment ( in case anyone else got the same issue ):

add_filter('litespeed_esi_nonces', 'remove_wp_rest_nonce', 20);
function remove_wp_rest_nonce($nonces) {
    
    // Check if nonces array is empty
    if (empty($nonces)) {
        return $nonces;
    }
    
    $modified = false;
    
    // Loop through nonces
    foreach ($nonces as $i => $nonce) {
    
        // Check if nonce is wp_rest
        if (strpos($nonce, 'wp_rest') === 0) {
            // Remove wp_rest nonce
            unset($nonces[$i]);
            $modified = true;
        }
    
    }
    
    // Reindex array if modified
    if ($modified) {
        $nonces = array_values($nonces);
    }
    
    return $nonces;
}

How to reproduce the issue: simply activate ESI on Litespeed Cache Plugin ( Server needs to be Litespeed / support ESI - nginx/apache does not support ESI )

regards,
Artem

Maybe this? Email support.

Hi @Faradei,

yeah, this might be the same issue as the one that was linked by @macksix. You can email us, and we can send you the fix. Then you should see if it’s the same issue or not - but it looks like it is.

Best regards,
Matej

Hi Matej,

Thanks for the quick reply. I’d appreciate it if you could send the fix.

Could you please let me know what email address I should use to contact you?

Hi there,

no need to send a fix now :slight_smile: Please update to 2.1.4 , and the issue should be solved. :slight_smile:

Let me know please.

Thanks.
Matej

Hi Matej,

I’m on 2.3.1 now and the issue still exists. I think there’s been a mix-up — the linked thread is about nonces expiring after cache TTL (forms break after days). My issue is different:

When LiteSpeed ESI is enabled, it replaces the wp_rest nonce inside the bricksData JSON with an HTML comment block. This breaks the entire JavaScript object immediately, not after TTL expiry:

js

var bricksData = {"wpRestNonce":"<!-- ESI block by LiteSpeed -->","postId":"2"...}

This is invalid JS — bricksData never initializes, and all Bricks frontend JS fails (forms, filters, AJAX pagination, interactions).

The root cause: wp_rest is in LiteSpeed’s predefined ESI nonces list (data/esi.nonces.txt), and Bricks outputs it inline inside wp_localize_script JSON (line 406, frontend.php).

A proper fix would be to output wpRestNonce in a separate inline script instead of inside the JSON object, so ESI replacement doesn’t break the JSON structure.

I’m still using my workaround (removing wp_rest from ESI nonces list via litespeed_esi_nonces filter).

Could this be re-opened?

Hi @Faradei ,

Thanks for the detailed explanation.

This doesn’t appear to be specific to Bricks itself, but rather an interaction with LiteSpeed ESI replacing the wp_rest nonce inside JavaScript output. In that scenario, any plugin or theme that embeds the REST nonce into a JS object/string could run into the same problem. Don’t you think so?

Based on the documentation, Bricks just added the frontend object data to the array correctly. (third parameter as an array)

Regards,
Jenn