blitz-js / legacy-framework

MIT License
3 stars 2 forks source link

Infinite loop in queries in non localhost #14

Open aefmind opened 3 years ago

aefmind commented 3 years ago

What is the problem?

Infinite loop in queries (useQuery, usePaginatedQuery...). Problem is even present in a clean project generated by blitz new command, accessing from custom domain.

When using blitz dev, sometimes it stops after several reloads. But using blitz build && blitz start loop is always endless.

Paste all your error logs here:

No errors are shown. But content is reloaded infinitely and server console show queries log. Example (in a clean blitz new project):

00:45:06.270 INFO getCurrentUser() Starting with input: null
00:45:06.273 INFO getCurrentUser() Finished: resolver:0ms serializer:1ms total:1ms

00:45:13.466 INFO getCurrentUser() Starting with input: null
00:45:13.468 INFO getCurrentUser() Finished: resolver:0ms serializer:1ms total:1ms

Paste all relevant code snippets here:

Code needed to face the problem is the one generated by blitz new in app/pages/index.ts

What are detailed steps to reproduce this?

  1. blitz new myproject
  2. cd myproject
  3. blitz dev or: blitz build && blitz start
  4. Go to http://IP:3000

Run blitz -v and paste the output here:

blitz: 0.41.1 (global)
blitz: 0.41.1 (local)

  Package manager: yarn
  System:
    OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (1) x64 Intel Core Processor (Haswell, no TSX)
    Memory: 414.99 MB / 3.75 GB
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 14.18.1 - /usr/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 6.14.15 - /usr/bin/npm
    Watchman: Not Found
  npmPackages:
    @prisma/client: 3.3.0 => 3.3.0
    blitz: 0.41.1 => 0.41.1
    prisma: 3.3.0 => 3.3.0
    react: alpha => 18.0.0-alpha-6bce0355c-20211031
    react-dom: alpha => 18.0.0-alpha-6bce0355c-20211031
    typescript: ~4.3 => 4.3.5

UPDATED:

flybayer commented 3 years ago

@aefmind what are you using to access it via different hostname? Something like ngrok?

aefmind commented 3 years ago

@flybayer No. I've hosted it in both OVH VPS (with Ubuntu and Debian) and in my Windows PC.

Even it fails when using 192.168.0.x IP address to access from local network (or same PC) to blitz app hosted in my Windows PC.

VPS has attached a domain.

aefmind commented 3 years ago

@flybayer using ngrok, it also fails.

Doing SSH port forwarding (from port in remote server to a local port in my PC) makes it works by simply being able to access using localhost. But using IP address or domain, it has the infinite loop.

I've tried with blitz start -H <hostname> whitout success.

aefmind commented 3 years ago

@flybayer I managed to workaround this issue creating two files (dev.js and start.js) and running:

The contents of these files are:

start.js:

"use strict"

var _path = require("path")

var _startServer = require("next/dist/server/lib/start-server").default

const dir = (0, _path).resolve(".")

const port = 3000
const host = "domain.com"
const appUrl = `http://${host === '0.0.0.0' ? 'localhost' : host}:${port}`;

_startServer({ dir }, port, host)
  .then(async (app) => {
    console.log(`started server on ${host}:${port}, url: ${appUrl}`)
    await app.prepare()
  })
  .catch((err) => {
    console.error(err)
    process.exit(1)
  })

and dev.js:

"use strict"

var _path = require("path")

var _startServer = require("next/dist/server/lib/start-server").default
var _output = require("next/dist/build/output")

const dir = (0, _path).resolve(".")

const port = 3000
const host = "domain.com"
const appUrl = `http://${host === '0.0.0.0' ? 'localhost' : host}:${port}`;

_startServer(
  {
    dir,
    dev: true,
    isNextDevCommand: true,
  },
  port,
  host
)
  .then(async (app) => {
    _output.startedDevelopmentServer(appUrl, `${host || "0.0.0.0"}:${port}`)
    // Finalize server bootup:
    await app.prepare()
  })
  .catch((err) => {
    if (err.code === "EADDRINUSE") {
      let errorMessage = `Port ${port} is already in use.`
      console.error(errorMessage)
    } else {
      console.error(err)
    }
    process.nextTick(() => process.exit(1))
  })

It is working fine. So the error is in blitz cli.

flybayer commented 2 years ago

Thanks @aefmind for your help! We'll soon be removing the blitz cli wrapper, so probably this will be automatically fixed by that change then.

yanniz0r commented 2 years ago

I ran into the same issue when using blitzjs in a kubernetes environment. I was able to work around it by manually specifying the host header in my service using the following nginx ingress annotation on the corresponding service:

nginx.ingress.kubernetes.io/upstream-vhost: "localhost:3000"

Maybe this helps some folks running in the same issue :) I guess this also applies to other environments allowing you to overwrite the host header.

PumAmurcat commented 1 year ago

Unfortunately this problem is still present in the newest version. @martinmuzatko is having the same problem.

flybayer commented 1 year ago

@PumAmurcat can you open a new issue in the https://github.com/blitz-js/blitz repo?

silencej commented 1 year ago

I am also having this issue. I am using node v18.14.1 just FYI. I may help to get to a Minimum Working Example when I have time.