create-t3-app icon indicating copy to clipboard operation
create-t3-app copied to clipboard

bug: Seems tRPC (or its config) doesn't work when internet is off on local machine?

Open sdoxsee opened this issue 2 years ago • 3 comments

Provide environment information

npx envinfo --system --binaries

  System:
    OS: macOS 13.0.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 589.69 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm

ct3a from package.json

  "ct3aMetadata": {
    "initVersion": "6.10.0"
  }

image

Describe the bug

tRPC hangs on the hello endpoint. I just get Loading tRPC query...

I don't see an error but no success either.

To reproduce

Start up a freshly generated app (npm run dev) without the internet, open http://localhost:3000, I just get Loading tRPC query....

Additional information

Turning on the internet solves this. Does something require the internet? There are many times that I work offline and this prevents progress.

sdoxsee avatar Nov 18 '22 01:11 sdoxsee

image Just tested it, it works as expected. I'm using Linux and the same ct3a version. Tried both node v19 and v16.

ronanru avatar Nov 18 '22 08:11 ronanru

Can reproduce on Windows 10.

image

"ct3aMetadata": {
    "initVersion": "6.10.0"
  }

AyanavaKarmakar avatar Nov 18 '22 09:11 AyanavaKarmakar

Thanks @ronanru and @AyanavaKarmakar. I tried it on Ubuntu and it worked fine as well. Seems like MacOS and Windows?

sdoxsee avatar Nov 18 '22 15:11 sdoxsee

seems related to: https://tanstack.com/query/v4/docs/guides/network-mode

Since React Query is most often used for data fetching in combination with data fetching libraries, the default network mode is online.

In this mode, Queries and Mutations will not fire unless you have network connection. This is the default mode. If a fetch is initiated for a query, it will always stay in the state (loading, error, success) it is in if the fetch cannot be made because there is no network connection. However, a fetchStatus is exposed additionally.

image

you can modify the behaviour via changing react-query's networkMode configuration src/utils/trpc.ts

export const trpc = createTRPCNext<AppRouter>({
  config() {
    return {
      queryClientConfig: {
        defaultOptions: {
          queries: {
            networkMode:
              process.env.NODE_ENV === "development" ? "always" : "online",
          },
        },
      },
      transformer: superjson,
      links: [
        loggerLink({
          enabled: (opts) =>
            process.env.NODE_ENV === "development" ||
            (opts.direction === "down" && opts.result instanceof Error),
        }),
        httpBatchLink({
          url: `${getBaseUrl()}/api/trpc`,
        }),
      ],
    };
  },
  ssr: false,
});

needim avatar Nov 19 '22 07:11 needim

If you want also mutations to run:

...
queries: {
  networkMode:
    process.env.NODE_ENV === "development" ? "always" : "online",
},
mutations: {
  networkMode:
    process.env.NODE_ENV === "development" ? "always" : "online",
},
...

needim avatar Nov 19 '22 07:11 needim

But, if you wanna develop an offline UX, you should revert it back.

needim avatar Nov 19 '22 07:11 needim

Thanks @needim. That definitely works for me.

Should that be the default for both queries and mutations in create-t3-app? An offline UX wouldn't be the most common scenario.

It also baffles me why those default react-query settings don't seem to affect Linux (or Ubuntu).

sdoxsee avatar Nov 19 '22 13:11 sdoxsee

I didn't know about this behaviour in tRPC either. But I don't think we should override the default behaviour as that would break peoples expectations.

c-ehrlich avatar Nov 19 '22 17:11 c-ehrlich

@c-ehrlich it is not a tRPC behavior actually. It is @tanstack/react-query configuration. (since tRPC uses react-query under the hood it looks like that) I agree with you about not changing the default behavior. 👍

and I have no idea about this nor do I have a Linux to test this.

It also baffles me why those default react-query settings don't seem to affect Linux (or Ubuntu).

developers should handle network situations like this.

needim avatar Nov 19 '22 17:11 needim

Sounds good. Glad we can at least capture this issue so that other devs can see the solution. Thanks for all your help.

sdoxsee avatar Nov 19 '22 17:11 sdoxsee