Skip to content

Conversation

@robinbraemer
Copy link

Problem

When calling redirect() in remote functions, SvelteKit crashes with:

TypeError: Cannot convert undefined or null to object
    at Object.keys (<anonymous>)
    at serialize_refreshes (packages/kit/src/runtime/server/remote.js:214:14)

This happens because:

  1. Remote function calls redirect(302, '/some-path')
  2. SvelteKit catches the Redirect exception and tries to create a JSON response
  3. During response creation, it calls serialize_refreshes() to handle any pending refreshes
  4. serialize_refreshes assumes state.refreshes is always an object, but it can be null/undefined
  5. Object.keys(refreshes) throws when refreshes is null/undefined

Solution

Add a fallback to an empty object when state.refreshes is null/undefined:

const refreshes = /** @type {Record<string, Promise<any>>} */ (state.refreshes || {});

Testing

  • ✅ Remote function redirects now work without crashing
  • ✅ Normal remote function calls still work as expected
  • ✅ The fix is minimal and doesn't affect existing functionality

Reproduction

// This would previously cause a 500 error
export const myRemoteFunction = query(async () => {
  redirect(302, '/auth/logout');
});

Impact

This is a bug fix that allows remote functions to use redirects as intended, without breaking existing functionality.

… null

Fixes issue where remote function redirects would crash with 'TypeError: Cannot convert undefined or null to object' when calling Object.keys() on undefined state.refreshes.

This happens when redirect() is called in remote functions and serialize_refreshes tries to process refresh data that doesn't exist.

The fix adds a fallback to an empty object when state.refreshes is null/undefined.
@changeset-bot
Copy link

changeset-bot bot commented Sep 10, 2025

⚠️ No Changeset found

Latest commit: adc4a89

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.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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

@svelte-docs-bot
Copy link

@dummdidumm
Copy link
Member

Thank you, the fix is correct - but #14400 also fixes this while adding tests+changeset, therefore closing in favor of that

@Rich-Harris
Copy link
Member

merged #14400, closing — thanks

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.

3 participants