Replies: 1 comment 2 replies
-
I think part of the issue is that some of your concerns can be mitigated by adjusting your route structure as well as judicious use of In your Tickets example, you have a nested route of Even if your layout had a list of tickets in the left nav, and adding a comment won't change the ticket title, etc. use of I'm not sure where you got the "never load more than once" idea from, but this function receives all the data you need to make an informed decision for every revalidation. You can return true/false for each scenario that is appropriate. import type { ShouldReloadFunction } from "@remix-run/react";
export const unstable_shouldReload: ShouldReloadFunction =
({
// same params that go to `loader` and `action`
params,
// a possible form submission that caused this to be reloaded
submission,
// the next URL being used to render this page
url,
// the previous URL used to render this page
prevUrl,
}) => false; // or `true` Anyway, for this proposal to gain any traction, I think you're going to have to create some real use cases. Show how current Remix is inadequate, cumbersome, inefficient, etc. And how this idea is novel and worth incorporating. The main concern is that you will introduce additional concepts beyond existing revalidation/shouldReload mechanics, and are those concepts worthwhile? That's my 2c. Feel free to ask questions if needed. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Add new Route Module export called
actionLoader
A new method called
actionLoader
which acts as a reducer, taking the last state of theloader
, the result of the lastaction
, and then computing the next state of theloader
. In this scenario, the data revalidation process is handed over to developer.TL;DR
Add support in Remix to be able to take the result of an action (POST, etc.) and use it to satisfy the result of a loader (GET).
The Remix
loader
/action
conundrumIn Remix, when an action runs successfully, one or more loaders will rerun (depending on the URL and layout) to keep UI state in sync with server state. The process is outlined here:
https://remix.run/docs/en/v1/guides/data-writes
The problem
Remix's data revalidation process requires at least one additional request (sometimes more) every time an action is taken.
Many times an updated resource can be sent back in the same request but given Remix's current process there is no way to take the data returned from an action and hand it over to the loader.
If a loader reaches out to multiple API endpoints but only a single representation changed, developers are left to implement a caching layer like Redis (which is fine, we need to do that anyway at some point) to make sure that we don't run up against rate limits and other potential bottlenecks related to extra API calls.
Example: mutations that happen from a collection of resources
Often, web apps will pull in collections of resources, for example:
Let's look at the "tickets" example. Consider the list view in a ticket system that contains quick actions from each item. Every time an action is taken on an item, the loader has to rerun to ensure that server state matches UI state. Imagine a scenario where ONLY the state of the individual ticket changed, it seems unnecessary to reach out for the entire list after each action when it can be returned in the successful POST request.
In other words, we don't want to
GET /tickets
every timePOST /tickets/123/comments
is successful.What about
unstable_shouldreload
?See: https://remix.run/docs/en/v1/api/conventions#unstable_shouldreload
This API handles the "never run more than once" scenario which is a solution to another problem entirely.
This proposal is not about never rerunning the loader but rather, about having a way to instruct Remix what to reload based on the result of an action.
Is there already a better way to handle this problem?
Part of me wonders if this problem can be solved another way by restructuring our routes, if you have ideas please share.
The Remix Way
The Remix revalidation process may be the best way to handle this problem, it ensures that UI state and server state align. It seems like the simplest way to guarantee things are correct. However, in my discussions with BE devs, there is still some concern around extra round trips to the server both from HTTP level and a server resource level. Both HTTP and server-side caching are solutions we are looking at but they don't entirely address the problem.
At the end of the day, I am fine with The Remix Way because it is pretty much equivalent to how the browser works without JS enabled (progressive enhancement FTW). But still.
Web Standards ❤️
In looking at the HTTP spec, given this proposal, I think Remix may be able to align a little more closely in scenarios when the "best" thing to do would be to respond with the new representation obtained from the action (i.e., POST requests returning
200 OK
...not to abuse this status code but it seems right in this case). Consider...As an aside, though it doesn't solve the problem this discussion is about, there are some instances when the "round trip" back to the server to validate can actually be satisfied from the cached result of a POST.
This is not common but seems like a reasonable means to forego the second trip out to the server. Methinks this is possible already in Remix, just return the appropriate cache-control headers in your response from an action. Subsequent GET requests should already be satisfied.
Benefits
Related discussions
I looked around a bit to see if anyone was talking about this, doesn't appear to be much discussion, though there is some. Not sure about Discord, I took a peek but search didn't turn up much.
Final note
I realize that some of the ideas in this proposal may not be plausible just due to blind spots and/or lack of knowledge of the HTTP spec and Remix as a framework. Feedback is very much welcome.
Beta Was this translation helpful? Give feedback.
All reactions