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

docs: New blog about Progressive Forms #1195

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions packages/website/private/contents/post-007.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
slug: progressive-forms
title: Progressive forms
description: Forms work without javascript!
author: tyler
date: 2025/02/01
---

With Oracle clinging on to the trademark for [javascript](https://javascript.tm/), you'll be happy to know that your forms will work without it!

Jokes aside, this is a really cool framework feature that connects server actions directly to your forms.

## What does this look like?

So before React 19 and before we had waku server actions, a very basic form might look like this:
tylersayshi marked this conversation as resolved.
Show resolved Hide resolved

```jsx
const ClientForm = () => {
const handleSubmit = async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
await fetch('/api/submit', {
method: 'POST',
body: JSON.stringify(Object.fromEntries(formData)),
});
};

return (
<form onSubmit={handleSubmit}>
<input name="email" type="email" required />
<input name="password" type="password" required />
<button type="submit">Submit</button>
</form>
);
};
```

This is just fine, but if we want to run our form in an environment with no javascript. Or we just want to optimize the form to not need to render, we can upgrade to a new way.

## Introducing progressive forms with Waku Server actions

In this example, it can be pure RSC built. There is no client JS needed.
tylersayshi marked this conversation as resolved.
Show resolved Hide resolved

```jsx
async function submitForm(formData) {
'use server';
await fetch('https://api.example.com/submit', {
method: 'POST',
body: JSON.stringify(Object.fromEntries(formData)),
});
}

const ServerForm = () => {
return (
<form action={submitForm}>
<input name="email" type="email" required />
<input name="password" type="password" required />
<button type="submit">Submit</button>
</form>
);
};
```

We can sprinkle in some js where it can be helpful. Like for client-side validation, or for showing errors from the server. The key point though, is that the form will always submit and pair with your server action regardless of whether JS is running or not.
17 changes: 16 additions & 1 deletion packages/website/src/lib/get-author.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const getAuthor = (author: string) => {
type Author = {
name: string;
biography: string;
avatar: string;
url: string;
};

export const getAuthor = (author: string): Author => {
switch (author) {
case 'daishi':
return {
Expand All @@ -14,11 +21,19 @@ export const getAuthor = (author: string) => {
avatar: `https://cdn.candycode.com/waku/sophia.png`,
url: `https://x.com/razorbelle`,
};
case 'tyler':
return {
name: 'Tyler Lawson',
biography: 'senior engineer at second spectrum',
avatar: 'https://avatars.githubusercontent.com/u/26290074',
url: 'https://tylur.dev',
};
default:
return {
name: ``,
biography: ``,
avatar: ``,
url: '',
};
}
};
Loading