feat: form actions for progressive enhancement #20852
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π Linked issue
#20649
β Type of change
π Description
Here's a standalone module with the most up-to-date version : https://github.com/Hebilicious/form-actions-nuxt
This PR is a POC that attempts to implements #20649. Almost everything is implemented as a Nuxt Module.
It adds the following helper features to Nuxt or Nitro :
defineServerLoader
defineFormActions
actionResponse
It adds the following helpers to H3 :
getRequestFromEvent
respondWithResponse
respondWithRedirect
getFormData
All of these should only be available on the server side (ie
/server
directory)It could be argued that some of these helpers might be overkill and should be implemented by users, but for the sake of nice DX I added them anyways
It adds the
useFormAction
composable to Nuxt.And the
v-enhance
directive.Finally it adds the possibility to Nitro to specify fallback routes (
nitro.config.routesWithFallback
) to an array of route objects through the configuration ({ route: "api/todos", fallbackTo: '/**' }
). The fallback route will be matched with a GET method.Effectively This allows creating POST endpoints on the same route than pages.
There's a classic todo example with optimistic updates for the delete method.
π Checklist