sveltejs/kit · error

requested(...) can only be called in the context of a comman

Error message

requested(...) can only be called in the context of a command/form remote function

What it means

requested() reads payloads recorded by a command or form remote function while it runs. It is only valid inside those wrappers; the request state flag is_in_remote_form_or_command is initialized when entering them, and calls elsewhere are rejected.

Source

Thrown at packages/kit/src/runtime/app/server/remote/requested.js:142

	const ignored = (state.remote.ignored ??= new Set());

	/** @param {string} payload */
	const consume = (payload) => {
		payloads.delete(payload);
		if (payloads.size === 0) requested?.delete(__.id);
	};

	/** @param {string} payload */
	const create_ignore = (payload) => () => {
		ignored.add(create_remote_key(__.id, payload));
	};

	// note: don't initialize these maps here -- they will be initialized by the
	// command/form wrapper when we enter them, and if we initialize them here
	// we will enable requested(...) in contexts where it shouldn't be allowed,
	// such as load functions or other server functions
	if (!state.is_in_remote_form_or_command) {
		throw new Error(
			'requested(...) can only be called in the context of a command/form remote function'
		);
	}
	const [selected, skipped] = split_limit([...payloads], limit);

	/**
	 * Registers the failure exactly like `.set()` registers a value: the error record
	 * is serialized to the client (putting the query there into a failed state), and
	 * subsequent server-side calls of the query with the same argument reject with it.
	 * @param {string} payload
	 * @param {unknown} error
	 */
	const record_failure = (payload, error) => {
		const promise = Promise.reject(error);
		promise.catch(noop);

		get_cache(__, state)[payload] = promise;
		refresh(event, state, __, payload, () => promise);

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Move the requested(...) call inside the command or form remote function body
  2. If needed in load, have the command return the data and consume it there
  3. Restructure so query tracking happens during the command execution window

Example fix

// before
export const load = async () => {
  const pending = await requested(getPosts, 5); // invalid context
};
// after
export const createPost = command(async (input) => {
  const pending = await requested(getPosts, 5);
  /* ... */
});
Defensive patterns

Strategy: validation

Validate before calling

// ensure the call site is inside a command/form body
export const myCommand = command(async (input) => {
  // requested(...) is valid only here
});

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling requested(...) in a load function, a plain query, a module-level server file, or any server context that is not inside an executing command/form remote function.

Common situations: Hoisting requested() out of a command into load; calling it inside another query's validator; trying to inspect other queries in module scope at startup.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/3ee247daf2ffbb0b. Report an issue: GitHub.