4lejandrito / next-plausible

Simple integration for https://nextjs.org and https://plausible.io analytics
https://next-plausible.vercel.app
MIT License
558 stars 30 forks source link

Self-hosting script with plausible-tracker? #78

Closed Schmedu closed 1 year ago

Schmedu commented 1 year ago

Hey @4lejandrito, first of all thanks for maintaining!

I have a question how to self host the script, because of SRI. I came across this here:

In another post I saw that the script is now posted to npm: https://github.com/plausible/analytics/discussions/1834

Is there a way that I can combine these two and just link to the script from the npm package with the scriptProps like

    <PlausibleProvider
      domain="example.com"
      scriptProps={{
        src: '/custom/js/script.js',
      }}
    />

Thanks in advance.

neilk17 commented 1 year ago

It already does that without passing the props

4lejandrito commented 1 year ago

Hi @Schmedu! next-plausible always gets the script remotely, either from plausible.io or your custom domain, depending on how you set it up. At the moment it cannot use any script stored locally out of the box.

However if you create a nextjs api endpoint that returns the npm script, let's say (/api/script) then you could theoretically overwrite the src prop as you're saying. But I am not sure that would work, because using the npm script requires an extra step of initializing the tracker and next-plausible is designed to work with the hosted script that self initializes.

I will investigate and consider if it is worth supporting the npm script natively from next-plausible, but I can't promise anything 😄 .

Schmedu commented 1 year ago

Thanks for the answer, @4lejandrito.

I thought that it might be possible to hook up the initialization of the script with next-plausibles Context provider, where we are also provide the same information. With this approach you could skip the proxying of the script as it would be self hosted.

Would be nice if you could make it work. I'll close the issue in the meantime.