Skip to content

Commit 5425471

Browse files
add preload API so that Boxzilla JS API can be used before Boxzilla script itself has loaded. This also allows us to defer the Boxzilla script itself, allowing for a nice performance improvement:
1 parent bb29e84 commit 5425471

7 files changed

Lines changed: 43 additions & 5 deletions

File tree

assets/js/preload.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/script.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/src/preload.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var boxzilla_queue = []
2+
3+
// create a temporary global Boxzilla object
4+
// this allows these methods to be called before the Boxzilla script itself has loaded
5+
var Boxzilla = {};
6+
['on', 'off', 'toggle', 'show'].forEach((m) => {
7+
Boxzilla[m] = function () {
8+
boxzilla_queue.push([m, arguments])
9+
}
10+
})
11+
12+
window.Boxzilla = Boxzilla
13+
window.boxzilla_queue = boxzilla_queue

assets/js/src/script.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@
125125
// init boxzilla
126126
Boxzilla.init()
127127

128-
// on window.load, create DOM elements for boxes
129-
window.addEventListener('load', createBoxesFromConfig)
128+
window.addEventListener('load', () => {
129+
// create JS objects for each box
130+
createBoxesFromConfig()
131+
132+
// fire all events queued up during DOM load
133+
window.boxzilla_queue.forEach((q) => {
134+
const [method, args] = q
135+
Boxzilla[method].apply(null, args)
136+
})
137+
})
130138
})()

boxzilla.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
defined('ABSPATH') or exit;
3131

3232
define( 'BOXZILLA_FILE', __FILE__ );
33+
define( 'BOXZILLA_DIR', __DIR__ );
3334
define( 'BOXZILLA_VERSION', '3.3.1' );
3435

3536
require __DIR__ . '/autoload.php';

src/class-loader.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,23 @@ public function init() {
3838
$this->box_ids_to_load = $this->filter_boxes();
3939

4040
// Only add other hooks if necessary
41+
add_action('wp_head', array($this, 'print_preload_js'));
4142
if ( count( $this->box_ids_to_load ) > 0 ) {
4243
add_action( 'wp_footer', array( $this, 'print_boxes_content' ), 1 );
4344
add_action( 'wp_enqueue_scripts', array( $this, 'load_assets' ), 90 );
4445
}
4546
}
4647

48+
/**
49+
* Prints the preload API so that the Boxzilla JS API can be used before Boxzilla itself is loaded
50+
* This allows us to defer the Boxzilla script itself.
51+
*/
52+
public function print_preload_js() {
53+
echo '<script>';
54+
echo file_get_contents(BOXZILLA_DIR . '/assets/js/preload.js');
55+
echo '</script>';
56+
}
57+
4758
/**
4859
* Get global rules for all boxes
4960
*
@@ -248,7 +259,10 @@ private function filter_boxes() {
248259
*/
249260
public function load_assets() {
250261
wp_enqueue_style( 'boxzilla', $this->plugin->url( '/assets/css/styles.css' ), array(), $this->plugin->version() );
251-
wp_enqueue_script( 'boxzilla', $this->plugin->url( '/assets/js/script.js' ), array(), $this->plugin->version(), true );
262+
wp_enqueue_script( 'boxzilla', $this->plugin->url( '/assets/js/script.js' ), array(), $this->plugin->version(), array(
263+
'strategy' => 'defer',
264+
'in_footer' => true,
265+
));
252266

253267
// create boxzilla_Global_Options object
254268
$plugin_options = $this->options;

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const path = require('path')
33
module.exports = {
44
entry: {
55
'admin-script': './assets/js/src/admin-script.js',
6-
script: './assets/js/src/script.js'
6+
script: './assets/js/src/script.js',
7+
preload: './assets/js/src/preload.js'
78
},
89
output: {
910
filename: '[name].js',

0 commit comments

Comments
 (0)