Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance: add frontend variables only when there is a block that needs them #3019

Merged
merged 7 commits into from
Jan 17, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function __construct() {

// Adds a special class to the body tag, to indicate we can now run animations.
add_action( 'wp_footer', array( $this, 'init_animation' ) );
add_action( 'wp_footer', array( $this, 'init_stackable_vars' ) );

// Add the fallback values for the default block width and wide block width.
// These are used for the inside "Content width" option of Columns.
Expand Down Expand Up @@ -101,12 +102,6 @@ public function register_frontend_assets() {
wp_register_script( 'ugb-block-frontend-js', null, [], STACKABLE_VERSION );
}

$args = apply_filters( 'stackable_localize_frontend_script', array(
'restUrl' => get_rest_url(),
'i18n' => array(), // Translatable labels used in the frontend should go here.
) );
wp_localize_script( 'ugb-block-frontend-js', 'stackable', $args );

// Register inline frontend styles, these are always loaded.
// Register via a dummy style.
wp_register_style( 'ugb-style-css-nodep', false );
Expand Down Expand Up @@ -455,6 +450,14 @@ public function load_plugin_textdomain() {
public function init_animation() {
echo '<script>requestAnimationFrame(() => document.body.classList.add( "stk--anim-init" ))</script>';
}

// TODO: make this dynamic
public function init_stackable_vars() {
$args = apply_filters( 'stackable_localize_frontend_script', array() );
if ( array_key_exists( 'restUrl', $args ) ) {
echo '<script> stackable = {"restUrl": "' . $args["restUrl"] .'"} </script>';
Copy link
Contributor

@bfintal bfintal Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of writing the restUrl manually, please convert $args to JSON instead to print out the entire $args if it has contents

}
}
}

new Stackable_init();
Expand Down
Loading