-
Notifications
You must be signed in to change notification settings - Fork 0
feat(browser): scope waitFor rules by page type instead of a duplicated path regex (v1.17.0) #71
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -366,11 +366,16 @@ const renderer: Renderer = async (page, job) => { | |||||
| /* leave '' */ | ||||||
| } | ||||||
| for (const rule of config.waitFor ?? []) { | ||||||
| // Per-rule scoping: skip rules that don't target this device or path, so a rule never | ||||||
| // polls to its timeout on renders it isn't meant for (e.g. a PDP-reviews rule on a | ||||||
| // category page, or on desktop where the content is already in view). Validated at config | ||||||
| // load, so a bad pathPattern regex can't reach here. | ||||||
| // Per-rule scoping: skip rules that don't target this device, page type, or path, so a | ||||||
| // rule never polls to its timeout on renders it isn't meant for (e.g. a PDP-reviews rule | ||||||
| // on a category page, or on desktop where the content is already in view). Scopes AND | ||||||
| // together. Validated at config load, so a bad pathPattern regex can't reach here. | ||||||
| if (rule.devices && !rule.devices.includes(deviceType)) continue; | ||||||
| // A job with no declared page type never matches a pageTypes rule — the plugin may | ||||||
| // predate the field, or the route may name no template. Skipping costs the content this | ||||||
| // rule would have waited for; applying it blindly would cost a poll to the timeout on | ||||||
| // every page that lacks the widget, which is the failure this scoping exists to prevent. | ||||||
| if (rule.pageTypes && !(job.pageType && rule.pageTypes.includes(job.pageType))) continue; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logical expression
Suggested change
|
||||||
| if (rule.pathPattern && !new RegExp(rule.pathPattern).test(path)) continue; | ||||||
|
|
||||||
| const contentSelector = rule.waitForSelector ?? rule.selector; | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,26 @@ test('waitFor: validation of devices / pathPattern scoping', () => { | |
| ); | ||
| }); | ||
|
|
||
| test('waitFor: validation of pageTypes scoping', () => { | ||
| assert.throws( | ||
| () => mergeConfig({ waitFor: [{ selector: '#r', pageTypes: 'pdp' as unknown as string[] }] }), | ||
| /waitFor\[0\]\.pageTypes must be an array/ | ||
| ); | ||
| assert.throws( | ||
| () => mergeConfig({ waitFor: [{ selector: '#r', pageTypes: ['pdp', ''] }] }), | ||
| /waitFor\[0\]\.pageTypes must be an array/ | ||
| ); | ||
|
Comment on lines
+145
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a test case to verify that configuring assert.throws(
() => mergeConfig({ waitFor: [{ selector: '#r', pageTypes: ['pdp', ''] }] }),
/waitFor\\[0\\]\\.pageTypes must be an array/
);
assert.throws(
() => mergeConfig({ waitFor: [{ selector: '#r', pageTypes: [] }] }),
/waitFor\\[0\\]\\.pageTypes must be an array/
); |
||
| // One template reached by several URL shapes is several names here, not a regex alternation | ||
| // that has to be kept in step with the plugin's route list by hand. | ||
| assert.doesNotThrow(() => | ||
| mergeConfig({ waitFor: [{ selector: '#r', devices: ['mobile'], pageTypes: ['pdp', 'category'] }] }) | ||
| ); | ||
| // pageTypes and pathPattern coexist — the migration path is rule-by-rule, not a flag day. | ||
| assert.doesNotThrow(() => | ||
| mergeConfig({ waitFor: [{ selector: '#r', pageTypes: ['pdp'], pathPattern: '^/product/' }] }) | ||
| ); | ||
| }); | ||
|
|
||
| test('validation: a device must have a numeric viewport', () => { | ||
| const file = writeConfig({ devices: { desktop: { viewport: { width: 'wide' } } } }); | ||
| assert.throws(() => loadConfig(file), /requires a viewport with numeric width and height/); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The validation check for
rule.pageTypesdoes not prevent an empty array ([]) from being configured. IfpageTypesis configured as an empty array, the rule will always be skipped silently becauserule.pageTypes.includes(job.pageType)will always evaluate tofalse. To prevent this silent misconfiguration, we should explicitly check thatrule.pageTypes.length === 0is also treated as an invalid configuration.