kit icon indicating copy to clipboard operation
kit copied to clipboard

Return `ServerLoadEvent` from `getRequestEvent` when called from load function

Open ottomated opened this issue 8 months ago • 2 comments

Describe the problem

getRequestEvent is great! It simplified my trpc package greatly by allowing library functions to access event.locals without passing it in all the time. However, because it returns the raw RequestEvent rather than the augmented ServerLoadEvent that includes dependency tracking on event.url, I was unable to fully replace the pattern of passing event into library functions.

This was a nightmare to track down because RequestEvent is so similar to ServerLoadEvent that it was hard to tell the difference.

Describe the proposed solution

Either:

  • When called from within a server load function, getRequestEvent should return the augmented ServerLoadEvent (would cause typescript issues, however)
  • or, a 2nd function called getLoadEvent that only works within server load functions (confusing to users)

Typescript could be something like

function getRequestEvent(): RequestEvent | ServerLoadEvent;

which forces the user to check something like 'depends' in event, but is strictly correct

Importance

would make my life easier

ottomated avatar Mar 27 '25 01:03 ottomated

This would be incredibly useful for things like clients that can be used in universal load functions. I am trying to use a TRPC client in the universal load function, in which case it needs to do some cookie magic if executed on the server. But it is very hard to do this with passed events. And if I forget to pass the event to the client in any load function everything breaks. Please implement this!!!

p-mercury avatar May 09 '25 13:05 p-mercury

I also used getRequestEvent to avoid needing to pass the event down a long chain of function calls.

My app has a page with paginated data whose page is controlled by some query parameters. When using getRequestEvent the dependency tracking does not work correctly and thus clicks on links to switch the page are not (pre)loading.

fehnomenal avatar May 20 '25 22:05 fehnomenal

Just happened to have the same issue. Any updates ? 👀

novaotp avatar Oct 06 '25 14:10 novaotp