sveltejs / sveltejs/kit

Allow work to be scheduled after a response has been sent

Open
#13,157 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

There are cases when we would like to schedule some work after a response has been sent. For my use case, I am writing a Discord bot that performs potentially long-running work. It is thus necessary for me to acknowledge an incoming slash command immediately (i.e., send an HTTP response) and defer a more complete response later (i.e., eventually via the Discord API).

Currently, the Discord API provides an endpoint that lets us edit the original response for a slash command response. Unfortunately, as there is no way to dispatch the work after a response has been sent, there is a fairly common race condition where editing the original response may come first before the original response is even returned from the route handler. Since the slash command response is yet to be received by Discord, invoking the API endpoint for editing the original response (rightly) results in a 404.

Describe the proposed solution

SvelteKit needs an API for scheduling work after a response has been sent. In the context of my use case, this makes sure that editing the original response comes after the original one has been sent. I believe a solution like the after API in Next.js is ideal here.

Alternatives considered

Currently, the way I hacked through this problem is to introduce an artificial timeout. This is far from ideal, of course...

import { json } from '@sveltejs/kit';
import { setTimeout } from 'node:timers/promises';

// +server.js
async function POST(...) {
    // Handle the slash command and preserve some context.
    const { result, ctx } = await handleSlashCommand(...);

    // Detached promise runs in the background.
    void (async () => {
        // HACK: Wait for the response to be sent.
        await setTimeout(1000);

        // Use the existing context to edit the response eventually.
        await doPostResponseWork(ctx, ...);
    })();

    // Send the initial response immediately.
    return json(result);
}
Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with a +server.js route handler and trace how its response is sent before the proposed deferred work runs. Define how the scheduling API interacts with the response lifecycle, then verify that follow-up work cannot race the initial response, using the Discord example as the completion case.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.