sveltejs/kit

Shared shadow endpoint

livehtml opened this issue · 2 comments

Describe the problem

Is it possible to have one shared shadow endpoint for several .svelte pages?

For example, we have public pages: index.svelte, about.svelte, privacy-policy.svelte
And for all these pages need to retrieve identical information from server about the user (logged or not, unreaded messages to show counter, etc)

So, how can we use say index.js endpoint? This filename will work with index.svelte, but not with the others pages. Renaming to [index].js or [...index].js breaks at all.

Current workaround I found is using the 'old' way with load and fetch in all other than index.svelte pages:

export const load = async ({ fetch }) => {
    const res = await fetch('/', { headers: { accept: 'application/json'}});
    
    if (res.ok) {
        return {
            props: await res.json()
        }
    }

    const { message } = await res.json();

    return {
        status: res.status,
        error: new Error(message)
    };
}; 

But I'm not sure how good this solution is.

Describe the proposed solution

Ability to pair one endpoint with many pages. May be with regex like filename:
[index|about|privacy-policy].js

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

I do like this idea, similar to a layout but maybe an __endpoint.js file to load multiple routes with data. I would also love the ability to actually import an endpoint into routes instead of the pure magic load. This would allow the endpoints to be placed away from the routes also.

The solution here isn't to have a shared page endpoint, it's to have layout endpoints — closing this in favour of #4274