Skip to content
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

SEO - description audit #3227

Merged
merged 7 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lighthouse-core/audits/seo/meta-description.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const Audit = require('../audit');

class Description extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'Content Best Practices',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meta question for the LH team. category here is referring to the title of the audit group, not to be confused with the top-level audit category, SEO, defined in the config file?

https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/config/seo.js#L25-L27

Copy link
Collaborator

@patrickhulce patrickhulce Sep 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rviscomi yeah the field here has an ambiguous name and is not used anywhere anymore 😆

I'm in favor of removing entirely now that we have real categories that control quite a bit of report building (just as cleanup work by the LH team in other PRs, it's a mandatory field currently).

name: 'meta-description',
description: 'Document has a meta description',
failureDescription: 'Document does not have a meta description',
helpText: 'Meta descriptions may be included in search results to concisely summarize ' +
'page content. Read more in the ' +
'[Search Console Help page](https://support.google.com/webmasters/answer/35624?hl=en#1).',
requiredArtifacts: ['MetaDescription']
};
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.MetaDescription === null) {
return {
rawValue: false
};
}

if (artifacts.MetaDescription.trim().length === 0) {
return {
rawValue: false,
debugString: 'Description text is empty.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Seems okay to keep this, as it's an unexpected case that'd otherwise be hard to debug.

};
}

return {
rawValue: true
};
}
}

module.exports = Description;
12 changes: 11 additions & 1 deletion lighthouse-core/config/seo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

module.exports = {
extends: 'lighthouse:default',
passes: [{
passName: 'defaultPass',
gatherers: [
'seo/meta-description',
]
}],
audits: [
'seo/meta-description',
],
groups: {
'seo-mobile': {
title: 'Mobile Friendly',
Expand All @@ -28,7 +37,8 @@ module.exports = {
description: 'These ensure your app is able to be understood by search engine crawlers.',
audits: [
{id: 'meta-viewport', weight: 1, group: 'seo-mobile'},
{id: 'document-title', weight: 1, group: 'seo-content'}
{id: 'document-title', weight: 1, group: 'seo-content'},
{id: 'meta-description', weight: 1, group: 'seo-content'},
]
}
}
Expand Down
25 changes: 25 additions & 0 deletions lighthouse-core/gather/gatherers/seo/meta-description.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const Gatherer = require('../gatherer');

class MetaDescription extends Gatherer {

/**
* @param {{driver: !Object}} options Run options
* @return {!Promise<?string>} The value of the description meta's content attribute, or null
*/
afterPass(options) {
const driver = options.driver;

return driver.querySelector('head meta[name="description"]')
.then(node => node && node.getAttribute('content'));
}
}

module.exports = MetaDescription;

2 changes: 2 additions & 0 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Runner {
const fileList = [
...fs.readdirSync(path.join(__dirname, './audits')),
...fs.readdirSync(path.join(__dirname, './audits/dobetterweb')).map(f => `dobetterweb/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/seo')).map(f => `seo/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/accessibility'))
.map(f => `accessibility/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/byte-efficiency'))
Expand All @@ -245,6 +246,7 @@ class Runner {
static getGathererList() {
const fileList = [
...fs.readdirSync(path.join(__dirname, './gather/gatherers')),
...fs.readdirSync(path.join(__dirname, './gather/gatherers/seo')).map(f => `seo/${f}`),
...fs.readdirSync(path.join(__dirname, './gather/gatherers/dobetterweb'))
.map(f => `dobetterweb/${f}`)
];
Expand Down
42 changes: 42 additions & 0 deletions lighthouse-core/test/audits/seo/meta-description-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const Audit = require('../../../audits/seo/meta-description.js');
const assert = require('assert');

/* eslint-env mocha */

describe('SEO: description audit', () => {
it('fails when HTML does not contain a description meta tag', () => {
const auditResult = Audit.audit({
MetaDescription: null
});
assert.equal(auditResult.rawValue, false);
});

it('fails when HTML contains an empty description meta tag', () => {
const auditResult = Audit.audit({
MetaDescription: ''
});
assert.equal(auditResult.rawValue, false);
assert.ok(auditResult.debugString.includes('empty'), auditResult.debugString);
});

it('fails when description consists only of whitespace', () => {
const auditResult = Audit.audit({
MetaDescription: '  '
});
assert.equal(auditResult.rawValue, false);
assert.ok(auditResult.debugString.includes('empty'), auditResult.debugString);
});

it('passes when a description text is provided', () => {
return assert.equal(Audit.audit({
MetaDescription: 'description text'
}).rawValue, true);
});
});
34 changes: 34 additions & 0 deletions lighthouse-core/test/gather/gatherers/seo/meta-description-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

/* eslint-env mocha */

const MetaDescriptionGather = require('../../../../gather/gatherers/seo/meta-description');
const assert = require('assert');
const EXAMPLE_DESCRIPTION = 'description text';
let metaDescriptionGather;

describe('Meta description gatherer', () => {
// Reset the Gatherer before each test.
beforeEach(() => {
metaDescriptionGather = new MetaDescriptionGather();
});

it('returns an artifact', () => {
return metaDescriptionGather.afterPass({
driver: {
querySelector() {
return Promise.resolve({
getAttribute: () => EXAMPLE_DESCRIPTION
});
}
}
}).then(artifact => {
assert.equal(artifact, EXAMPLE_DESCRIPTION);
});
});
});