kit icon indicating copy to clipboard operation
kit copied to clipboard

Determine if Page or Layout from within the load function

Open pjebs opened this issue 3 years ago • 3 comments

Describe the problem

There should be a way to determine from within a load function inside a +page.ts file or +layout.ts whether the load function is being called for a page or layout.

I am making a 3rd party library that is intended to be used only inside load functions. But I need to know cleanly (i.e. without user directly informing my library), whether it is being called for a layout or page.

Describe the proposed solution

I don't have a nice solution.

One solution is:

export async function load(x) {

x.layout = true/false <---- implying whether it is a layout or page. 

const { routeId, params } = x;
}

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

pjebs avatar Sep 19 '22 14:09 pjebs

Does anyone know some kind of hack or workaround?

pjebs avatar Sep 20 '22 07:09 pjebs

Could you provide more info/details what you library does, what the API is and how it's invoked?

dummdidumm avatar Jan 09 '23 20:01 dummdidumm

@dummdidumm

  1. Currently sveltekit only works with node.js backends.
  2. My library allows sveltekit (in static mode) to be used with any backend language.
  3. To facilitate this, the library makes a HTTP request to an arbitrary backend with details such as the route name, route params etc and the backend will return a JSON response, intended to be the return value of the load function.
  4. The issue is when a page has a page.ts and layout.ts. Both will call it's own load function.
  5. The arbitrary backend will need to differentiate the two separate requests since all other details (i.e. route name, route params etc) are identical.
  6. Currently my library is requesting manual intervention via the developer setting if the load is inside a page or layout.
  7. Hence the request for some kind of flag.

pjebs avatar Jan 09 '23 21:01 pjebs