next-plugin-websocket icon indicating copy to clipboard operation
next-plugin-websocket copied to clipboard

Support new the app directory's Route Handlers of Next.js v13.3

Open yoieh opened this issue 1 year ago • 1 comments

Expected working example:

import { NextApiHandler } from "next";
import { NextWebSocketHandler } from "next-plugin-websocket";

export const socket: NextWebSocketHandler = (client, req) => {
  console.log("Client connected");

  client.on("message", (msg) => {
    client.send(msg);
  });

  client.on("close", () => {
    console.log("Client disconnected");
  });
};

export async function GET(request: Request) {
  return new Response("Upgrade Required", {
    status: 426,
  });
}

After PR #6 has been merged it will give an error if you try and use the above example.

error - unhandledRejection: PageNotFoundError: Cannot find module for page: /api/ws
    at findPagePathData (.../node_modules/next/dist/server/dev/on-demand-entry-handler.js:272:15)
    at async Object.ensurePage (.../node_modules/next/dist/server/dev/on-demand-entry-handler.js:444:38)
    at async Server.<anonymous> (.../node_modules/next-plugin-websocket/dist/index.js:88:13) {
  code: 'ENOENT'
}

It only works with the old pages directory.

yoieh avatar Apr 10 '23 12:04 yoieh