Skip to content

Commit

Permalink
fix: Skip requests in asset discovery that will never be saved (#457)
Browse files Browse the repository at this point in the history
* fix: Skip requests that will never be saved

We shouldn't bother waiting for a request to finish during asset discovery if
we're not going to save it

* fix: import URL

Somehow this worked in node 10?
  • Loading branch information
Robdel12 committed Jan 16, 2020
1 parent 11db7aa commit ce36cf2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/services/asset-discovery-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as merge from 'deepmerge'
import * as pool from 'generic-pool'
import * as puppeteer from 'puppeteer'
import { URL } from 'url'
import { AssetDiscoveryConfiguration } from '../configuration/asset-discovery-configuration'
import { DEFAULT_CONFIGURATION } from '../configuration/configuration'
import { SnapshotOptions } from '../percy-agent-client/snapshot-options'
import domainMatch from '../utils/domain-match'
import { addLogDate, logError, profile } from '../utils/logger'
import { cacheResponse, getResponseCache } from '../utils/response-cache'
import waitForNetworkIdle from '../utils/wait-for-network-idle'
Expand Down Expand Up @@ -168,6 +170,25 @@ export class AssetDiscoveryService extends PercyClientService {
await this.closeBrowser()
}

// We shouldn't bother passing on requests that will never be saved
shouldProcessRequest(resourceUrl: string, rootResourceUrl: string): boolean {
const parsedRootResourceUrl = new URL(rootResourceUrl)
const rootUrl = `${parsedRootResourceUrl.protocol}//${parsedRootResourceUrl.host}`

// Process if the resourceUrl has a hostname in the allowedHostnames
if (this.configuration['allowed-hostnames'].some((hostname) => domainMatch(hostname, resourceUrl))) {
return true
}

// Capture if the resourceUrl is the same as the rootUrL
if (resourceUrl.startsWith(rootUrl)) {
return true
}

// We won't be capturing this asset, no need to wait for it to respond
return false
}

private async resourcesForWidth(
pool: pool.Pool<puppeteer.Page>,
width: number,
Expand Down Expand Up @@ -207,13 +228,21 @@ export class AssetDiscoveryService extends PercyClientService {
return
}

if (!this.shouldProcessRequest(requestUrl, rootResourceUrl)) {
logger.debug(addLogDate(`Aborting ${requestUrl} -- will never be saved`))
await request.abort()
return
}

if (this.configuration['cache-responses'] === true && getResponseCache(requestUrl)) {
logger.debug(addLogDate(`Asset cache hit for ${requestUrl}`))
await request.respond(getResponseCache(requestUrl))

return
}

logger.debug(addLogDate(`Starting processing for: ${requestUrl}`))

await request.continue()
} catch (error) {
logError(error)
Expand Down

0 comments on commit ce36cf2

Please sign in to comment.