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

web-vitals extension loads script from incorrect URL #1346

Closed
Jacksondr5 opened this issue Aug 7, 2024 · 4 comments · Fixed by #1365
Closed

web-vitals extension loads script from incorrect URL #1346

Jacksondr5 opened this issue Aug 7, 2024 · 4 comments · Fixed by #1365
Assignees

Comments

@Jacksondr5
Copy link

Jacksondr5 commented Aug 7, 2024

Hello, I'm encountering an issue where PostHog is attempting to load the web-vitals script, but is using an incorrect URL: /ingest/ingest/static/web-vitals.js?v=1.154.2. This does proxy through to posthog.com (I see some ASCII art of a hedgehog), but returns a 404.

I'm running a NextJS app with rewrites as a reverse proxy. Specifically, these:

next.config.js
{
  source: '/ingest/static/:path*',
  destination: 'https://eu-assets.i.posthog.com/static/:path*',
},
{
  source: '/ingest/:path*',
  destination: 'https://eu.i.posthog.com/:path*',
},
{
  source: '/ingest/decide',
  destination: 'https://eu.i.posthog.com/decide',
},

My calls to the /ingest/decide and /ingest/e endpoints are working properly.

Looking through the code, I think the issue may be this call to endpointFor. When I run through the code in the Chrome debugger, I can see that endpointFor gets called and prepends the extra /ingest that's causing the issue:
image

Happy to contribute a PR to a solution. Not entirely sure what that solution should be.

My thoughts are to replace

this.instance.requestRouter.loadScript(
this.instance.requestRouter.endpointFor('assets', `/static/web-vitals.js?v=${Config.LIB_VERSION}`),
(err) => {

with

this.instance.requestRouter.loadScript(
    `/static/web-vitals.js?v=${Config.LIB_VERSION}`,

This would mean that endpointFor only gets called once, by loadScript.

There are other extensions that do this, instead of also calling endpointFor themselves (tracing-headers, toolbar, surveys). However, autocapture does make the same kind of call that web-vitals does, so I'm not sure if this is correct.

@junioryono
Copy link

+1

I am also receiving this error for web vitals. I have a NextJS app with rewrites as a reverse proxy.

@junioryono
Copy link

The simplest solution as of now is to add the path in your list of rewrites

next.config.js

async rewrites() {
    return [
      {
        source: "/ingest/ingest/static/:path*",
        destination: "https://us-assets.i.posthog.com/static/:path*",
      },
      {
        source: "/ingest/static/:path*",
        destination: "https://us-assets.i.posthog.com/static/:path*",
      },
      {
        source: "/ingest/:path*",
        destination: "https://us.i.posthog.com/:path*",
      },
      {
        source: "/ingest/decide",
        destination: "https://us.i.posthog.com/decide",
      },
    ];
  },

@pauldambra
Copy link
Member

great write up @Jacksondr5 thank you! Super clear

really we should make endpointFor safe for multiple calls but in the meantime I've opened #1365 that will resolve this

@pauldambra pauldambra self-assigned this Aug 19, 2024
@lyonz
Copy link

lyonz commented Nov 11, 2024

I just updated to newest version and still have this issue, has it been resolved? or do I need to use @junioryono workaround? Thank you

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 a pull request may close this issue.

4 participants