Make WordPress Core

Opened 3 years ago

Last modified 8 weeks ago

#53638 reopened defect (bug)

Duplicate inline JS

Reported by: malthert's profile malthert Owned by:
Milestone: Priority: normal
Severity: minor Version: 5.8
Component: Script Loader Keywords:
Focuses: Cc:

Description

in /wp-includes/class.wp-scripts.php

public function __construct() {
    $this->init();
    add_action( 'init', array( $this, 'init' ), 0 );
}

calls the init twice which leads to include the script twice as action

do_action_ref_array( 'wp_default_scripts', array( &$this ) );

Which then ends up with

<script id='lodash-js-after'>
window.lodash = _.noConflict();
window.lodash = _.noConflict();
</script>

on frontend.

This affects all scripts that use add inline script, basically bloating the frontend.

Change History (6)

#1 @malthert
3 years ago

Update: it's not only bloating the frontend, in some cases (like the above example) it will cause js console errors making the site unusable

#2 @desrosj
3 years ago

  • Keywords reporter-feedback added

Hi @malthert,

I have not been able to reproduce the behavior you are describing.

Do you have any plugins active? Are you testing on a default theme? Are there any steps someone needs to take in order to see this happening?

#3 @SergeyBiryukov
3 years ago

This could probably use a comment explaining the reason for double initialization.

Some history here:

#4 @malthert
3 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Issue fixed itself now. Problem was something else, but I can't remember what exactly.

#5 @desrosj
3 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution changed from invalid to worksforme

#6 @kkmuffme
8 weeks ago

  • Resolution worksforme deleted
  • Severity changed from blocker to minor
  • Status changed from closed to reopened

This happens when registering/enqueuing a script on a hook before init OR on init with priority < 0

There currently is a "doing_it_wrong" when using any hook before init already, however this doing_it_wrong won't trigger if a script is enqueued on init with priority < 0
(e.g. add_action( 'init', 'foo', -10 );)

Note: See TracTickets for help on using tickets.