Wp-rocket: Page integrity is not checked when putting buffer to cache file

Created on 5 Aug 2019  ·  4Comments  ·  Source: wp-media/wp-rocket

Describe the bug
My site has some 300K pages, it is very old, and without cache page load is very slow. So, I noticed that sometimes pages are not loaded to cache completely. This means that html in cached file is broken (half of page is missing). I think that due to some timeout or when the page load is extremely slow, page is not loaded completely. And this may happen, that such page version can go to cache.
Apart from load speed problems on my site, the problem is, that page integrity in buffer is never checked.

Here is part of your function maybe_process_buffer from class-cache.php

public function maybe_process_buffer( $buffer ) {
        if ( ! $this->tests->can_process_buffer( $buffer ) ) {
            $this->log_last_test_error();
            return $buffer;
        }

        $footprint = '';
        $is_html   = $this->is_html( $buffer );

        if ( ! static::can_generate_caching_files() ) {
            // Not allowed to generate cache files.
            if ( $is_html ) {
                $footprint = $this->get_rocket_footprint();
            }

            $this->log(
                'Page not cached by filter.',
                [
                    'filter' => 'do_rocket_generate_caching_files',
                ]
            );
            return $buffer . $footprint;
        }

        $cache_filepath = $this->get_cache_path();
        $cache_dir_path = dirname( $cache_filepath );

        // Create cache folders.
        rocket_mkdir_p( $cache_dir_path );

        if ( $is_html ) {
            $footprint = $this->get_rocket_footprint( time() );
        }

        // Save the cache file.
        rocket_put_content( $cache_filepath, $buffer . $footprint );
               //... rest of code
}

$is_html reflects html integrity but is not used to stop caching if there is no closing tag.

Expected behavior
You could prevent uncomplete $buffer from caching as following:

public function maybe_process_buffer( $buffer ) {
        if ( ! $this->tests->can_process_buffer( $buffer ) ) {
            $this->log_last_test_error();
            return $buffer;
        }

        $footprint = '';
        $is_html   = $this->is_html( $buffer );

        //TODO: is_html() check fix
                if ( ! $is_html ) {
                     return $buffer;
                }

               //... rest of code
}

I tried this solution on my site, and now I am getting no more broken pages.
Of course, there could be more elegant solution, because you know your code better than I do.

cache enhancement

Most helpful comment

@arunbasillal This has not been fixed properly, please read on :)

Your recent implemented fix, reports issues of missing ,,wp_footer() to the admin user.
This is perfect for informing about theme issues, prohibiting WP Rocket to function normally.

But the issue @leurdo experienced, which we are also experiencing on multiple customers is much more important.

During a cache load, somehow WP Rocket/WP is interrupted, but WP Rocket still saves the cache.
This provokes the "White page bug", delivered to all visitor until the cache runs out.

We are implementing @leurdo 's fix on every customer to prevent this..
We have been looking into error_logs etc. but are unable to detect the problem in any themes of plugins. The problem has been reported by 5 customers so far.

All 4 comments

That's interesting, I don't think it's something that came up before.

There is a reason we don't stop caching if there is no closing </html> tag, and that is because it's possible to cache the REST API responses with the plugin, which are in JSON format.

But there could still be some additional checks to make sure the whole HTML content is there when caching a normal page.

@Tabrisrp Now I understand, why there was no such check, thank you.

It woud be nice to have some solution before next update, or I will get my bug back :)

@arunbasillal This has not been fixed properly, please read on :)

Your recent implemented fix, reports issues of missing ,,wp_footer() to the admin user.
This is perfect for informing about theme issues, prohibiting WP Rocket to function normally.

But the issue @leurdo experienced, which we are also experiencing on multiple customers is much more important.

During a cache load, somehow WP Rocket/WP is interrupted, but WP Rocket still saves the cache.
This provokes the "White page bug", delivered to all visitor until the cache runs out.

We are implementing @leurdo 's fix on every customer to prevent this..
We have been looking into error_logs etc. but are unable to detect the problem in any themes of plugins. The problem has been reported by 5 customers so far.

@JeppeKr Thanks for the feedback. This issue isn't closed yet as you can see. I will look into this and consider this for an upcoming major version :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webtrainingwheels picture webtrainingwheels  ·  5Comments

NataliaDrause picture NataliaDrause  ·  4Comments

aniatanasova picture aniatanasova  ·  5Comments

piotrbak picture piotrbak  ·  3Comments

webtrainingwheels picture webtrainingwheels  ·  5Comments