• Resolved playalistic_p

    (@playalistic_p)


    Hi there,
    I installed WooCommerce for the first time and right now are in the progress of creating/modifying the necessary pages for the users.

    When i drop the “All products” block on the shop page in Gutenberg editor, my 3 test-products are shown fine.

    But when i visit the actual page, i get the following error:
    Uncaught ReferenceError: wp is not defined at bortec-tuning.de/:2:843

    At the above location there is this snippet:

    <script type='text/javascript' id='wp-api-fetch-js-after'>wp.apiFetch.use(wp.apiFetch.createRootURLMiddleware( "https://bortec-tuning.de/wp-json/" ) );
    wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "9f1078e68b" );
    wp.apiFetch.use( wp.apiFetch.nonceMiddleware );
    wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );
    wp.apiFetch.nonceEndpoint = "https://bortec-tuning.de/wp-admin/admin-ajax.php?action=rest-nonce";</script>

    I have Autoptimize and Async JavaScript installed, but this particular snippet is the only one on that page that has no defer attribute attached and is not minimized. Why is this omitted by both plugins?

    Now when i deactivate Autoptimize AND Async JavaScript, everything works fine. As soon as one of the two plugins is activated it does not work again (with other errors).

    Thank you in advance!

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 28 total)
  • Optimizing Matters

    (@optimizingmatters)

    make sure wp-includes/js is excluded in both AsyncJS & Autoptimize and all should be fine 🙂

    frank (ao dev)

    Thread Starter playalistic_p

    (@playalistic_p)

    Did so and now i have 11 errors:

    Uncaught TypeError: Cannot read properties of undefined (reading '...')

    Optimizing Matters

    (@optimizingmatters)

    try with just Autoptimize (or just AsyncJS) to make sure which one is causing that, based on that info we can see what the next steps would need to be (tomorrow though, I’m off to bed now 😉 )

    Thread Starter playalistic_p

    (@playalistic_p)

    Excluded only in Autoptimize:
    No content is output. The following errors appear in the console:

    1 x Uncaught ReferenceError: wp is not defined
    12 x Uncaught TypeError: Cannot read properties of undefined (reading '...')

    Excluded only in AsyncJS:
    Skeleton loaders are shown, but never get replaced with the actual content. The following errors appear in the console:

    1 x Uncaught ReferenceError: wp is not defined
    2 x GET https://bortec-tuning.de/wc/store?_locale=user 404
    2 x Uncaught (in promise) {code: 'invalid_json', message: 'The response is not a valid json response.'} (message translated by me)

    Excluded in both:
    Like Autoptimize

    Excluded in none:
    Like AsyncJS

    Optimizing Matters

    (@optimizingmatters)

    OK, can you keep AsyncJS off for now and in Autoptimize try activating “don’t aggregate but defer” + “also defer inline JS” and remove all default exclusions (keep them copy/pasted somewhere for later referral) ?

    Thread Starter playalistic_p

    (@playalistic_p)

    Deactivated AsyncJS and set both options in Autoptimize. There where no exclusions set, so nothing to remove here.

    Still the same errors.

    Thread Starter playalistic_p

    (@playalistic_p)

    Maybe i’m onto something.

    wp-includes/script-loader.php, on line 291, there is the following function.

    function wp_default_packages_inline_scripts( $scripts ) {
    	global $wp_locale;
    
    	if ( isset( $scripts->registered['wp-api-fetch'] ) ) {
    		$scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
    	}
    	$scripts->add_inline_script(
    		'wp-api-fetch',
    		sprintf(
    			'wp.apiFetch.use( wp.apiFetch.createRootURLMiddleware( "%s" ) );',
    			esc_url_raw( get_rest_url() )
    		),
    		'after'
    	);
    	$scripts->add_inline_script(
    		'wp-api-fetch',
    		implode(
    			"\n",
    			array(
    				sprintf(
    					'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
    					( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' )
    				),
    				'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );',
    				'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
    				sprintf(
    					'wp.apiFetch.nonceEndpoint = "%s";',
    					admin_url( 'admin-ajax.php?action=rest-nonce' )
    				),
    			)
    		),
    		'after'
    	);
    	[...]

    If i comment out the second call to “add_inline_script” then of course the script only consists of the one line from the first call. But then everything works fine again. Activated AsyncJS again and now the script has the defer attribute set as i would expect it. In Autoptimize “aggregate JS files” and “also aggregate inline JS” are set as it was before. Still working.

    So what is wrong with this piece of code?

    Thread Starter playalistic_p

    (@playalistic_p)

    Did further testing with the wp-includes/script-loader.php.

    As stated previously, this is the original code which gives me “Uncaught ReferenceError: wp is not defined” and other errors:

    $scripts->add_inline_script(
    	'wp-api-fetch',
    	implode(
    		"\n",
    		array(
    			sprintf(
    				'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
    				( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' )
    			),
    			'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );',
    			'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
    			sprintf(
    				'wp.apiFetch.nonceEndpoint = "%s";',
    				admin_url( 'admin-ajax.php?action=rest-nonce' )
    			),
    		)
    	),
    	'after'
    );

    This modified code works fine, just removed the enclosing “implode(“\n”)”:

    $scripts->add_inline_script(
    	'wp-api-fetch',
    	array(
    		sprintf(
    			'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
    			( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' )
    		),
    		'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );',
    		'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
    		sprintf(
    			'wp.apiFetch.nonceEndpoint = "%s";',
    			admin_url( 'admin-ajax.php?action=rest-nonce' )
    		),
    	),
    	'after'
    );
    Optimizing Matters

    (@optimizingmatters)

    hmm … we’ll need to take a step back here;
    1. does all work when AsyncJS is off and Autoptimize’s “optimize JS” is off?
    2. does all work when AsyncJS is off and Autoptimize’s “optimize JS” is on but “aggregate JS” is off?

    Thread Starter playalistic_p

    (@playalistic_p)

    AsyncJS off
    Autoptimize on
    Optimize JS off
    Aggregate JS off
    Aggregate inline JS off
    WORKS!

    AsyncJS off
    Autoptimize on
    Optimize JS on
    Aggregate JS off
    Aggregate inline JS off
    WORKS!

    AsyncJS off
    Autoptimize on
    Optimize JS on
    Aggregate JS on
    Aggregate inline JS off
    FAILS! Many “Uncaught ReferenceError”s

    AsyncJS off
    Autoptimize on
    Optimize JS on
    Aggregate JS on
    Aggregate inline JS on
    FAILS! Single “Uncaught ReferenceError” as in first post

    con

    (@conschneider)

    Engineer

    Howdy @playalistic_p,

    You could try and employ https://wordpress.org/plugins/plugin-organizer/ to change the loading order. Try loading Autoptimize either later or earlier in an attempt to dodge the Uncaught Ref errors.

    Kind regards,

    Thread Starter playalistic_p

    (@playalistic_p)

    Tried Autoptimize on the very first and last postion, still the same errors.

    Optimizing Matters

    (@optimizingmatters)

    OK, can you now (re-)try

    AsyncJS off
    Autoptimize on
    Optimize JS on
    Aggregate JS on
    Aggregate inline JS off

    but with the following in the comma-separated JS optimization exclusion list:
    wp-includes/js/

    Thread Starter playalistic_p

    (@playalistic_p)

    Works!

    But fails with AsyncJS turned on, even with exclusion of wp-includes/js/.

    Optimizing Matters

    (@optimizingmatters)

    OK, we’re getting closer 🙂

    Now; do you _need_ asyncjs? what in the performance of the site with AO needs to be improved?

Viewing 15 replies - 1 through 15 (of 28 total)

The topic ‘Uncaught ReferenceError: wp is not defined’ is closed to new replies.