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

feat(cli): add static dir file discovery depth flag #940

Merged
merged 6 commits into from
Dec 14, 2023
Merged
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
Next Next commit
feat(cli): add static dir file discovery depth flag
DanielSwarup committed Aug 24, 2023
commit 88447d09db4a0924da63ec4bd701d37814f0f54f
22 changes: 22 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -230,6 +230,9 @@ Options:
--maxAutodiscoverUrls The maximum number of pages to collect when using the staticDistDir
option with no specified URL. Disable this limit by setting to 0.
[number] [default: 5]
--staticDirFileDiscoveryDepth The maximum depth of nested folders Lighthouse will look into to discover
URLs on a static file folder.
[number] [default: 2]
```

#### `method`
@@ -420,6 +423,25 @@ lhci collect --url=https://example-1.com --url=https://example-2.com
lhci collect --start-server-command="yarn serve" --url=http://localhost:8080/ --puppeteer-script=./path/to/login-with-puppeteer.js
```

### `staticDirFileDiscoveryDepth`

The maximum depth level of nested folders that Lighthouse will look into to discover URLs. If not set, this will default to 2.

### Example
```text

public/
├── index.html #level 0
├── contact/
│ └── index.html #level 1
├── projects/
│ ├──index.html #level 1
│ └── crisis/
│ ├──index.html #level 2
│ └── earthquake/
│ └── index.html #level 3
```

---

### `upload`
9 changes: 8 additions & 1 deletion packages/cli/src/collect/collect.js
Original file line number Diff line number Diff line change
@@ -94,6 +94,12 @@ function buildCommand(yargs) {
default: 5,
type: 'number',
},
staticDirFileDiscoveryDepth: {
description:
'The maximum depth level of nested folders that Lighthouse will look into to discover URLs. If not set, this will default to 2.',
default: 2,
type: 'number',
},
});
}

@@ -187,7 +193,8 @@ async function startServerAndDetermineUrls(options) {
: options.autodiscoverUrlBlocklist
? [options.autodiscoverUrlBlocklist]
: [];
const availableUrls = server.getAvailableUrls();
const maxStaticDirFileDiscoveryDepth = options.staticDirFileDiscoveryDepth || 2;
const availableUrls = server.getAvailableUrls(maxStaticDirFileDiscoveryDepth);
const normalizedBlocklist = autodiscoverUrlBlocklistAsArray.map(rawUrl => {
const url = new URL(rawUrl, 'http://localhost');
url.port = server.port.toString();
17 changes: 14 additions & 3 deletions packages/cli/src/collect/fallback-server.js
Original file line number Diff line number Diff line change
@@ -72,9 +72,20 @@ class FallbackServer {
);
}

/** @return {string[]} */
getAvailableUrls() {
const htmlFiles = FallbackServer.readHtmlFilesInDirectory(this._pathToBuildDir, 2);
/**
* @param {number} maxDepth
* @return {string[]}
*/
getAvailableUrls(maxDepth) {
if (maxDepth >= 0) {
maxDepth = Math.floor(maxDepth);
} else {
process.stderr.write(
`WARNING: staticDirFileDiscoveryDepth must be greater than 0. Defaulting to a discovery depth of 2\n`
);
maxDepth = 2;
}
const htmlFiles = FallbackServer.readHtmlFilesInDirectory(this._pathToBuildDir, maxDepth);
return htmlFiles.map(({file}) => `http://localhost:${this._port}/${file}`);
}

1 change: 1 addition & 0 deletions types/collect.d.ts
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ declare global {
additive: boolean;
settings?: LighthouseSettings;
maxAutodiscoverUrls?: number;
staticDirFileDiscoveryDepth?: number;
}
}
}