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(collect): Make it easy to use with SPA #159

Merged
merged 4 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/cli/src/collect/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function buildCommand(yargs) {
staticDistDir: {
description: 'The build directory where your HTML files to run Lighthouse on are located.',
},
isSinglePageApplication: {
description:
'If the application is created by Single Page Application, enable redirect to index.html.',
},
chromePath: {
description: 'The path to the Chrome or Chromium executable to use for collection.',
default: process.env.CHROME_PATH || ChromeLauncher.getInstallations()[0],
Expand Down Expand Up @@ -125,7 +129,7 @@ async function startServerAndDetermineUrls(options) {
}

const pathToBuildDir = path.resolve(process.cwd(), options.staticDistDir);
const server = new FallbackServer(pathToBuildDir);
const server = new FallbackServer(pathToBuildDir, options.isSinglePageApplication);
await server.listen();
process.stdout.write(`Started a web server on port ${server.port}...\n`);

Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/collect/fallback-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ const createHttpServer = require('http').createServer;
class FallbackServer {
/**
* @param {string} pathToBuildDir
* @param {boolean|undefined} isSinglePageApplication
*/
constructor(pathToBuildDir) {
constructor(pathToBuildDir, isSinglePageApplication) {
this._pathToBuildDir = pathToBuildDir;
this._app = express();
this._app.use(compression());
this._app.use('/', express.static(pathToBuildDir));
if (isSinglePageApplication) {
this._app.use('/*', (req, res) => res.sendFile(pathToBuildDir + '/index.html'));
}
this._port = 0;
/** @type {undefined|ReturnType<typeof createHttpServer>} */
this._server = undefined;
Expand Down
1 change: 1 addition & 0 deletions types/collect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ declare global {
export interface Options {
url?: string | string[];
staticDistDir?: string;
isSinglePageApplication?: boolean;
startServerCommand?: string;
startServerReadyPattern: string;
chromePath?: string;
Expand Down