Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow work to be scheduled after a response has been sent #13157

Open
BastiDood opened this issue Dec 13, 2024 · 3 comments
Open

Allow work to be scheduled after a response has been sent #13157

BastiDood opened this issue Dec 13, 2024 · 3 comments
Labels
feature / enhancement New feature or request

Comments

@BastiDood
Copy link
Contributor

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

@elliott-with-the-longest-name-on-github
Copy link
Contributor

I think this would have to be something the adapter would have to implement... waiting until after a request is served isn't possible on all platforms.

@eltigerchino
Copy link
Member

eltigerchino commented Dec 16, 2024

For reference, our officially supported adapters:

@eltigerchino eltigerchino added the feature / enhancement New feature or request label Dec 16, 2024
@BastiDood
Copy link
Contributor Author

The Node adapter probably just works if you choose not to await some work while the server still runs after the response is sent.

Yup! This is my current workaround, too. Though, I would prefer a cleaner API that hooks into the request-response lifecycle such that the async work only gets triggered when the response has finished writing to the socket. The fire-and-forget promise doesn't have that guarantee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants