Closed domluna closed 1 year ago
See issue #12: Next/Vercel CLI don't support WebSockets in local dev currently. They do apparently work when deployed which makes it even more frustrating IMO.
I'm running into this issue when deployed though. Locally this works fine for me with sveltekit
Are you deploying this as a Vercel Edge Function (via export const config = { runtime: 'edge' };
) or a traditional Serverless Function? A simple repro could be helpful.
using serverless. would this not work with that? only on edge?
I'm also having the same issue. Got the error locally, adding ws
to my project fixed it, but when deployed to Vercel (Serverless not edge) the error is there. Works fine on Cloudflare pages. I'm using SvelteKit as well.
fyi using edge functions works. I was having other issues before getting edge to work, which is why I was trying to do it with just serverless
A colleague has reproduced this issue on Vercel's non-Edge serverless functions. We're looking at what we can do it about it.
In the meantime, it should be possible to use ordinary node-postgres (npm install pg @types/pg
) there.
Error occurred prerendering page "/api/export/packages". Read more: https://nextjs.org/docs/messages/prerender-error
18:03:19.403 | Error: All attempts to open a WebSocket to connect to the database failed. If using Node, please install the `ws` package (or simply use the `pg` package instead).
18:03:19.403 | at ws (/vercel/path0/.next/server/chunks/7280.js:3961:44)
18:03:19.404 | info - Generating static pages (11/11)
18:03:19.405
I'm getting an error message about not being able to reach the websock. DATABASE_URL is set.
Unfortunately pg seems broken on vercel too...
I am also having this issue with edge functions, adding the ws library does not appear to fix the problem when running locally with vercel dev
Hi all. I know Vercel have made recent updates to the vercel
CLI tool, and npx vercel dev
now works for me for both Edge Functions and Serverless Functions.
import { Pool } from '@neondatabase/serverless';
export const config = {
runtime: 'edge',
regions: ['fra1'], // fra1 = Frankfurt: pick the Vercel region nearest your Neon DB
};
export default async (req: Request, ctx: any) => {
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const longitude = parseFloat(req.headers.get('x-vercel-ip-longitude') ?? '-122.47');
const latitude = parseFloat(req.headers.get('x-vercel-ip-latitude') ?? '37.81');
const { rows: sites } = await pool.query(`
SELECT
id_no, name_en, category,
'https://whc.unesco.org/en/list/' || id_no || '/' AS link,
location <-> st_makepoint($1, $2) AS distance
FROM whc_sites_2021
ORDER BY distance
LIMIT 10`,
[longitude, latitude]
);
ctx.waitUntil(pool.end());
return new Response(JSON.stringify({ longitude, latitude, sites }, null, 2));
}
import { Client, neonConfig } from '@neondatabase/serverless';
import { WebSocket } from 'undici';
import type { VercelRequest, VercelResponse } from '@vercel/node';
neonConfig.webSocketConstructor = WebSocket;
export default async (req: VercelRequest, res: VercelResponse) => {
const client = new Client(process.env.DATABASE_URL);
await client.connect();
const longitude = parseFloat(req.headers['x-vercel-ip-longitude'] as string ?? '-122.47');
const latitude = parseFloat(req.headers['x-vercel-ip-latitude'] as string ?? '37.81');
const { rows: sites } = await client.query(`
SELECT
id_no, name_en, category,
'https://whc.unesco.org/en/list/' || id_no || '/' AS link,
location <-> st_makepoint($1, $2) AS distance
FROM whc_sites_2021
ORDER BY distance
LIMIT 10`,
[longitude, latitude]
);
res.status(200).json({ longitude, latitude, sites });
await client.end();
}
Please shout if you're still having trouble, otherwise I'll close the issue.
so the error I had before is gone which is cool. I think now the trouble is with combining edge and serverless functions. Which I suppose is a vercel problem. Is it worthwhile to set the websocket constructor in an edge function as well?
Glad the previous error is resolved.
No, there's no need to set webSocketConstructor
in an Edge Function, and trying to do so is likely to raise an error.
I'm getting the following error when using this on vercel
I also got this locally and then install ws and then it went away.
ws
should be available on Vercel so I'm not sure what the issue this. This is my package.json