Skip to content

[Feat]: Better DX for Abort events聽#1753

Description

@tri2820

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 馃槸

When the request is aborted, the abort event is not fired within server API route.

Example server API route

import type { APIEvent } from "@solidjs/start/server";
export async function GET(event: APIEvent) {

    event.request.signal.addEventListener("abort", () => {
        console.log('abort detected')
    })

    let i = 0;
    while (i < 20) {
        console.log('looping', i++, event.request.signal.aborted)
        await new Promise(resolve => setTimeout(resolve, 1000))
    }

    return new Response(null, { status: 200 })
}

Example browser triggers

<button
onClick={() => {
  controller = new AbortController();
  fetch("/api/test", {
    method: "GET",
    signal: new AbortController().signal,
  })
    .then((response) => {
      console.log("response", response);
    })
    .catch((error) => {
      console.log("error", error);
    });
}}
>
Call
</button>
<button
onClick={() => {
  console.log("controller", controller);
  controller?.abort("I am aborting this request");
  controller = undefined;
}}
>
Abort
</button>

Expected behavior 馃

When user cancel the request in browser, the abort event should be fired.

Steps to reproduce 馃暪

Steps:

  1. Create an API route
  2. Add abort listener inside the API route
  3. In the browser, fetch & abort
  4. Notice that the abort event is not fired inside the API route

Context 馃敠

I'm building an AI chat app that streams the token from the AI provider back to the user.
Currently when the user clicks aborting the request, the api route could not detect that and keep streaming from the AI provider, wasting token.

Not sure if related but Next.js used to have a similar issue vercel/next.js#48682

Your environment 馃寧

Linux denpa 6.12.10-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 18 Jan 2025 02:26:57 +0000 x86_64 GNU/Linux

bun --version
1.2.0

node --version
v23.4.0

"dependencies": {
    "@solidjs/meta": "^0.29.4",
    "@solidjs/router": "^0.15.0",
    "@solidjs/start": "^1.0.11",
    "solid-js": "^1.9.2",
    "vinxi": "^0.4.3"
  },

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions