Comments (6)
Hello again.
I can't see anything wrong with your code usage. My best guess would be that one of the rules in one of your CSS files is missing a closing bracket (}
), causing rules that follow it to be swallowed in the parsing step in Penthouse. Given what you've said, it would most likely be a media query that is missing the closing bracket. Can you double check in these files? If you want, you can review how the CSS parser penthouse uses (csstree) sees your CSS files by:
- go to https://astexplorer.net/
- change language from
JavaScript
toCSS
- change parser from
cssom
tocsstree
- paste in your css content in the left side
If all rules in your css files are properly closed then I'm afraid I don't know what's going wrong. To debug further I would need a reproducible test case (with a URL I can test with).
from penthouse.
Hello, thanks for the kind words!
Are using Penthouse directly, i.e. merging your two css sources and passing them in as a cssString? Or are you using another tool?
If you are using penthouse directly, this sounds like a bug. You should not have to move the media queries for it to work without glitches.
To investigate what happens I would need to have a reproducible test case - or at least the URL this happens on.
from penthouse.
@pocketjoso according to what you mention, I seem to be using Penthouse directly. I went in and downloaded the two stylesheets manually, and then passed them in as cssFiles
ie.
cssFiles: [
'./6f823b0b21dbc62e6df437891dca1004.min.css',
'./styles-l.min.css'
],
.. in the same order as they are presented in the website.
Unfortunately I can't provide you with the URL this is happening on as it's a commercial project, but hopefully the above is enough to attempt to reproduce?
from penthouse.
Hello again.
Penthouse doesn't have any option for called cssFiles
, so not sure you are using it directly. Can you share the full code when you call it, with all options? You can obscure the URL if you cannot share it.
from penthouse.
@pocketjoso apologies, cssFiles
was a customisation in my project. Yes, I am using cssString
and concatenating 2 different files into a single string.
This is an extract of the config:
penthouseOptions = {
cssString: '',
blockJSRequests: false,
renderWaitTime: 15000,
timeout: (urls.length * 90000),
forceInclude: opts.selectors.include,
forceExclude: opts.selectors.exclude
};
if (opts.height){
penthouseOptions.height = opts.height;
}
if (opts.width){
penthouseOptions.width = opts.width;
}
As an example, since I'm using a Magento2 codebase, you could try a live demo: https://demo-m2.bird.eu/
--
Full code below:
const penthouse = require('penthouse');
const fs = require('fs');
const path = require('path');
const dirname = path.resolve(__dirname);
var urls,
filename,
penthouseOptions;
function init(opts){
urls = opts.urls;
filename = opts.filename;
setPenthouseOptions(opts);
getCSSContents(opts.cssFiles);
}
function setPenthouseOptions(opts){
penthouseOptions = {
cssString: '',
blockJSRequests: false,
renderWaitTime: 15000,
timeout: (urls.length * 90000),
forceInclude: opts.selectors.include,
forceExclude: opts.selectors.exclude
};
if (opts.height){
penthouseOptions.height = opts.height;
}
if (opts.width){
penthouseOptions.width = opts.width;
}
}
function getCSSContents(cssFiles){
var processed = 0;
for (var i = 0; i < cssFiles.length; i++) {
fs.readFile(dirname + '/css-source/' + cssFiles[i], 'utf8' , (err, data) => {
if (err) {
console.error(err)
return
}
processed++;
penthouseOptions.cssString += data;
if (processed === cssFiles.length){
startExtraction();
}
})
}
}
function startExtraction(){
Promise.all([
startNewJob()
]).then(() => {
console.log('Extraction finished.')
})
}
function startNewJob () {
const url = urls.pop()
if (!url) {
return Promise.resolve()
}
penthouseOptions.screenshots = {
basePath: dirname + '/generated/' + filename + '-screenshot-' + (urls.length + 1),
type: 'jpeg',
quality: 20
}
console.log(filename + '-' + (urls.length + 1) + ': ' + url);
return penthouse({
url,
...penthouseOptions
})
.then(criticalCss => {
fs.writeFileSync(dirname + '/generated/' + filename + '-extraction-' + (urls.length + 1) + '.css', criticalCss);
return startNewJob()
})
}
module.exports = init;
and
const extraction = require('./_extraction.js')
const path = require('path')
const opts = {
filename: path.basename(__filename).replace('.js', ''),
urls: [
'removed.com'
],
cssFiles: [
'./1592ed3f401e117a96d461d05abc6bfb.min.css',
'./styles-l.min.css'
],
selectors: { // regex accepted
include: [
/.*\.icon.*/
],
exclude: [
/.*\.catalog-category-view.*/ // as we're using a PLP URL
]
}
}
extraction(opts);
from penthouse.
Closing due to inactivity. If you have an update regarding the issue, just post it and we can re-open the issue.
from penthouse.
Related Issues (20)
- Cannot read property 'width' of null
- The media query `@media screen and (min-device-width: 20cm) and (min-aspect-ratio: 1/1)` is not inserted in the generated .ccs file. HOT 2
- Error: Failed to launch the browser process! on travis CI HOT 1
- why it crops critical css generated file? HOT 3
- What is CSSstring HOT 5
- Page speed insights CLS time on desktop get increased after resolving render blocking errors HOT 1
- Chrome processes HOT 4
- Output Wrong CSS HOT 1
- not finishing in Docker environment HOT 1
- Puppeteer Update HOT 3
- TypeError: Cannot read property 'clear' of undefined HOT 2
- Navigation timeout infinitely
- bug(iframes): timed out if lazy loaded iframes HOT 2
- Display none elements do not get added to critical
- Puppeteer screenshot works, but not penthouse HOT 2
- forceExclude is ignored / not working
- CSS selectors like `:where()` are not included in generated critical css HOT 1
- The generated CSS is too big and include many unused CSS
- Configuration to extract below the fold critical CSS
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from penthouse.