Using Server Functions with progressive enhancement #6176
-
|
What's the correct way to use server functions with progressive enhancement? it's documented that it can be done with .url but i can't see to figure it out: I have a form that posts: <form method="post" action={createBookmark.url}>
<input type="hidden" name="year" value={year} />
<input type="hidden" name="type" value={type} />
<input type="hidden" name="slug" value={slug} />
<input type="hidden" name="status" value={nextStatus} />
<input type="hidden" name="returnTo" value={returnTo} />
<Button
variant="outline"
disabled={!canSubmit}
title={
canSubmit
? "Bookmark this item"
: "Sign in to bookmark events"
}
type="submit"
>
<Icons.star
className={status === "favourited" ? "icon--filled" : ""}
/>
</Button>
</form>And I have the serverFn setup: export const createBookmark = createServerFn({
method: "POST",
})
.inputValidator(
(data: {
year: number;
type: string;
slug: string;
status: string;
returnTo?: string;
}) => data,
)
.handler(async (ctx) => {
const { year, type, slug, status, returnTo } = ctx.data;
if (!type || !slug || !status) {
throw new Error("Invalid request");
}I always seem to get an error back, can't figure it out. Error is: i logged ctx, the data is empty so maybe I presumed the usage of a form wrong: {
"functionId": "eyJmaWxlIjoiL0BpZC9zcmMvc2VydmVyL2Z1bmN0aW9ucy9ib29rbWFya3MudHM_dHNyLWRpcmVjdGl2ZS11c2Utc2VydmVyPSIsImV4cG9ydCI6ImNyZWF0ZUJvb2ttYXJrX2NyZWF0ZVNlcnZlckZuX2hhbmRsZXIifQ",
"context": {},
"data": {},
"signal": {},
"request": {},
"headers": {},
"sendContext": {}
}Any help would be appreciated, hoping for a rubber duck moment where i suddenly figure it out after posting this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
you need to use |
Beta Was this translation helpful? Give feedback.
checkout https://github.com/TanStack/router/blob/main/e2e/react-start/server-functions/src/routes/submit-post-formdata.tsx
you need to use
FormData