The issue you’re experiencing with wp.data
being undefined for anonymous users after updating WooCommerce and using WP Rocket for caching, combined with the Woodmart theme, is indicative of a JavaScript loading or execution timing issue. This can happen when scripts that rely on wp.data
are executed before wp.data
itself is fully loaded, especially when JS loading optimizations like deferring or delaying execution are applied.
Given that disabling the defer and delay options in WP Rocket resolves the issue but adversely affects your site’s performance, here are some strategies to tackle the problem without sacrificing page speed:
1. Selective Script Exclusion from Defer/Delay
- Identify Critical Scripts: Determine which scripts are crucial for
wp.data
and ensure they are not deferred or delayed. You might need to manually exclude these specific scripts from WP Rocket’s optimization features.
- WP Rocket Exclusion: In WP Rocket, you can exclude specific JavaScript files from being deferred or delayed. This is done under the File Optimization tab, where you can specify scripts by their relative paths.
2. Async Loading with Dependency Management
- Ensure Dependencies Are Met: If possible, modify how scripts are enqueued in WordPress to ensure that they explicitly declare
wp.data
as a dependency. This might require custom code or a child theme to adjust the script enqueue functions.
3. Use of the rocket_delay_js_exclusions
Filter
- Custom Filter for Exclusions: WP Rocket allows you to exclude scripts from delay using the
rocket_delay_js_exclusions
filter. You can add a custom function to your theme’s functions.php
file or a site-specific plugin to exclude scripts related to WooCommerce and wp.data
.
4. Optimize Loading with Inline Scripts
- Inline Critical JS: For essential inline scripts that depend on
wp.data
, ensure they are placed in a way that they execute after wp.data
is available. This might involve moving inline scripts to the footer or after the WP Rocket script that initializes deferred scripts.
5. Contact Theme and Plugin Developers
- Report the Issue: Since this issue persists with just WooCommerce, Woodmart theme, and WP Rocket activated, reaching out to the developers with detailed information might prompt them to investigate potential compatibility issues or offer a patch/update.
6. Monitoring and Testing
- Use Browser DevTools: Utilize your browser’s developer tools to monitor the loading order of scripts. This can help identify if critical scripts are loading too late due to deferring or delay optimizations.
- Incremental Changes: Make one change at a time and test the site’s functionality and performance to pinpoint the most effective solution without compromising site speed.
7. Alternative Performance Optimization
- Reevaluate Caching and Optimization Settings: If specific exclusions don’t fully resolve the issue, consider adjusting other WP Rocket settings, like cache lifespan, file optimization levels, or using its Critical Path CSS generation feature to improve load times without affecting script execution.