Skip to content

Commit a75a445

Browse files
authored
fix: Reject redirected requests for static snapshots (#273)
This makes it so we no longer get redirected from the URL we visit to another one. That usually results in an error, which fails the test suite. This also adds some try/catches around parts of the snapshot that could fail with helpful error messages
1 parent 8a85e6f commit a75a445

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

src/services/static-snapshot-service.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,46 @@ export default class StaticSnapshotService {
4444
const percyAgentClientFilename = agentJsFilename()
4545
const page = await browser.newPage()
4646

47+
// Do not follow redirects to ensure we don't navigate to another page
48+
await page.setRequestInterception(true)
49+
page.on('request', (request) => {
50+
if (request.isNavigationRequest() && request.redirectChain().length) {
51+
logger.debug(`Skipping redirect: ${request.url()}`)
52+
request.abort()
53+
} else {
54+
request.continue()
55+
}
56+
})
57+
4758
const pageUrls = await this._buildPageUrls()
4859

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

52-
await page.goto(url, { waitUntil: 'networkidle0' })
53-
54-
await page.addScriptTag({
55-
path: percyAgentClientFilename,
56-
})
57-
58-
await page.evaluate((url) => {
59-
const percyAgentClient = new PercyAgent()
60-
const parsedURL = new URL(url)
61-
const snapshotName = parsedURL.pathname || url
62-
63-
return percyAgentClient.snapshot(snapshotName)
64-
}, url)
63+
try {
64+
await page.goto(url, { waitUntil: 'networkidle2' })
65+
} catch (error) {
66+
logger.error(`Failed to navigate to ${url}, skipping. Error: ${error}`)
67+
}
68+
69+
try {
70+
await page.addScriptTag({
71+
path: percyAgentClientFilename,
72+
})
73+
74+
await page.evaluate((url) => {
75+
const percyAgentClient = new PercyAgent()
76+
const parsedURL = new URL(url)
77+
const snapshotName = parsedURL.pathname || url
78+
79+
return percyAgentClient.snapshot(snapshotName)
80+
}, url)
81+
} catch (error) {
82+
logger.error(`Failed to inject agent JS: ${error}`)
83+
}
6584
}
6685

67-
browser.close()
86+
await browser.close()
6887
}
6988

7089
async stop() {

0 commit comments

Comments
 (0)