Does Smart Placement support Hyperdrive? I'm using Next.js with Postgres

I successfuly deployed a page using Next.js with next-on-pages connected to a Postgres database using Drizzle/Postgres.js and Hyperdrive. Everything works, except that the latency is still very high. I have enabled Smart Placement but it still takes >700ms to respond on a route with just one simple database query. My current deployment on Kubernetes takes ~150ms to do the same. (I'm measuring round trip to my pc). The "cf-placement" header shows that Smart Placement didn't trigger. Does it work with Hyperdrive? Is there anything I can do to improve this latency?
3 Replies
Gabe
Gabe4mo ago
how did you get postgres.js to work with hyperdrive did you bundle the node dependencies? I couldn't get pages to work with postgres in the short amount of time I tried it
lbguilherme
lbguilherme4mo ago
1. Add this to nextconfig:
webpack: (config, { webpack }) => {
config.plugins.push(
new webpack.NormalModuleReplacementPlugin(/^node:stream$/, "readable-stream")
);

config.externals.push({ "cloudflare:sockets": "global __edge_sockets" });

return config;
},
webpack: (config, { webpack }) => {
config.plugins.push(
new webpack.NormalModuleReplacementPlugin(/^node:stream$/, "readable-stream")
);

config.externals.push({ "cloudflare:sockets": "global __edge_sockets" });

return config;
},
2. build with next-on-pages 3. run sed -i '1s/^/import * as cloudflare_sockets from \"cloudflare:sockets\"\\nglobalThis.__edge_sockets = cloudflare_sockets;\\n/' .vercel/output/static/_worker.js/index.js 4. deploy with wrangler there are a few more tricks to get it working on dev mode as well. Either way, I can't go to production with this without Smart Placement working
Gabe
Gabe4mo ago
@lbguilherme Thank you!
Want results from more Discord servers?
Add your server