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

fix: Reject redirected requests for static snapshots #273

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Changes from all commits
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
47 changes: 33 additions & 14 deletions src/services/static-snapshot-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,46 @@ export default class StaticSnapshotService {
const percyAgentClientFilename = agentJsFilename()
const page = await browser.newPage()

// Do not follow redirects to ensure we don't navigate to another page
await page.setRequestInterception(true)
page.on('request', (request) => {
if (request.isNavigationRequest() && request.redirectChain().length) {
logger.debug(`Skipping redirect: ${request.url()}`)
request.abort()
} else {
request.continue()
}
})

const pageUrls = await this._buildPageUrls()

for (const url of pageUrls) {
logger.debug(`visiting ${url}`)

await page.goto(url, { waitUntil: 'networkidle0' })

await page.addScriptTag({
path: percyAgentClientFilename,
})

await page.evaluate((url) => {
const percyAgentClient = new PercyAgent()
const parsedURL = new URL(url)
const snapshotName = parsedURL.pathname || url

return percyAgentClient.snapshot(snapshotName)
}, url)
try {
await page.goto(url, { waitUntil: 'networkidle2' })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

networkidle2 is much faster than networkidle0

} catch (error) {
logger.error(`Failed to navigate to ${url}, skipping. Error: ${error}`)
}

try {
await page.addScriptTag({
path: percyAgentClientFilename,
})

await page.evaluate((url) => {
const percyAgentClient = new PercyAgent()
const parsedURL = new URL(url)
const snapshotName = parsedURL.pathname || url

return percyAgentClient.snapshot(snapshotName)
}, url)
} catch (error) {
logger.error(`Failed to inject agent JS: ${error}`)
}
}

browser.close()
await browser.close()
}

async stop() {
Expand Down