QwikDev / qwik

Instant-loading web apps, without effort
https://qwik.dev
MIT License
20.69k stars 1.29k forks source link

[✨] Detect SPA navigation vs form submission on routeAction$ #5482

Open eduardvercaemer opened 10 months ago

eduardvercaemer commented 10 months ago

Is your feature request related to a problem?

I have muñtiple routeAction$ that I want to be usable both with or without javascript. The "business" logic works all right, but when I use a no-javascript browser and submit a form with an action, it sends me to a location like /current/page/?qaction=g3eClvApRiI which then messes up the UX since reloads will warn about form resubmission.

Describe the solution you'd like

I believe there should be a way of automatically redirecting the user to the base location after the action completes when there is no SPA navigation detected. This could be accomplished by qwik adding a custom header on requests done via fetch that won't be there in regular browser submissions and then use this header on the server to decide what to do after the action completes (such as a redirect).

Describe alternatives you've considered

A "userland" solution would be to add a hidden input to all forms and then use a visible task to change their value then use the submitted value in the action to decide what to do.

Additional context

No response

eduardvercaemer commented 10 months ago

A similar thing happens with the Auth integration actions. Submitting a form with the signout action for example just results in an update to the URL but update happens until a manual refresh.

wmertens commented 10 months ago

I believe no header is needed, the accepts header already differs between native form submit (html) and qwik (JSON), no?

So all that's needed is to redirect to the url without the qaction when serving html, correct? Although then the state is lost after redirect? 🤔

Sorry on my phone might not be making sense.

eduardvercaemer commented 10 months ago

So I tried looking at the differences in the request and it looked like this for the noscript environment:

POST /app/profile/?qaction=coVn6k6fzTE HTTP/2
Host: <host>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded
Content-Length: 26
Origin: <origin>
Connection: keep-alive
Referer: <referer>
Cookie: <auth cookies>
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: same-origin
Sec-Fetch-User: ?1
TE: trailers

Then I tried doing the same with javascript enabled the post request looked like it POSTed to q-data.json instead of the base path BUT the dev tools showed no headers or any payload response at all?

image

I'll try some more testing when I get a chance later today. I think it should work to redirect based on the q-data.json path.