#Throwing redirect in loader

1 messages · Page 1 of 1 (latest)

nimble lance
#

Hi there, quick question if there is a better (faster) practice to the effect that I'm trying to achieve.

Basically the user navigates to a page where he can book appoinmentments. In the loader, I validate the query-strings "date" and "duration" against some data in my DB and an external api - this query can take up to 200ms when the api is cold.

If the query is validated, I go on and show my own data defferred - great UX here. But when the query is invalid (for example, you cant book on holidays / weekends ) - i throw a redirect to the next possible date - for example monday. Then that validation loader runs again, leading to a possible 500ms delay.


export const loader = async({request}) => {
const {isValid, data}  = await validateQuery(request)
if(!isValid){
throw redirect(data.url)}
  }
return defer(...myData)
}

Since I have to await those queries in the loader (deferred data can't throw redirects if I'm correct) - it sometimes leads to some bad UX. Is there any better practice? Validating in JSON and then using the client to update the query params?

Thanks!

solid pawn
#

This isn't Remix specific, but the DB can be slow at times So in that case look to using a cache in some way. Could be local on the server or external like Redis or Memcached or AWS based caches, etc. First hit will take the brunt and have to call DB, but after it will be very fast.

nimble lance
#

I know - i cache as much as possible in a redis cache. Still, some users have to take the hit...and i cannot show any loader etc. whilst im validating...