Skip to content

Allow mounting routes from other places the src/routes/ #13705

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

Closed
ptrxyz opened this issue Apr 13, 2025 · 1 comment
Closed

Allow mounting routes from other places the src/routes/ #13705

ptrxyz opened this issue Apr 13, 2025 · 1 comment

Comments

@ptrxyz
Copy link

ptrxyz commented Apr 13, 2025

Describe the problem

Currently we are trying to build a larger app in SvelteKit and one of the pain points we stumbled on was that we couldn't figure out a way for a library to export a whole route.

In our scenario we need to divide the application in several pieces (we call them "modules" -- not to be confused with ESM modules) which consist of frontend pieces as well as quite some backend logic.

Imagine something like a multi-step dialog where you put in data on each step, then data is reloaded from the server and you proceed to the next step and finally data is commited. The developer would create the frontend dialogs but also his backend API routes.

Currenlty this is quite painful: there doesn't seem to be a simple way for the module to export it's whole sub-routes.

Describe the proposed solution

The workarounds we tried was to create a +server.ts with GET and POST handlers that are exported from the module -- this is bad for various resons, we loose data-preloading and invalidation mechanics and it's a painful to develop routes "in a vacuum" without type-safety and devalues serialization of higher-order data types (Dates, etc).

Another workaround was to export all the "load" functions separately and import them in various +page.server.svelte / +layout.server.svelte files. This was even more terrible, now the main app needs to know how the module wants it's subroutes to be organized and has to make sure the routing replicates the expected layout. This is very error prone, there is no way to validate that the routes the main app creates are the ones the module expects.

Alternatives considered

For now, the best workaround we have is to use another WinterCG compliant API handler -- again with all the problems mentioned above (no preloading + invalidation) and we have to add a separate software. However we get type-safe data fetching and the module can define it's own routes.

Importance

would make my life easier

Additional Information

I would love it if it was possible for SvelteKit to mount a complete sub-route at a specific endpoint, similar to the functionality other routes provide:

  • express, koa and oak have .use
  • Elysia has .mount
  • fastify has .register
  • hono has .route

So it would like to see the SvelteKit router to be able to do something similar -- a way to mount a "sub-app" at a defined entrypoint, preferably even at multiple different entrypoints and preferably as exports from other projects/NPM modules.

In my mind, this would be relevant for any library that provides frontend and backend functionality -- like plug-in mail clients or more complex auth systems (i.e. OIDC) and probably for any larger app that would be developed by multiple teams.

Are there any thoughts on this in the community?

@ptrxyz
Copy link
Author

ptrxyz commented Apr 13, 2025

I think this is similar: #8896

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants