emilkowalski / sonner

An opinionated toast component for React.
https://sonner.emilkowal.ski
MIT License
8.59k stars 270 forks source link

A Node.js API is used (MessageChannel) error when building nextjs app on docker #498

Open ezeamin opened 1 day ago

ezeamin commented 1 day ago

Describe the feature / bug 📝:

These warnings show when building a nextjs app (v14), with npm, pnpm, and bun on a docker container (nextjs detects it as "edge", so it shows the messages).

home:build: ../../node_modules/scheduler/cjs/scheduler.production.min.js
home:build: A Node.js API is used (MessageChannel at line: 14) which is not supported in the Edge Runtime.
home:build: Learn more: https://nextjs.org/docs/api-reference/edge-runtime
home:build:
home:build: Import trace for requested module:
home:build: ../../node_modules/scheduler/cjs/scheduler.production.min.js
home:build: ../../node_modules/scheduler/index.js
home:build: ../../node_modules/react-dom/cjs/react-dom.production.min.js
home:build: ../../node_modules/react-dom/index.js
home:build: ../../node_modules/sonner/dist/index.mjs
home:build: ../../packages/utilities/src/getToken.ts
home:build: ../../packages/utilities/index.ts

I'm trying to replicate the bug in an independent project, but I can't find how. However, if it helps, I will leave here the getToken() code, where this error is showing according to the stacktrace.

import { toast } from 'sonner';

import { isClient } from './utils';
import { useSessionStore } from 'global-stores';

import type { GetTokenParams } from './interface';

export const getToken = async (params?: GetTokenParams): Promise<string> => {
  const { avoidRedirect = false, ignoreCurrentAT = false } = params ?? {};

  let token: string | null = null;

  // Token is available on client side store
  if (isClient() && !ignoreCurrentAT) {
    token = useSessionStore.getState().accessToken;
  }

  // Refresh token (Client side)
  if (isClient() && !token) {
    try {
      token = (await fetchTokenClient()).token;
    } catch (e) {
      if (avoidRedirect) return '';

      console.error('⚠️ Error fetching token, will redirect.', e);

      toast.error('Necesita volver a iniciar sesión');
      const currentLocation = window.location.pathname;

      setTimeout(() => {
        window.location.href = `/auth${currentLocation !== '/' ? `?redirectTo=${currentLocation}` : ''}`;
      }, 2000);
    }
  }

  // Refresh token (Server side)
  if (!isClient() && !token) {
    try {
      token = (await fetchTokenServer(ignoreCurrentAT)).token;
    } catch (e) {
      console.error('⚠️ Error fetching token', e);
    }
  }

  return token ?? '';
};
ezeamin commented 1 day ago

It also happens with:

home:build: ../../node_modules/scheduler/cjs/scheduler.production.min.js
home:build: A Node.js API is used (setImmediate at line: 11) which is not supported in the Edge Runtime.
home:build: Learn more: https://nextjs.org/docs/api-reference/edge-runtime