sveltejs / sveltejs/kit

Offer a way to avoid the waterfall because of nested layouts, `await parent()` to retrieve `locals` / Bring back `session`

Open
#11,587 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the problem

Let's say I have a few layouts that fetch data using session credentials.

// hooks.server.ts
export const handle = ({event, resolve}) => {
  if (hasACertainCookie) {
    event.locals.session = getSessionFromCookie()
  }

  return resolve(event)
}

// Root +layout.server.ts
export const load = ({locals}) => locals; // Expose all locals to layouts and pages

// Many +layout.ts and +page.ts look like this
export const load = ({parent}) => {
  const {session} = await parent();
  return { thing: fetch(..., session) }
}

This will unfortunately create a waterfall, despite the only needed data are the one from local.session

Describe the proposed solution

Svelte-Kit < 1 had a session feature which allowed constant-time access to a bag of top props, and it can be used as inspiration

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

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 with the issue's examples in hooks.server.ts, +layout.server.ts, and the nested +layout.ts/+page.ts load functions, focusing on how await parent() exposes locals and creates a waterfall. Define a way to access session data without waiting through nested layouts, and verify that the resulting loads avoid the described waterfall while preserving access to session.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.