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: injectJQuery in context does not survive navs #1661

Merged
merged 2 commits into from
Nov 11, 2022

Conversation

barjin
Copy link
Contributor

@barjin barjin commented Nov 9, 2022

Let's play Bug Fix or a Breaking Change!

Because requestHandler and pre/postNavigationHooks share the same context, the surviveNavigations is disabled for injectJQuery in all of those handlers/hooks.

Consider the following snippet:

{
 preNavigationHooks: [
   async ({ injectJQuery }) => injectJQuery();
 ],
 requestHandler: async ({ page })  => {
   await page.evaluate(() => $('title').text);
 },
}

This could have worked before (albeit it was very unstable), but will fail every time now.

What is it then? Bug Fix or a Breaking Change?

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

i think we can do better, detecting whether the execution happens from inside the request handler should be easy as it all goes through BasicCrawler._runRequestHandler, similarly, we can detect the preNavigationHooks though BrowserCrawler._handleNavigation.

just to be sure, it won't work with pre nav hooks, but it will keep working with post nav hooks (as well as with direct request handler usage)

@barjin
Copy link
Contributor Author

barjin commented Nov 10, 2022

Okay, I understand we want to alter the behavior (turn off surviveNavigation) in requestHandler and postNavHooks as well - maybe output a warning message in preNavigationHooks?

Either way, this should be done in playwrightUtils module (sounds not SOLID to handle this anywhere else), which leaves us with analyzing the call stack there? Altering the behavior based on the stack contents sounds like a massive antipattern - but I'm probably missing something. What solution do you have in mind?

@B4nan
Copy link
Member

B4nan commented Nov 10, 2022

Stack traces are rather expensive and error-prone, I don't want such a solution to be done on a per request base.

What I was thinkin about is a simple bool flag on the request object maybe, that would tell you in what processing state the request is - pre/post nav hooks, or request handler (or maybe something else too, idk), maybe an enum instead of multiple bools, didn't put much thinking to it. You'd use that in here where you have the full context object available:

context.injectJQuery = () => injectJQuery(context.page);

@barjin barjin marked this pull request as ready for review November 10, 2022 16:49
Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

lgtm, i see it was a bit more work than expected, but i like that we now have this information available, i am sure there will be more use cases where this will be very handy

@B4nan B4nan merged commit 493a7cf into master Nov 11, 2022
@B4nan B4nan deleted the fix/jquerysurvivecontext branch November 11, 2022 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants