Search code examples
node.jstypescriptnext.jsvercelnext.js13

"TypeError: Failed to parse URL from (URL).vercel.app/api/getMessages" when building Next.js 13 with TypeScript on Vercel


Hello fellow StackOverflow Community: I'm facing this error "TypeError: Failed to parse URL from next-chat-lenx51hr5-gregory-buffard.vercel.app/api/getMessages" when trying to build my Next.js 13 app using TypeScript on Vercel. On localhost:3000 with npm run dev the app works good, but when trying to build it on Vercel the error appears. I'm a newbie noob in Next and I would really appreciate any kind of help. The error also contains a few sub-errors which are those (build log from Vercel):

TypeError: Failed to parse URL from next-chat-lenx51hr5-gregory-buffard.vercel.app/api/getMessages
22:34:29.292        at Object.fetch (node:internal/deps/undici/undici:11118:11)
22:34:29.292        at async HomePage (/vercel/path0/next-chat/.next/server/app/page.js:552:18) {
22:34:29.292      [cause]: TypeError [ERR_INVALID_URL]: Invalid URL

You can also review the getMessages.ts file down below:

import type { NextApiRequest, NextApiResponse } from "next";
import redis from "../../redis";
import { Message } from "../../typings";

type Data = {
  messages: Message[];
};

type ErrorData = {
  body: string;
};

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data | ErrorData>
) {
  if (req.method !== "GET") {
    res.status(405).json({ body: "Method Not Allowed" });
    return;
  }

  const messagesRes = await redis.hvals("messages");
  const messages: Message[] = messagesRes
    .map((message) => JSON.parse(message))
    .sort((a, b) => b.created_at - a.created_at);

  res.status(200).json({ messages });

Again, Thank You for any kind of help. Also, if you need any additional files to understand the problem, please feel free to ask.


Solution

  • I have resolved the issue by hosting the app on a dev server instead of sending it production- completely skipping the "build" errors and problems. Thanks for everyone's interest and contributions!