'Creating SvelteKit library with reusable routes
I'd like to create a SvelteKit library that provides a list of routes that the library user can then add to their Svelte app tree. This would be similar to some web frameworks, like reusable apps in Django.
- Is creating reusable routes in the library currently possible with SvelteKit?
- If it is possible are there any libraries that could be good examples to look upon?
- If this is not possible then what's the next best alternative to provide easy route integrations to library users?
Solution 1:[1]
Is creating reusable routes in the library currently possible with SvelteKit? If you want something like django then no, it's not possible.
Because of the file system based nature of SvelteKit's routing you can't define "routes" from outside of config.kit.files.routes.
However, you can define the response of those routes from anywhere. For endpoints, this is as simple as reexporting the RequestHandler functions with the proper name from your package.
For pages it's a bit more involved, for the script part it's again as easy as just reexporting. For the markup, it'd mean doing something like:
<script context="module">
export * from "your-lib/your-page.svelte";
import YourPage from "your-lib/your-page.svelte";
</script>
<YourPage />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nayan Gautam |
