nexty5
nexty5

Reputation: 55

Creating routes dynamically in Svelte

I'm creating a Svelte SPA as a wrapper for an API. I get all data (content, menu items) from this API and I would like to create these routes dynamically. I have only one page (Mypage.svelte) and its content is updated depending on the menu link I click. Currently I am using the svelte-spa-router where I define the menu URLs manually:

export const routes = {
  "/group0": Mypage,
  "/group1": Mypage,
  "/group2": Mypage,
  "/group3": Mypage,
  "/group4": Mypage,
  "*": NotFound
};

Is it possible in Svelte or SvelteKit to set these dummy routes dynamically for example by preloading them from my API?

Upvotes: 0

Views: 369

Answers (1)

leonard georg
leonard georg

Reputation: 33

As far as I am aware it is not possible to generate routes dynamically the way you describe

Although there might be an alternative at hand.

SvelteKit

In SvelteKit the routes are generated by the file System. Docs You can define a Layout for your app and for every Sub Folder Docs and pass arguments as part of the URL Docs

This way you could determine a Layout and pass a parameter via URL and fetch your data accordingly.

Depending on how variant your data is, this might be an option.

Upvotes: 1

Related Questions