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 5 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
52 changes: 52 additions & 0 deletions lighthouse-core/audits/seo/meta-description.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @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,
debugString: 'Description meta tag is missing.'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually now that you've brought it up in the other issue, I think I might have a slight preference to put these in the displayValue and keep debugString for errors like the gatherer failing (which is handled automatically)

Copy link
Member

Choose a reason for hiding this comment

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

Actually now that you've brought it up in the other issue, I think I might have a slight preference to put these in the displayValue and keep debugString for errors like the gatherer failing (which is handled automatically)

agree about confusion between user vs system errors in debugString, but don't we still want the red string for failure here? (We still use the guidance "Optional error string for helping the user figure out why they failed here." in our docs)

Copy link
Collaborator Author

@kdzwinel kdzwinel Sep 8, 2017

Choose a reason for hiding this comment

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

IMO debugString is more readable and, as @brendankenny mentioned, in this case it matches the description from the docs:

/**
* Optional error string for helping the user figure out why they failed here.
* @type {(string|undefined)}
*/
AuditResult.prototype.debugString;

@patrickhulce WDYT? Can we keep the debugString here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah sgtm, I'm sort of off the reservation when it comes to my hopes for debugString anyway :)

Copy link
Member

Choose a reason for hiding this comment

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

I'll put the title and debugString in the order you'd see them in the report:

Document does not have a meta description

Description meta tag is missing

Given that the debugString duplicates so much, i think we should exclude it.

};
}

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'}
Copy link
Member

Choose a reason for hiding this comment

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

nit: go for the trailing comma here so future diffs will be cleaner

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I saw trailing comma being used here and there, but since there is no 100% consistency, I left it out. The cleaner diffs argument SGTM, maybe we should make eslint to check for that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

AFAIK there wasn't any effort to do trailing commas other than occasionally sneaking them in as we touched code but huge +1 to turning on the eslint rule :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've opened an issue for it: #3304 👍

]
}
}
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
37 changes: 37 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,37 @@
/**
* @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', () => {
return assert.equal(Audit.audit({
Copy link
Member

Choose a reason for hiding this comment

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

it would be good to assert that the audit is also returning a debugString (or a displayValue, depending on how the comment above shakes out :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will do 👍

MetaDescription: null
}).rawValue, false);
});

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

it('fails when description consists only of whitespace', () => {
return assert.equal(Audit.audit({
MetaDescription: ' '
}).rawValue, false);
});

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);
});
});
});