Skip to content

Conversation

@Antonio-Bennett
Copy link
Contributor

@Antonio-Bennett Antonio-Bennett commented Jan 28, 2026

Summary

  • Attach an onrejected handler when calling the async SSR render thenable to mark the underlying render promise as handled
  • Treat Redirect thrown during render as a real redirect response in render_page
  • Keeps normal redirect handling intact elsewhere

Context

The async render path returns a thenable from Svelte’s server renderer. Calling .then without an onrejected means the underlying async render promise can reject (e.g. Redirect) before any handler is attached, which triggers Node’s unhandled rejection detection. Passing an onrejected handler attaches a rejection handler directly to the underlying promise.

Separately, when a Redirect is thrown during render (e.g. via a remote query used in a component), it currently falls into the generic error handler and returns a 500. Handling Redirect in render_page ensures the response redirects instead.

How This Relates To Svelte

The unhandled rejection itself originates in Svelte’s SSR runtime (Renderer.run() does not attach a rejection handler or tie the async work to renderer.promise). The Svelte PR below addresses that root cause; this Kit PR handles the redirect response once the render error propagates.

Notes

AI-Generated PR: This PR was created with assistance from AI (OpenCode) to help move the discussion forward on issue #14883. While this solution provides a functional workaround, it may not represent the optimal or preferred approach for the SvelteKit maintainers. Please feel free to suggest alternative implementations or close this PR if a better solution is identified.

Related

Test Plan

  • Repro app: redirect thrown from remote query during render no longer triggers unhandled rejection and now redirects properly

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link

changeset-bot bot commented Jan 28, 2026

🦋 Changeset detected

Latest commit: 45c99a9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

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

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

This isn't really the correct solution here. The problem is that somewhere in Svelte or SvelteKit, when a promise is created and stashed, it's not having a .catch handler attached to it. It's likely the promise is await-ed somewhere else, but it's after a microtask, so if the promise rejects it's treated as unhandled.

So basically there's somewhere we're missing a .catch(() => {}) and we just have to find that spot.

@Antonio-Bennett
Copy link
Contributor Author

Yeah def thought so as well. Just put this out for an immediate workaround for now. Will keep digging around to see if I can find where that catch isn't being added! :)

Co-authored-by: Elliott Johnson <hello@ell.iott.dev>
@Antonio-Bennett
Copy link
Contributor Author

@elliott-with-the-longest-name-on-github thanks for your feedback and review! Committed the change :)

@teemingc teemingc changed the title fix: handle unhandledRejection from remote query redirects (#14883) fix: handle unhandledRejection from remote query redirects Feb 5, 2026
Copy link
Member

@teemingc teemingc left a comment

Choose a reason for hiding this comment

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

Could we add a test for this?

@svelte-docs-bot
Copy link

@Antonio-Bennett
Copy link
Contributor Author

@teemingc added, is it satisfactory?

@Antonio-Bennett
Copy link
Contributor Author

@teemingc Hey sorry don't want to be a bother, know you have a lot on your plate just wondering if you could take a look in case this slipped by

@teemingc
Copy link
Member

teemingc commented Feb 9, 2026

So sorry about that! It definitely did slip by 🤦🏼 It's 1 AM here so I'll take a look at this tomorrow. Thanks!

@teemingc teemingc changed the title fix: handle unhandledRejection from remote query redirects fix: handle remote query redirects Feb 10, 2026
@teemingc
Copy link
Member

teemingc commented Feb 10, 2026

I was a little confused when the test still passed even when I commented out the fix. Turns out the console.log from the original issue is caused by us passing the redirect error to the handleError hook. The redirect always worked but if you had no handleError hook, the default one would try to log the .message property of a Redirect, and that would trigger the error.

I've cleaned it up a bit so that the test does fail if a Redirect somehow slips through. I've also added the proper status code if someone uses error in the remote function and that gets thrown

@teemingc teemingc merged commit 159aece into sveltejs:main Feb 10, 2026
43 of 44 checks passed
@github-actions github-actions bot mentioned this pull request Feb 10, 2026
@Antonio-Bennett
Copy link
Contributor Author

@teemingc thanks for taking a look and bringing it across the finish line! 🐐

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.

Remote query with redirect produce 500 undefined error

3 participants