diff --git a/lib/index.js b/lib/index.js
index 68bf50c..8285f33 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -33,10 +33,11 @@ var yellowLabTools = function(url, options) {
// If a screenshot saveFunction was provided in the options
if (options && typeof options.saveScreenshotFn === 'function') {
+ const screenshotTmpPath = data.params.options.screenshot;
debug('Now optimizing screenshot...');
// TODO: temporarily set all screenshot sizes to 600px, until we find a solution
- ScreenshotHandler.findAndOptimizeScreenshot(data.params.options.screenshot, 600)
+ ScreenshotHandler.findAndOptimizeScreenshot(screenshotTmpPath, 600)
.then(function(screenshotBuffer) {
debug('Screenshot optimized, now saving...');
@@ -50,6 +51,7 @@ var yellowLabTools = function(url, options) {
// Remove uneeded temp screenshot path
delete data.params.options.screenshot;
+ return ScreenshotHandler.deleteTmpFile(screenshotTmpPath);
})
.catch(function(err) {
diff --git a/lib/metadata/policies.js b/lib/metadata/policies.js
index 3157fb1..7214982 100644
--- a/lib/metadata/policies.js
+++ b/lib/metadata/policies.js
@@ -729,7 +729,7 @@ var policies = {
"totalWeight": {
"tool": "redownload",
"label": "Total weight",
- "message": "
The weight is of course very important if you want the page to load fast. Try to stay under 1MB, which is already very long to download over a slow connection.
",
+ "message": "
The weight is of course very important if you want the page to load fast. Try to stay under 1.5MB.
Measures the number of bytes that could be saved by converting images to newer and more efficient formats. The best image format is generally AVIF and the second best is WebP.
Be careful, you need to provide fallback images for old browsers and search engine bots.
",
+ "isOkThreshold": 30720,
+ "isBadThreshold": 307200,
+ "isAbnormalThreshold": 512000,
+ "hasOffenders": true,
+ "unit": 'bytes'
+ },
"imagesTooLarge": {
"tool": "redownload",
"label": "Oversized images",
diff --git a/lib/metadata/scoreProfileGeneric.json b/lib/metadata/scoreProfileGeneric.json
index 8bff9e4..4ecdc42 100644
--- a/lib/metadata/scoreProfileGeneric.json
+++ b/lib/metadata/scoreProfileGeneric.json
@@ -5,6 +5,7 @@
"policies": {
"totalWeight": 5,
"imageOptimization": 2,
+ "oldImageFormats": 2,
"imagesTooLarge": 1,
"compression": 2,
"fileMinification": 2
diff --git a/lib/screenshotHandler.js b/lib/screenshotHandler.js
index 3aab753..c175fd1 100644
--- a/lib/screenshotHandler.js
+++ b/lib/screenshotHandler.js
@@ -1,90 +1,19 @@
-var debug = require('debug')('ylt:screenshotHandlerAgent');
-var Jimp = require('jimp');
+var debug = require('debug')('ylt:screenshotHandler');
+var sharp = require('sharp');
var Q = require('q');
var fs = require('fs');
var path = require('path');
+// Disable sharp cache to reduce the "disk is full" error on Amazon Lambda
+sharp.cache(false);
var screenshotHandler = function() {
- this.findAndOptimizeScreenshot = function(tmpScreenshotPath, width) {
- var that = this;
-
- debug('Starting screenshot transformation');
-
- return this.openImage(tmpScreenshotPath)
-
- .then(function(image) {
- that.deleteTmpFile(tmpScreenshotPath);
- return that.resizeImage(image, width);
- })
-
- .then(this.toBuffer);
- };
-
-
- this.openImage = function(imagePath) {
- var deferred = Q.defer();
-
- Jimp.read(imagePath, function(err, image){
- if (err) {
- debug('Could not open imagePath %s', imagePath);
- debug(err);
-
- deferred.reject(err);
- } else {
- debug('Image correctly open');
- deferred.resolve(image);
- }
- });
-
- return deferred.promise;
- };
-
-
- this.resizeImage = function(image, newWidth) {
- var deferred = Q.defer();
-
- var currentWidth = image.bitmap.width;
-
- if (currentWidth > 0) {
- var ratio = newWidth / currentWidth;
-
- image.scale(ratio, function(err, image){
- if (err) {
- debug('Could not resize image');
- debug(err);
-
- deferred.reject(err);
- } else {
- debug('Image correctly resized');
- deferred.resolve(image);
- }
- });
- } else {
- deferred.reject('Could not resize an empty image');
- }
-
- return deferred.promise;
- };
-
-
- this.toBuffer = function(image) {
- var deferred = Q.defer();
-
- image.quality(85).getBuffer(Jimp.MIME_JPEG, function(err, buffer){
- if (err) {
- debug('Could not save image to buffer');
- debug(err);
-
- deferred.reject(err);
- } else {
- debug('Image correctly transformed to buffer');
- deferred.resolve(buffer);
- }
- });
-
- return deferred.promise;
+ this.findAndOptimizeScreenshot = async function(tmpScreenshotPath, width) {
+ return sharp(tmpScreenshotPath)
+ .resize({width: 600})
+ .jpeg({quality: 85})
+ .toBuffer();
};
diff --git a/lib/tools/redownload/contentTypeChecker.js b/lib/tools/redownload/contentTypeChecker.js
index ea06d88..a37c7d5 100644
--- a/lib/tools/redownload/contentTypeChecker.js
+++ b/lib/tools/redownload/contentTypeChecker.js
@@ -1,20 +1,12 @@
-var debug = require('debug')('ylt:contentTypeChecker');
-var Q = require('q');
-var isJpg = require('is-jpg');
-var isPng = require('is-png');
-var isSvg = require('is-svg');
-var isGif = require('is-gif');
-var isWebp = require('is-webp');
-var isWoff = require('is-woff');
-var isWoff2 = require('is-woff2');
-var isOtf = require('is-otf');
-var isTtf = require('is-ttf');
-var isEot = require('is-eot');
-var isJson = require('is-json');
+var debug = require('debug')('ylt:contentTypeChecker');
+var Q = require('q');
+var FileType = require('file-type');
+var isSvg = require('is-svg');
+var isJson = require('is-json');
var ContentTypeChecker = function() {
- function checkContentType(entry) {
+ async function checkContentType(entry) {
var deferred = Q.defer();
// Setting isSomething values:
@@ -55,12 +47,12 @@ var ContentTypeChecker = function() {
var foundType;
try {
- foundType = findContentType(entry.weightCheck.bodyBuffer);
+ foundType = await findContentType(entry.weightCheck.bodyBuffer);
// If it's an image or a font, then rewrite.
if (foundType !== null && (foundType.type === 'image' || foundType.type === 'webfont' || foundType.type === 'json')) {
if (foundType.type !== entry.type) {
- debug('Content type %s is wrong for %s. It should be %s.', entry.type, entry.ulr, foundType.type);
+ debug('Content type %s is wrong for %s. It should be %s.', entry.type, entry.url, foundType.type);
}
rewriteContentType(entry, foundType);
}
@@ -76,54 +68,23 @@ var ContentTypeChecker = function() {
return deferred.promise;
}
- function findContentType(bodyBuffer) {
+ async function findContentType(bodyBuffer) {
var bodyStr = bodyBuffer.toString();
- if (isJpg(bodyBuffer)) {
- return contentTypes.jpeg;
- }
-
- if (isPng(bodyBuffer)) {
- return contentTypes.png;
- }
-
// https://github.com/sindresorhus/is-svg/issues/7
if (/