Skip to content
Advertisement

Svelte SPA Router – Invalid component object error

I’m using SvelteKit and svelte-spa-router.

My file structure looks like the following:

JavaScript

__layout.svelte:

(As per the example in the link above.)

JavaScript

Error:

JavaScript

Console:

Console Text

If I remove <Router {routes} /> and use <slot></slot>, everything works fine.

The only thing I managed to find about this error was this GitHub source code of Router.svelte (line #301), but it didn’t help much.

I tried changing the names of the components like in the example, but the error was still thrown (I figured maybe the fact that they’re all called the same might be a bug, I don’t know…).

Manually navigating via localhost:3000/[path] throws the error as well, including /test path which is outside in the same path as the __layout.svelte. The latter I mention because in the first link I provided the author said:

To display the router, in a Svelte component (usually App.svelte), first import the router component…

Usually from the examples I’ve seen, the structure you normally put in the App.svelte goes in the __layout.svelte and then the index.svelte, which serves as the “Home”/”Landing” page, automatically goes in the <slot /> (and whatever other routes you might have) located in __layout.svelte, by default.

Last but not least, dynamically importing them didn’t work either. See –> Edit 3

I understand a lot of the things I’ve tried probably have nothing to do with the problem, but the problem itself makes no sense to me. Like, why are the components getting passed as objects of a type the Router doesn’t understand? Test.svelte literally only has <h1>TEST COMPONENT</h1> in it.

Edit:

Added the following code to my __layout.svelte‘s <script></script> section:

JavaScript

After which I put the following below it, as shown in this test example:

JavaScript

None of these got called, there was only the red console message seen in the picture above.

Edit 2:

As suggested by Thomas Hennes, I replaced <Router {routes} /> with each component individually in my __layout.svelte file, like so:

JavaScript

None of them worked.

Edit 3:

Funny thing, I noticed dynamically importing the routes crashes my local server. xD

JavaScript

It doesn’t matter which one I import, if I only import one of them or all of them at the same time.

This is the console output of this adventure:

JavaScript

Advertisement

Answer

svelte-spa-router is designed as a routing solution for Svelte projects (which are client-side only). It is not meant to function in server-side rendered SvelteKit projects.

SvelteKit, which is a universal (CSR + SSR) framework, provides its own routing solution, which will work client-side and server-side.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement