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

Feature Request: Allow bundling of analytics scripts #163

Open
jawnsy-humane opened this issue Jan 8, 2025 · 1 comment
Open

Feature Request: Allow bundling of analytics scripts #163

jawnsy-humane opened this issue Jan 8, 2025 · 1 comment

Comments

@jawnsy-humane
Copy link

For production builds, this library loads an additional analytics script from /_vercel/insights/script.js on the current domain. For development builds, this library loads an analytics script from https://va.vercel-scripts.com/v1/script.debug.js.

export const DEV_SCRIPT_URL =
'https://va.vercel-scripts.com/v1/script.debug.js';
export const PROD_SCRIPT_URL = '/_vercel/insights/script.js';

This has a few implications:

  • The latter URL (on va.vercel-scripts.com) needs to be added to the content security policy, but only applies in development mode
  • The analytics script can be updated at any time without changes to the application

It would be nice if the scripts themselves could be bundled in the npm package, so that everything is upgraded reproducibly.

@jawnsy-humane
Copy link
Author

A workaround (for Next.js) is to create some rewrites and use the scriptSrc prop to load the script through the local server, proxying to va.vercel-scripts.com.

const nextConfig = {
  async rewrites() {
    return [
      /* Proxy to Vercel Scripts for Analytics and Speed Insights,
       * so that the CSP does not need to include an external domain.
       */
      {
        source: '/vercel/insights.js',
        destination: 'https://va.vercel-scripts.com/v1/script.js',
      },
      {
        source: '/vercel/insights.debug.js',
        destination: 'https://va.vercel-scripts.com/v1/script.debug.js',
      },
      {
        source: '/vercel/speed-insights.js',
        destination: 'https://va.vercel-scripts.com/v1/speed-insights/script.js',
      },
      {
        source: '/vercel/speed-insights.debug.js',
        destination: 'https://va.vercel-scripts.com/v1/speed-insights/script.debug.js',
      },
    ];
  }
};

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

No branches or pull requests

1 participant