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

core(tsc): NetworkRequest.RESOURCE_TYPES type fix #5851

Merged
merged 2 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class CacheHeaders extends Audit {
static isCacheableAsset(record) {
const CACHEABLE_STATUS_CODES = new Set([200, 203, 206]);

/** @type {Set<string>} */
const STATIC_RESOURCE_TYPES = new Set([
NetworkRequest.TYPES.Font,
NetworkRequest.TYPES.Image,
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/gather/computed/critical-request-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CriticalRequestChains extends ComputedArtifact {
// XHRs are fetched at High priority, but we exclude them, as they are unlikely to be critical
// Images are also non-critical.
// Treat any missed images, primarily favicons, as non-critical resources
/** @type {Array<string>} */
const nonCriticalResourceTypes = [
NetworkRequest.TYPES.Image,
NetworkRequest.TYPES.XHR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const CHROME_EXTENSION_PROTOCOL = 'chrome-extension:';
const compressionHeaders = ['content-encoding', 'x-original-content-encoding'];
const compressionTypes = ['gzip', 'br', 'deflate'];
const binaryMimeTypes = ['image', 'audio', 'video'];
/** @type {Array<string>} */
Copy link
Collaborator

Choose a reason for hiding this comment

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

so Array<LH.Crdp.Page.ResourceType> won't work anymore? Maybe I'm confused but I'm not sure why it's better now :)

What are the good cases we expect to catch by having Network.TYPES.Fetch be just 'Fetch' instead of LH.Crdp.Page.ResourceType?

Copy link
Member Author

Choose a reason for hiding this comment

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

so Array<LH.Crdp.Page.ResourceType> won't work anymore?

uh, no, that will work and is way better :) fixing now.

The issue was that these are defined with const strings, so tsc wasn't widening them at all. e.g.

const textResourceTypes = [
  NetworkRequest.TYPES.Document,
  NetworkRequest.TYPES.Script,
  NetworkRequest.TYPES.Stylesheet,
];

was becoming only type Array<'Document'|'Script'|'Stylesheet'>, so textResourceTypes.includes() wasn't allowing you to pass it the general LH.Crdp.Page.ResourceType type (see microsoft/TypeScript#26255 for more on this annoyance :)

But Array<LH.Crdp.Page.ResourceType> is a way better fix than Array<string> for that.

Copy link
Member Author

Choose a reason for hiding this comment

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

What are the good cases we expect to catch by having Network.TYPES.Fetch be just 'Fetch' instead of LH.Crdp.Page.ResourceType?

basically the inverse of the situation with the arrays. e.g if you had doSomething(resourceType: 'Fetch'|'XHR'), passing it NetworkRequest.TYPES.Fetch will be an error (since it's the full union). Not an issue in our current code, but it could be in the future.

(but it sounds like you're ok with this without the regression to Array<string> in the original form of the PR?)

const textResourceTypes = [
NetworkRequest.TYPES.Document,
NetworkRequest.TYPES.Script,
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/lib/network-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const SECURE_SCHEMES = ['data', 'https', 'wss', 'blob', 'chrome', 'chrome-extens
* @property {string} securityOrigin
*/

/** @type {Record<LH.Crdp.Page.ResourceType, LH.Crdp.Page.ResourceType>} */
/** @type {SelfMap<LH.Crdp.Page.ResourceType>} */
const RESOURCE_TYPES = {
XHR: 'XHR',
Fetch: 'Fetch',
Expand Down