Skip to content

JS and CSS should load only on the pages where donations form appear #5772

Open
@aurelienpierre

Description

@aurelienpierre

Details

Google Page Speed really doesn't like the 250 kB or so of JS, CSS, Polyfill.js etc. (not counting addons) that load on every page of the website as soon as you enable Give, even on pages where no forms are present. Right now, scripts are loaded in a flat way through Give_Scripts::init().

Additional Context

https://developers.google.com/web/updates/2018/07/search-ads-speed

Acceptance Criteria

  • The statics should load only where relevant.

Hints

There are 2 ways it can be done :

  • calling Give_Scripts::public_enqueue_styles() and Give_Scripts::public_enqueue_scripts() inside the shortcodes and widget functions, rather than in Give_Script::init(),
  • filter the page content after shortcodes have rendered, to search for Give objects, like :
add_filter('the_content', 'enqueue_give_statics', 20); // shortcodes render at priority 8
function enqueue_give_statics( $content ) {
  if(preg_match('/class=(\").*?wp-give-form.*?\1/s', $content))
  {
    Give_Scripts::public_enqueue_scripts();
    Give_Scripts::public_enqueue_styles()
  }
  return $content;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    keep-fresh"Keep Fresh" issues should not be marked as stale.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions