sveltejs / sveltejs/kit

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

Open
#13,647 3 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

load / layout types / typescript
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the getRequestEvent implementation and its type declarations, then trace how it is called from server load functions. Compare the available RequestEvent and ServerLoadEvent behavior, especially dependency tracking through event.url. Done means the chosen API and its TypeScript typing correctly expose the load event without breaking other callers.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
backend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.