aleph.js icon indicating copy to clipboard operation
aleph.js copied to clipboard

Fetching API Routes with `useDeno` causes infinite loop

Open iFwu opened this issue 4 years ago • 5 comments
trafficstars

// api/test.ts
import type { APIRequest } from "https://deno.land/x/aleph/types.ts"

export default async function handler(req: APIRequest) {
  req.status(200).json({ data: 'test' })
}
// pages/index.tsx
import { useDeno } from "https://deno.land/x/aleph/mod.ts";
import React from "https://esm.sh/react";

export default function Home() {
  const testData = useDeno(async () => {
    const { data } = await fetch(
      "http://localhost:8080/api/test",
      { method: "GET" },
    ).then((res) => res.json());
    return data;
  });

  return (
    <div className="page">
      {testData}
    </div>
  );
}

When request http://.../api/test it will send { data: "test" } but after requesting the index page, the server pend all requests.

iFwu avatar Dec 08 '20 05:12 iFwu

@iFwu thanks, I will try you example later.

ije avatar Dec 08 '20 06:12 ije

I have got this error too.

alfredosalzillo avatar Jan 03 '21 11:01 alfredosalzillo

I think it's because the API and the pages are served from the same for await loop.

This prevents the API to be served before the page is rendered, causing a waiting loop locking the execution.

The page is waiting for the response of the API that will be executed after the page is rendered.

alfredosalzillo avatar Jan 03 '21 12:01 alfredosalzillo

Any news on this?

alfredosalzillo avatar Apr 23 '21 07:04 alfredosalzillo

@alfredosalzillo will test later

ije avatar Apr 23 '21 08:04 ije