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

Support multiple calls to onINP() with different config options #583

Merged
merged 9 commits into from
Jan 10, 2025

Conversation

philipwalton
Copy link
Member

@philipwalton philipwalton commented Dec 20, 2024

Fixes #582.

This PR updates the interactions.ts logic to encapsulate it in a class that can be instantiated per onINP() call. It also moves most of the functions in the attribution/onINP.ts file inside of the onINP() closure, to keep all of the variables local to each onINP() call.

The main downside of this PR is it increases the bundle size of the library (since object properties don't minify as well as instance variables), but I added an additional terser config option to mangle object properties that begin with a $ character, which negates much of the increased size (at the expense of being a bit annoying to write).

Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

This all looks to work but I'm really, really, really hating the $ thing. I don't see how this is maintainable going forward. I'm already confused as to which variables should have a $ and which shouldn't (see comment below).

And this has just gets MORE complicated in #585 as we need to roll this pattern out to LCP and CLS :-(

There surely must be a better way to have separate entryPreProcessingCallbacks values (maybe keyed by a unique id created at each onINP instantiation?).

Comment on lines 20 to 22
id: number;
entries: PerformanceEventTiming[];
$latency: number;
Copy link
Member

Choose a reason for hiding this comment

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

Why are only two of these public, and one not? Seems weird to mix and match public and private names in the same interface.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nit: don't think of the $ prefix and representing public or private (at least in the traditional sense of those words)—only whether or not the symbol will be mangled by terser. All of these symbols are "public" in the strict sense (in that they are accessible outside of their class), but "private" to the package (in that no consumer of the web-vitals library can access these methods).

As for why some of them have the $ prefix and some don't, the only reason they all don't is because the property names id, entries are already part of the public Metric API, and so the library compresses better if we don't mangle those symbols but instead reuse the existing dictionary entry for them.

We can probably add a comment in the code to avoid that confusion.

Copy link
Member

Choose a reason for hiding this comment

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

Ahhh that's what I wasn't getting. Definitely needs a comment to explain this.

Copy link
Member

Choose a reason for hiding this comment

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

@philipwalton why $ and not _?

Copy link
Member

Choose a reason for hiding this comment

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

_ is more common for internal variable names in my experience.

Copy link
Member Author

Choose a reason for hiding this comment

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

I originally had _, but then I was concerned that was confusing since a _ prefix typically implies private in the traditional sense (which this is not).

But I'm happy to move back to that if you'd all prefer it.

Copy link
Member

Choose a reason for hiding this comment

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

Well _ is more a convention of "internal", than necessarily private. The newer # would strictly mean private. So I think _ is better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Switched from $ to _.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I don't feel strongly, but $ makes me think, "oh no, what weird thing are they doing with their build" while _ I think, "well, that's not exactly 'private', but close enough" :)

Copy link
Member

@tunetheweb tunetheweb Jan 10, 2025

Choose a reason for hiding this comment

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

Yup me too.

I also don’t think it needs documented as much with _ because it’s a more well-known pattern.

src/lib/InteractionManager.ts Show resolved Hide resolved
@philipwalton
Copy link
Member Author

This all looks to work but I'm really, really, really hating the $ thing. I don't see how this is maintainable going forward. I'm already confused as to which variables should have a $ and which shouldn't (see comment below).

See my other comment for an explanation as to why some have the $ and some don't. The alternatives to using the $ prefix are:

  • Default to compressing all property names but allowlist the ones we don't want to mangle in the config (which I think will be harder to maintain)
  • Just live with the larger brotli size (which personally I don't want to do)

Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

With the switch to _ I feel more comfortable with this since it’s a more used pattern.

@philipwalton philipwalton merged commit 06df5a7 into v5 Jan 10, 2025
6 checks passed
@philipwalton philipwalton deleted the rework-interaction-logic branch January 10, 2025 21:35
@philipwalton philipwalton restored the rework-interaction-logic branch January 10, 2025 21:35
@philipwalton philipwalton deleted the rework-interaction-logic branch January 10, 2025 21:47
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.

3 participants