Why is action customize_register being done twice?

I think the customizer creates an xhr request for to load the site preview( see devtools under the network > xhr tab ). In my case I want to check if a certain variable isset via the $_GET[‘foo’] variable. My function must be called once and not twice otherwise it will override my previous state.

My solution is to check if the $_POST[‘wp_customize’] it’s empty or not. If empty that means that my function doesn’t get called twice by preview request.

I use the following hook/action customize_loaded_components

Example:

function wpdocs_remove_widgets_panel( $components ) {
    if( empty( $_POST['wp_customize'] ) ) {
       // Do stuff here that will not be loaded twice by the previewer
    }
    return $components;
}

add_filter( 'customize_loaded_components', 'wpdocs_remove_widgets_panel' );