Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per-route hook or middleware #5763

Closed
CaptainCodeman opened this issue Jul 30, 2022 · 3 comments · Fixed by #6174
Closed

Per-route hook or middleware #5763

CaptainCodeman opened this issue Jul 30, 2022 · 3 comments · Fixed by #6174
Labels
feature / enhancement New feature or request
Milestone

Comments

@CaptainCodeman
Copy link
Contributor

CaptainCodeman commented Jul 30, 2022

Describe the problem

Occasionally, I have a need to load some data in an endpoint and then re-use some of that data in a child endpoint. There's currently no way to pass the parent data down to the child on the server-side other than indirectly via the rendering system, and in some cases that data shouldn't go to the client at all and / or would add unnecessary bloat to the page payload.

To paint a more concrete picture of a scenario, imagine a forum system ...

/routes/topic/[id]/__layout.svelte Renders the topic title, author, summary, date, reply count etc...
/routes/topic/[id]/index.ts Is the endpoint for the topic data
/routes/topic/[id]/[slug]/index.svelte Renders the paginated posts in the topic
/routes/topic/[id]/[slug]/index.tx Is the endpoint for the post data, it uses the page number from the url + a slice of the post references on the topic to fetch

How to re-use the data from the parent endpoint, in the child endpoint, without passing it through the page (which doesn't need it).

Describe the proposed solution

What I'd like to have is the ability to hook into the request at any level, to pass data down. Exactly like the current hooks, which only runs at the root app level. Or maybe it could work like the new load system is going to, where a child can await a parent ... but on the server side for endpoints (?).

Alternatives considered

Right now I get around it by using a sequenced handler in the app-level hooks, to check for a particular routeId prefix, do the data fetch there, put it into locals and then have access to it in both places.

But it feels odd that I can't use the routing system to do something so routing-related, and I didn't see anything in the new routing proposal that would address it.

Importance

nice to have

Additional Information

It's not possible to add anything to locals from the parent endpoint AFAICT (I guess 'cause it's a child request at that point?)

It's a fairly normal NoSQL approach to embed references to a list of things in a parent as it saves on expensive queries (esp. with offsets when using paginated data).

Avoiding unnecessary reads of the parent data is also beneficial, it would only be 1 but it's additional cost when charged per-read and adds to overall request latency.

This kind of thing has been trivial to do when writing server APIs in Go, but hasn't translated well to a SvelteKit powered API.

Running and accessing a caching server on Google Cloud from Cloud Run is surprisingly complicated and expensive. Per-instance caching would be problematic on an auto-scaling platform (for cache invalidation) so reading per-request is the best compromise.

Related to: #3881

@coryvirok
Copy link
Contributor

I think the new layout Middleware (+layout.server.js ?) solves this by letting you call await parent() and capturing the result.

Aside: Combining "layout" and "server" makes me cringe as a predominantly backend dev. I think Kit is what we get when front-end devs redesign backend systems and nomenclature. No judgement here, just highlighting the peculiarities.

Ps. I enjoy your blog

@CaptainCodeman
Copy link
Contributor Author

I think the new layout Middleware (+layout.server.js ?) solves this by letting you call await parent() and capturing the result.

Hopefully, but I understood that to be part of the load mechanism for pages, rather than the server endpoints which is where I need it (e.g. on a fetch of the child endpoint only, after page navigation). I'll try things out when the new routing lands and see what works and if this still applies.

Aside: Combining "layout" and "server" makes me cringe as a predominantly backend dev. I think Kit is what we get when front-end devs redesign backend systems and nomenclature. No judgement here, just highlighting the peculiarities.

I don't think it's too bad - the server is server only which makes things clearer and layout is a templating thing, which can run on both server and client.

Ps. I enjoy your blog

Thanks! So many things to write, so little time ...

BTW: It's SvelteKit powered :)

@dummdidumm
Copy link
Member

With the redesign, the parent endpoint would become a layout endpoint, whose data is automerged with the page data. #6056 will make rerunning load smarter so it doesn't rerun unnecessarily. I think this covers all use cases of this request, if I'm not mistaken.

dummdidumm added a commit that referenced this issue Aug 24, 2022
#6124

Closes #6196 (using (groups) and/or composition)
Closes #5763 (root layout guaranteed to always exist + await parent())
Closes #5311 (+page@.svelte)
Closes #4940 (no longer possible to get into this situation)
Closes #2154 (only a single root layout now)

Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>
Co-authored-by: Dominik G. <dominik.goepel@gmx.de>
Co-authored-by: Ignatius Bagus <ignatius.mbs@gmail.com>
Co-authored-by: Conduitry <git@chor.date>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants