Skip to content

Blank page when using image lazy-loading on a page with a large script #421

Description

@siliconforks

Consider the following WordPress plugin:

<?php

/*
Plugin Name: Megabyte of Nonsense
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

add_action( 'wp_footer', static function () {
	$nonsense = [];
	for ( $i = 0; $i < 1024; ++$i ) {
		$random_bytes = random_bytes( 1024 );
		$nonsense[] = base64_encode( $random_bytes );
	}
	?>
	<script>
	var MEGABYTE_OF_NONSENSE = <?php echo json_encode( $nonsense, JSON_PRETTY_PRINT ) ?>;
	</script>
	<?php
} );

If you install the above plugin and you have image lazy-loading enabled in Autoptimize, you will get a blank page (assuming default PHP settings).

The reason this is happening is that in autoptimizeBase::replace_contents_with_marker_if_exists() there is a call to preg_replace_callback() which is failing and returning null. It is possible to fix this by increasing the value of the PHP setting pcre.backtrack_limit. The default value is 1000000; if you double this to 2000000 then the plugin above will work without causing a blank page.

You might argue that there is nothing that Autoptimize can do about this (since it is really an issue with the server PHP settings), but I think at least the error handling could be improved here. Right now it just produces a blank page with no error message, a 200 HTTP code and nothing at all in the site's error log. (Actually, if you have WP_DEBUG enabled you may get some messages in the error log about unexpected null values, but these are not very helpful since they occur long after the original problem of preg_replace_callback() failing.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions