question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Isomorphic form handling?

See original GitHub issue

Is your feature request related to a problem? Please describe. When you need to use a form like <form action="/items" method="POST">, currently you have two choices:

  1. leave it as it is, and when it’s submitted, the page refreshes, and then redirects to a new page.
  2. hijack the submission, and use fetch() to submit to a different api that doesn’t redirect, and use goto() to redirect the page

The API for these two destinations might be wildly different, one accepts form data, and the other probably json, and if you don’t do #2 to save the trouble, the SPA flow is going to be interrupted whenever you need to use a form. If you don’t do #1, then javascript is required to use the site.

It’d be nice if svelte kit can handle the two case as transparently as it does for <a>

Describe the solution you’d like I don’t have a solution in mind yet, since redirection seems to be transparent to fetch(). I’m currently just asking if this is a problem worth solving.

People are actually trying to implement this themselves as can be seen from this issue

Describe alternatives you’ve considered

How important is this feature to you? Somewhat, a page refresh is not the end of the world.

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Rich-Harriscommented, Apr 24, 2021

Yep. If you look at the demo app’s todos page, we’re using an enhance action that progressively enhances a form if JS is available: https://github.com/sveltejs/kit/blob/aa3610bc3608814f775d89dcff6ed5995343ce5b/packages/create-svelte/templates/default/src/routes/todos/index.svelte#L55-L62

The enhance action uses an accepts header to signal that it’s coming from fetch (there may be a more semantically appropriate way to do this, but e.g. sec-fetch-dest isn’t supported widely enough yet, so this’ll do for now): https://github.com/sveltejs/kit/blob/master/packages/create-svelte/templates/default/src/lib/form.ts#L27-L33

Our endpoints have to have some special logic that returns a redirect if it sees that header: https://github.com/sveltejs/kit/blob/master/packages/create-svelte/templates/default/src/routes/todos/_api.ts#L30-L42

This works — try https://svelte-kit-demo.richard-a-harris.workers.dev/todos with and without JS — but it’s definitely not as turnkey as it should be. We need to figure out where enhance should live (whether it’s part of Kit, or a standalone package or whatever) and how to make the redirect logic more seamless, since it’s not very portable.

0reactions
DanielCollinscommented, Jan 15, 2022

Yeah, that was the only real option I could think of. But on a large form that’s potentially a lot of state to (manually…) serialize and deserialize through a bug ugly url.

It’s particularly unfortunate because even in something as mindless as PHP this (i.e., rendering a page after a POST) would be completely trivial

Read more comments on GitHub >

github_iconTop Results From Across the Web

isomorphic-form - npm
Start using isomorphic-form in your project by running `npm i isomorphic-form` ... url) you can pass it to your universal router or other...
Read more >
Isomorphism - Wikipedia
In mathematics, an isomorphism is a structure-preserving mapping between two structures of the same type that can be reversed by an inverse mapping....
Read more >
Introducing Passable. Fiverr's open source isomorphic… |
We decided that in our first implementation, our solution would only handle product validations— since they are safe to perform on the browser ......
Read more >
isomorphic-form - npm Package Health Analysis - Snyk
We're still processing downloads data for the npm package isomorphic-form. Visit the popularity section on Snyk Advisor to see the full ...
Read more >
React Tips — Form Validation, Links, and Isomorphic ...
We can do form validation with our own checks. For instance, we can do the value checks within the submit handler. ... We...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found