-
Notifications
You must be signed in to change notification settings - Fork 634
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
Add manifest-exists
rule and make other improvements
#54
Merged
molant
merged 5 commits into
webhintio:master
from
alrra:add-manifest-exists-rule-and-make-improvements
Mar 21, 2017
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a29246
Add rule to check the existence of a manifest file
alrra 608c373
Fix error in rule-runner when using `element::name::index`
molant 6df65f4
Fix `manifest-exists` rule
molant 99cf297
Make rule-runner allow to specify request results
alrra 8ee1076
Use `NetworkData` instead of `FetchResponse`
alrra File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Require a web app manifest file (manifest-exists) | ||
|
||
## Rule Details | ||
|
||
This rule warns against not providing a | ||
[web app manifest](https://www.w3.org/TR/appmanifest) file. | ||
|
||
To ensure a web app manifest file is provided, the rule basically | ||
checks: | ||
|
||
* if the web app manifest file is specified correctly in the | ||
page (i.e. the page contains a single, valid declaration such | ||
as: `<link rel="manifest" href="site.webmanifest">`) | ||
|
||
* if the declared web app manifest file is actually accessible | ||
(i.e. the request doesn't result in a 404, 500, etc.) | ||
|
||
In the context of [progressive web | ||
apps](https://en.wikipedia.org/wiki/Progressive_web_app), this | ||
rule is important as providing a web app manifest file is essential. | ||
|
||
## Resources | ||
|
||
* [Web App Manifest Specification](https://www.w3.org/TR/appmanifest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* @fileoverview Check if a single web app manifest file is specified, | ||
* and if that specified file is accessible. | ||
*/ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
import * as url from 'url'; | ||
|
||
import { Rule, RuleBuilder, ElementFoundEvent } from '../../types'; // eslint-disable-line no-unused-vars | ||
import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars | ||
|
||
const debug = require('debug')('sonar:rules:manifest-exists'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Public | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule: RuleBuilder = { | ||
create(context: RuleContext): Rule { | ||
|
||
let manifestIsSpecified = false; | ||
|
||
const manifestWasSpecified = () => { | ||
|
||
// If no web app manifest file was specified when | ||
// the parsing of the page ended, emit an error. | ||
|
||
if (!manifestIsSpecified) { | ||
context.report(null, null, 'Web app manifest not specified'); | ||
} | ||
}; | ||
|
||
const manifestExists = async (data: ElementFoundEvent) => { | ||
const { element, resource } = data; | ||
|
||
if (element.getAttribute('rel') === 'manifest') { | ||
|
||
// Check if we encounter more than one | ||
// <link rel="manifest"...> declaration. | ||
|
||
if (manifestIsSpecified) { | ||
context.report(resource, element, 'Web app manifest already specified'); | ||
|
||
return; | ||
} | ||
|
||
manifestIsSpecified = true; | ||
|
||
// Check if a web app manifest file is specified, | ||
// and if the specified file actually exists. | ||
// | ||
// https://w3c.github.io/manifest/#obtaining | ||
|
||
const manifestHref = element.getAttribute('href'); | ||
let manifestURL = ''; | ||
|
||
// Check if `href` doesn't exist or it has the | ||
// value of empty string. | ||
|
||
if (!manifestHref) { | ||
context.report(resource, element, `Web app manifest specified with invalid 'href'`); | ||
|
||
return; | ||
} | ||
|
||
// If `href` exists and is not an empty string, try | ||
// to figure out the full URL of the web app manifest. | ||
|
||
if (url.parse(manifestHref).protocol) { | ||
manifestURL = manifestHref; | ||
} else { | ||
manifestURL = url.resolve(resource, manifestHref); | ||
} | ||
|
||
// Try to see if the web app manifest file actually | ||
// exists and is accesible. | ||
|
||
try { | ||
const { statusCode } = await context.fetchContent(manifestURL); | ||
|
||
// If it's not a local file (has statusCode === null), | ||
// check also if the status code is `200`. | ||
|
||
if (statusCode && statusCode !== 200) { | ||
context.report(resource, element, `Web app manifest file could not be fetched (status code: ${statusCode})`); | ||
} | ||
|
||
// Check if fetching/reading the file failed. | ||
|
||
} catch (e) { | ||
debug('Failed to fetch the web app manifest file'); | ||
context.report(resource, element, `Web app manifest file request failed`); | ||
} | ||
|
||
} | ||
}; | ||
|
||
return { | ||
'element::link': manifestExists, | ||
'traverse::end': manifestWasSpecified | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'PWA', | ||
description: 'Provide a web app manifest file', | ||
recommended: true | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
} | ||
}; | ||
|
||
module.exports = rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/tests/lib/rules/manifest-exists/fixtures/manifest-specified-and-file-does-exist.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>test</title> | ||
<link rel="manifest" href="site.webmanifest"> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> |
10 changes: 10 additions & 0 deletions
10
src/tests/lib/rules/manifest-exists/fixtures/manifest-specified-and-file-does-not-exist.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>test</title> | ||
<link rel="manifest" href="does-not-exist.webmanifest"> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> |
10 changes: 10 additions & 0 deletions
10
src/tests/lib/rules/manifest-exists/fixtures/manifest-specified-as-full-url.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>test</title> | ||
<link rel="manifest" href="https://example.com/site.webmanifest"> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@molant At some point rules will also need to know the request headers, so maybe we should rename
headers
toresponseHeaders
, or better yet just split this into 2 objects ({ request: {}, response: {} }
). What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give me an example of why a rule should know the request headers? If they need an specific header they can pass it via
customHeaders
.If we need something else other than the headers from the response then I'd go for the 2 objects. If it is only the headers then
responseHeaders
andrequestHeaders
are ok.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While users can make extra requests where they can set if needed custom request headers so that they control exactly how the requests are made, I think that providing the request headers will simplify some things, and in certain cases could potentially avoid some extra requests (e.g.: users can check what headers where sent by default and based on that conditionally do extra things, they can quickly use request headers while debugging).
That being said, I don't expect them to be used much, probably mostly with rules related to: content negotiation.
Another question is: in the future, are we considering providing any other information about the requests (e.g.: information about timing, security)?
My opinion right now is that, even if we don't expose the request headers for now, I think the response related data should be under
response: {...}
so that we can add other things in the future without any problems.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, let's go with
request: {}, response: {}
then :)