jitsucom / jitsu

Jitsu is an open-source Segment alternative. Fully-scriptable data ingestion engine for modern data teams. Set-up a real-time data pipeline in minutes, not days
https://jitsu.com
MIT License
4.11k stars 292 forks source link

Jitsu causing an internal server error with Next JS deployed on cloudflare #1118

Closed Ali-hd closed 1 month ago

Ali-hd commented 2 months ago

Summary

Hi,

I am facing an issue using Jitsu with Next js. I deployed my app to cloudflare pages but the following happens

There is an error returned on any page that uses Jitsu (either @jitsu/jitsu-react or @jitsu/js). The page returns Internal Server Error with no other error info.

When checking the cloudflare deployment logs, I see two errors messages. This:

  "truncated": false,
  "outcome": "ok",
  "scriptName": "pages-worker--3536470-production",
  "diagnosticsChannelEvents": [],
  "exceptions": [],
  "logs": [
    {
      "message": [
        "Error: An error occurred while evaluating the target edge function (__next-on-pages-dist__/functions/index.func.js)"
      ],
      "level": "error",
      "timestamp": 1724090044141
    }
  ],
  "eventTimestamp": 1724090044139,
  "event": {
    "request": {
      "url": "https://jistu-cloudflare-nextjs-bug-reproduce.pages.dev/",
      "method": "GET",
      "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-encoding": "gzip, br",
        "accept-language": "en-US,en;q=0.9,ar;q=0.8",
        "cache-control": "max-age=0",

Or this

{
  "truncated": false,
  "outcome": "ok",
  "scriptName": "pages-worker--3536470-production",
  "diagnosticsChannelEvents": [],
  "exceptions": [],
  "logs": [
    {
      "message": [
        "TypeError: Cannot read properties of undefined (reading '__global__')"
      ],
      "level": "error",
      "timestamp": 1724090106484
    }
  ],
  "eventTimestamp": 1724090106408,
  "event": {
    "request": {
      "url": "https://jistu-cloudflare-nextjs-bug-reproduce.pages.dev/",
      "method": "GET",
      "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-encoding": "gzip, br",
        "accept-language": "en-US,en;q=0.9,ar;q=0.8",
        "cache-control": "max-age=0",

These errors did not help me identify the cause of the issue. I tried running the code on the server side only flags, but the error still occurred.

I also tried using the App Router, it displayed the page, but I can see a server error 500 in the browser network.

Reproduction

Fresh next js Repo deployed to cloudflare pages with jitsu added https://github.com/Ali-hd/jistu-cloudflare-nextjs-bug-reproduce

You can test it in this url https://jistu-cloudflare-nextjs-bug-reproduce.pages.dev

You can visit the app router page here https://jistu-cloudflare-nextjs-bug-reproduce.pages.dev/todo

I don't think jitsu host and writekey values matter, you can use empty values

System configuration and versions

next: v14.2.5 @jitsu/jitsu-react: v1.9.8 @cloudflare/next-on-pages: v1.13.2

vklimontovich commented 2 months ago

Hi @Ali-hd, have you followed all steps from Cloudflare docs? As far as I see there's not Cloudflare calls in next-config.

If you can work on example a little more and make a npm preview script that starts the app in the dev mode, it would be a huge help for us. Let's see if the issue persists in dev mode and take from there. Unfortunately, we don't have resources to test clouflare pages from ground up at the moment...

Ali-hd commented 2 months ago

Hi @vklimontovich, the part I didn't add from Cloudflare docs was not needed for my usage.

Our production app uses many packages and everything is working normally. This issue suddenly happened without any change in code, if we rebuild our production it would trigger the issue. We had previously used jitsu with no issues.

This issue doesn't happen locally on a production build when using the same build command, it only happens on Cloudflare.

I deployed it on preview but it's using the same build command as production, when I try to change it the build breaks.

vklimontovich commented 2 months ago

Were you able reproduce the issue with locally? If so, would you mind committing it to your repo?

Ali-hd commented 2 months ago

No, could not reproduce it locally

vklimontovich commented 2 months ago

Ok, then we need to wait for someone to make the issue reproducible. Meanwhile, here's what I suggest:

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 month ago

This issue was closed because it has been inactive for 14 days since being marked as stale.