Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

should we move Core Web Vitals to their own package? #350

Open
sndrs opened this issue May 11, 2022 · 1 comment
Open

should we move Core Web Vitals to their own package? #350

sndrs opened this issue May 11, 2022 · 1 comment

Comments

@sndrs
Copy link
Member

sndrs commented May 11, 2022

main reason being it turns the google package into a peerDep of libs regardless of what you're using from it.

i'm getting warnings about it being missing when using libs in another node module.

i don't want to add it to keep that warning quiet.

maybe anything that has a peerDep should not be included in libs?

any thoughts?

cc @mxdvl @guardian/client-side-infra

@mxdvl
Copy link
Member

mxdvl commented May 12, 2022

I think that’s a valid point, and it would be better to keep @guardian/libs dependency-free. Any idea where it might live instead?

I’ve been thinking recently about creating a better abstraction around this anyways, and we could possibly move the core-vitals dependencies back to the consumers.


Just so I get it outside my brain, I was thinking something like below, where the init function returns a method you can call to register sending the metrics at page unload:

/** List of collecting functions to run at page unload */
const collectors = new Map<string, () => void>();

/** Initialiser logger */
const init = ({ label, collector }: {
  label: string;
  collector: () => void;
}) => ({
  /** Call this function to schedule collection at page unload */
  schedule: (consent: boolean) => {
    if (!consent) {
      console.warn(`No consent for “${label}” logging`);
      return;
    }

    if (collectors.has(label)) {
      console.warn(`Overriding “${label}” logging`);
    }

    collectors.set(label, collector);
  },
  /** Call this function to stop collection at page unload */
  remove: () => {
    console.warn(`Removing “${label}” logging`);
    collectors.delete(label);
  },
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants