Skip to content

Commit

Permalink
fix condition
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Oct 31, 2023
1 parent 1191e98 commit 4a0e76c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/audits/bf-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ class BFCache extends Audit {
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts) {
if (artifacts.HostUserAgent.indexOf('HeadlessChrome') !== -1) {
// Old headless mode does not obfuscate the specific Chrome version in the UA string
// Old Headless Example: HeadlessChrome/120.0.6099.1
// New Headless Example: HeadlessChrome/120.0.0.0
const isOldHeadless = /HeadlessChrome\/[0-9]+\.0.[0-9]{2,}.0/.test(artifacts.HostUserAgent);
if (isOldHeadless) {
return {
score: null,
notApplicable: true,
Expand Down
36 changes: 36 additions & 0 deletions core/test/audits/bf-cache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('BFCache audit', () => {
it('fails if there are actionable failure reasons', async () => {
/** @type {any} */
const artifacts = {
// eslint-disable-next-line max-len
HostUserAgent: 'Mozilla/5.0 (Linux; Android 11; moto g power (2022)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Mobile Safari/537.36',
BFCacheFailures: [{
notRestoredReasonsTree: {
PageSupportNeeded: {
Expand Down Expand Up @@ -62,6 +64,8 @@ describe('BFCache audit', () => {
it('fails if there are only non-actionable failures', async () => {
/** @type {any} */
const artifacts = {
// eslint-disable-next-line max-len
HostUserAgent: 'Mozilla/5.0 (Linux; Android 11; moto g power (2022)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Mobile Safari/537.36',
BFCacheFailures: [{
notRestoredReasonsTree: {
PageSupportNeeded: {},
Expand All @@ -84,9 +88,41 @@ describe('BFCache audit', () => {
expect(result.details.items).toHaveLength(2);
});

it('is n/a if using old headless', async () => {
/** @type {any} */
const artifacts = {
// eslint-disable-next-line max-len
HostUserAgent: 'Mozilla/5.0 (Linux; Android 11; moto g power (2022)) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/109.0.5000.0 Mobile Safari/537.36',
BFCacheFailures: [{
notRestoredReasonsTree: {
PageSupportNeeded: {},
Circumstantial: {
BackForwardCacheDisabled: ['https://example.com'],
},
SupportPending: {
CacheControlNoStore: ['https://frame.com'],
},
},
}],
};

const result = await BFCache.audit(artifacts);

expect(result.displayValue).toBeUndefined();
expect(result.score).toEqual(null);
expect(result.details).toBeUndefined();

expect(result.warnings).toHaveLength(1);
expect(result.warnings?.[0]).toBeDisplayString(
/Back\/forward cache cannot be tested in old Headless/
);
});

it('passes if there are no failures', async () => {
/** @type {any} */
const artifacts = {
// eslint-disable-next-line max-len
HostUserAgent: 'Mozilla/5.0 (Linux; Android 11; moto g power (2022)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Mobile Safari/537.36',
BFCacheFailures: [{
notRestoredReasonsTree: {
PageSupportNeeded: {},
Expand Down

0 comments on commit 4a0e76c

Please sign in to comment.