kit icon indicating copy to clipboard operation
kit copied to clipboard

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

Open GauBen opened this issue 2 years ago • 0 comments

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

GauBen avatar Jan 10 '24 16:01 GauBen