jitsu icon indicating copy to clipboard operation
jitsu copied to clipboard

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

Open Ali-hd opened this issue 1 year ago • 5 comments

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

Ali-hd avatar Aug 19 '24 18:08 Ali-hd

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...

vklimontovich avatar Aug 20 '24 16:08 vklimontovich

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.

Ali-hd avatar Aug 20 '24 18:08 Ali-hd

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

vklimontovich avatar Aug 20 '24 19:08 vklimontovich

No, could not reproduce it locally

Ali-hd avatar Aug 20 '24 20:08 Ali-hd

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

  • Follow Cloudflare docs. Maybe, the proper setup will fix the issue
  • Check if the jitsu package was upgraded automatically and pin it to a last working version

vklimontovich avatar Aug 21 '24 15:08 vklimontovich

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

github-actions[bot] avatar Sep 06 '24 01:09 github-actions[bot]

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

github-actions[bot] avatar Sep 21 '24 01:09 github-actions[bot]