Menu Developer Moovweb University

perf Namespace

A namespace containing methods for performance optimizations.


.blacklistContains(domain)
moov_perf
Source (L181)

Checks whether the domain is blacklisted. Note that domains to check should not include any protocols or paths.

Parameters

Name Type Description
domain String A string for the domain to check.

Return Value

A boolean for whether the domain is in the blacklist.

Example

const Perf = require("moov_perf");
let perf = new Perf($);
perf.ignore("http://www.google.com/");
perf.blacklistContains("www.google.com");
// => Returns: true
perf.blacklistContains("http://www.google.com/");
// => Returns: false

Back to top


.ignore(domain)
moov_perf
Source (L456)

Add domain to the Perf instance's blacklist. Thus, when using any of the image optimization methods for a given Cheerio object selector, it will specifically avoid rewriting the image source for any domains included in the ignored list. This method will strip off the protocol, as well as any trailing path.

Parameters

Name Type Description
domain String A string for the domain to ignore.

Return Value

The original Perf instance.

Example

const Perf = require("moov_perf");
let perf = new Perf($);
perf.ignore("http://www.google.com/");
// => Returns: The Perf object instance assigned to `perf`

perf.ignore("http://www.bing.com");
perf.ignore("http://www.yahoo.com/image.jpg");
perf.ignored();
// => Returns: {
//      "www.google.com": true,
//      "www.bing.com": true,
//      "www.yahoo.com": true
//    }

Back to top


.ignored()
moov_perf
Source (L430)

View the Perf instance's blacklisted domains, which are the domains that do not get rewritten to reference Moovweb's image optimization domain. Domains added to this list via the `.ignore()` method will strip off the protocol and any trailing path.

Return Value

An object containing all domains currently being ignored.

Example

const Perf = require("moov_perf");
let perf = new Perf($);
perf.ignore("http://www.google.com/");
perf.ignore("http://www.bing.com");
perf.ignore("http://www.yahoo.com/image.jpg");
perf.ignored();
// => Returns: {
//      "www.google.com": true,
//      "www.bing.com": true,
//      "www.yahoo.com": true
//    }

Back to top


.optimizedAttrs(domain, [opts])
moov_perf
Source (L220)

Provides a first pass to convert an object containing options meant to be applied to an optimized image element (as passed into other public helper methods), including a newly-generated `src` attribute.

Parameters

Name Type Description
domain String A string for the image source.
[ opts ] Object Optional. An object containing optimization options. The default options object is `{ link_encoded: false, preserve_aspect: true }`, unless the host is blacklisted (in which case, the object only contains `bypass_attributes: true`, a reserved property-value pair for knowing not to set height or width HTML attributes). The usable attributes are as follows:
Properties
Name Type Description
[ bypass_attributes ] Boolean Optional. A boolean for whether attributes should be bypassed from the optimized image elemetnt.
[ width ] Number | String Optional. A number or a string containing the number for the desired pixel width.
[ height ] Number | String Optional. A number or string containing the number for the desired pixel height.
[ link_encoded ] Boolean Optional. A boolean for whether the link should be base64-encoded.
[ preserve_aspect ] Boolean Optional. A boolean for whether the aspect ratio should be preserved, provded that only one dimension is given. If both dimensions are given, width is given priority. If both dimensions are set, this setting is ignored.
[ quality ] Number | String Optional. A number or string containing the number for the desired quality, on a scale from 1 (worst) to 100 (best).
[ format ] String Optional. A string containing the desired file format. Options include jpg, png, and webp.

Return Value

A first-pass object containing attributes to apply to an image node.

Examples

const Perf = require("moov_perf");
let perf = new Perf($);
perf.ignore("www.moovweb.com");
perf.optimizedAttrs("http://www.moovweb.com/img.jpg", {height: 300});
Returns: { height: 300, bypass_attributes: true, src: 'http://www.moovweb.com/selfie.jpg' }
let perf = new Perf($);
perf.ignore("www.moovweb.com");
perf.optimizedAttrs("http://www.google.com/img.jpg", {height: 300});
Returns: { height: 300, bypass_attributes: true, src: 'http://www.moovweb.com/selfie.jpg' }

Back to top


.optimizeImageAttribs(node, [opts])
moov_perf
Source (L343)

Provides a second pass to convert an object containing options meant to be applied to an optimized image element. Note that this calls the "first-pass" `.optimizedAttrs()` method with the given settings object. This second pass most notably does the following two things: it allows for the use of `data-src` (in the case of a lazyloading usage), and it retains any width or height dimensions for the purpose HTML attribute usage.

Parameters

Name Type Description
node Object An image node element.
[ opts ] Object Optional. An object containing optimization options. The default is the list of attributes attached to the input node, along with the settings that come from the `.optimizedAttrs()` method (`{ link_encoded: false, preserve_aspect: true }`, unless the host is blacklisted, in which case, the object only contains `bypass_attributes: true`, a reserved property-value pair for knowing not to set height or width HTML attributes). The usable attributes (that get passed in to other Perf methods) are as follows:
Properties
Name Type Description
[ bypass_attributes ] Boolean Optional. A boolean for whether attributes should be bypassed from the optimized image elemetnt.
[ width ] Number | String Optional. A number or string containing the number for the desired pixel width.
[ height ] Number | String Optional. A number or string containing the number for the desired pixel height.
[ link_encoded ] Boolean Optional. A boolean for whether the link should be base64-encoded.
[ preserve_aspect ] Boolean Optional. A boolean for whether the aspect ratio should be preserved, provded that only one dimension is given. If both dimensions are given, width is given priority. If both dimensions are set, this setting is ignored.
[ quality ] Number | String Optional. A number or string containing the number for the desired quality, on a scale from 1 (worst) to 100 (best).
[ format ] String Optional. A string containing the desired file format. Options include jpg, png, and webp.

Return Value

  • A second-pass object containing attributes to apply to an image node.

  • An object containing the second-pass optimized form of attributes, including the original node attributes, with some or all that may be overridden by the input options, as well as support for `data-src` and dimension* attributes.

Examples

const Perf = require("moov_perf");
let perf = new Perf($);
let bingTag = tag("img", {src: "http://www.bing.com/selfie.jpg"});
console.log(perf.optimizeImageAttribs(bingTag, {height: 300}));
// => Returns: { src: '//opt.moovweb.net/img?img=http%3A%2F%2Fwww.bing.com%2Fselfie.jpg&linkEncoded=0&height=300&preserve_aspect=1&preserve_aspect=1', height: 300}
let perf = new Perf($);
let moovTag = tag("img", {src: "http://www.moovweb.com/selfie.jpg"});
perf.ignore("http://www.moovweb.com/");
console.log(perf.optimizeImageAttribs(moovTag, {height: 300}));
// => Returns: { src: 'http://www.moovweb.com/selfie.jpg', height: 300 }

Back to top


Last updated Tue Nov 15 2016 22:28:30 GMT+0000 (UTC)