New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LazyLoad for CSS background images triggers DOMException errors when the CSS is coming from a PHP file #6389
Comments
Reproduce the problemLike explain in the issue itself, <div class="my-test-background-1"></div> Creating a <?php
header('Content-Type: text/css');
echo '.my-test-background-1 {
width: 500px;
height: 500px;
margin-left: auto;
margin-right: auto;
background-image: url("http://wp-rocket.esy.es/wp-content/uploads/2020/10/rose-165819_1920-1024x678.jpg");
}'; And adding this to wp_enqueue_block_style(
'test',
array(
'handle' => 'my-test-background-1',
'src' => get_home_url( null,'wp-content/rocket-test-data/styles/test.php' ),
'path' => get_home_url( null,'wp-content/rocket-test-data/styles/test.php' )
)
); We get the console error as mentioned. Identify the root causeThe issue is that when we are extracting the content of the file, we keep the whole php content. Which means we are keeping the Scope a solutionTo solve the issue, we could modify the function
protected function fetch_content( string $path ) {
if ( ! wp_http_validate_url( $path ) ) {
$file_extension = pathinfo($path, PATHINFO_EXTENSION);
if (strtolower($file_extension) !== 'php') {
return $this->filesystem->get_contents($path);
}
}
return wp_remote_retrieve_body( wp_remote_get( $path ) );
} would work. Development steps:Effort estimation:XS Is a refactor needed in that part of the codebase?No |
@Miraeld This solution would work fine if we had only
or
to say the least, this kind of construct will trigger yet another console error. |
I thought about that, but I wan't able to think of a global solution for this kind of thing except playing with regex ... If you have an idea in mind, please share it |
@wp-media/engineering-plugin-team I see here that we will get content here when the css url does not contain a host, so for this maybe we can do a check for a php ext and do a request with a constructed url to get the file buffer instead of the content. |
Based on @jeawhanlee comment, here is what we could do: In the function
we can add a condition to check if this is a php file, and if this is, then we return the content of the loaded php: protected function fetch_content( string $path ) {
if ( ! wp_http_validate_url( $path ) ) {
$file_extension = pathinfo($path, PATHINFO_EXTENSION);
if (strtolower($file_extension) !== 'php') {
return $this->filesystem->get_contents($path);
}
}
return wp_remote_retrieve_body( wp_remote_get( $path ) );
} This would work. I have updated the grooming accordingly |
Looks good to me |
Before submitting an issue please check that you’ve completed the following steps:
Describe the bug
When a stylesheet is added from a PHP file, like this:
The following error is thrown:
DOMException: Failed to execute 'querySelectorAll' on 'Document': 'echo '.my-test-background-1' is not a valid selector.
It seems that the feature is getting the wrong selector, including the "echo '" in the PHP file.
Causing the
querySelectorAll
function to fail.This error prevents all the Background images to be loaded, not only those coming from the PHP file.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The feature should get the correct selector to prevent this issue.
Acceptance Criteria (for WP Media team use only)
Clear instructions for developers, to be added before the grooming
The text was updated successfully, but these errors were encountered: