Skip to content

Conversation

@darkobits
Copy link

For context, this issue was discovered using:

This proposal fixes an issue when using next-ws with App Router, wherein route handlers are compiled in a way such that the default export can be a Promise that resolves to an object containing the actual handlers (re: named exports like SOCKET). When next-ws imports a route module and tries to access properties like:

module?.default?.routeModule?.userland?.SOCKET

It fails because module.default can be a Promise, not an object, resulting in the following error:

[next-ws] /path/name/here does not export a SOCKET handler

This patch addressed that issue and next-ws is now working brilliantly in my application. If the maintainers would prefer to address this in a different way, please just consider this a bug report. 😄

And finally, thank you @apteryxxyz and the rest of the team for this project! ❤️

@darkobits darkobits requested a review from k0d13 as a code owner April 10, 2025 00:40
@changeset-bot
Copy link

changeset-bot bot commented Apr 10, 2025

⚠️ No Changeset found

Latest commit: 0242860

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Owner

@k0d13 k0d13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, though it is still unclear when default is a promise, as tests pass.
Anyway, I think I would prefer if the awaiting of default be moved to the getSocketHandler then awaited, along the lines of:

export async function getSocketHandler(routeModule: RouteModule) {
  if (routeModule?.default instanceof Promise) {
    return getSocketHandler(await routeModule.default);
  }

  return (
    routeModule?.default?.routeModule?.userland?.SOCKET ??
    routeModule?.routeModule?.userland?.SOCKET ??
    routeModule?.default?.handlers?.SOCKET ??
    routeModule?.handlers?.SOCKET
  );
}

export interface RouteModule {
  default?: Promise<RouteModule> | RouteModule;
  routeModule?: { userland?: RouteModule['handlers'] };
  handlers?: { SOCKET?: SocketHandler };
}

Also missing changeset and needs formatting

@k0d13
Copy link
Owner

k0d13 commented May 4, 2025

Stale

@k0d13 k0d13 closed this May 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants