Fails with `Error: The script will never generate a response.` on cloudflare pages
This is similar to #865, however unlike in that issue I create the postgres client on each request and still get following error on cloudflare pages with "compatibility_flags": ["nodejs_compat"]
✘ [ERROR] A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
[wrangler:inf] GET /api/test 500 Internal Server Error (8ms)
✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.
Using it as follows
app.get("/api/test", async (c) => {
console.log("test route");
const sql = postgres(c.env.DATABASE_URL);
const users = await sql`
select *
from users`;
// users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
return c.json(users);
});
did you manage to fix it?
@ssebastianoo not in pages, but in workers it works with hyperdrive. But still too many connection errors, so I ended up migrating to node-pg
I am facing the same issue today, after a few hours investigation, it seems like the cause is that Cloudflare Worker forbids the same connection client across multiple requests.
so instead having a single connect object exported and imported everywhere
const { env } = await getCloudflareContext({ async: true });
export const sql = postgres(env.HYPERDRIVE.connectionString, { max: 5, fetch_types: false });
in ended up making this function
export const getDb = async () => {
const { env } = await getCloudflareContext({ async: true });
const sql = postgres(env.HYPERDRIVE.connectionString, { max: 5, fetch_types: false });
return sql;
}
and just create a new connect object in each request function
const sql = await getDb();
await sql`xxxxxxxxxx`
Not sure how much it impacts the performance though.