Skip to content
tezvyn:

How does src/routes determine routing in SvelteKit?

Source: svelte.devEasyHow cards are made

How does src/routes determine routing in SvelteKit?
Summary

Filesystem routing and plus-prefix naming.

Key points

src/routes mirrors URLs, folders become paths, [slug] creates dynamic params, and +page.svelte renders pages.

Watch out for

Saying routes live in a central config file, not the filesystem.

What's really being asked

This question checks if you understand that SvelteKit uses a filesystem-based router where the directory structure inside src/routes directly determines the URL paths of the application. It also checks whether you know the plus-prefix convention for route files and can explain the difference between static segments and dynamic parameters.

The full answer

First, the root principle: src/routes corresponds to the root URL, and every subdirectory inside it becomes a route segment. Second, dynamic parameters: a folder named with brackets like [slug] creates a dynamic segment that matches any value in that URL position and exposes it as a parameter. Third, the plus prefix: only files starting with plus such as +page.svelte are treated as route files, which prevents helper or component files from accidentally becoming routes. Fourth, data loading: mention that +page.js or +page.server.js can export a load function to fetch and supply data to the page before rendering. Fifth, layout inheritance: note that +layout files apply to their own directory and all nested subdirectories unless explicitly reset.

The mistakes people make

Claiming that routes are declared in a central configuration file or a routes array like in React Router. Saying that any svelte file inside src/routes automatically becomes a page, which misses the plus prefix requirement. Confusing dynamic route segments with query parameters. Forgetting that +server files run only on the server and do not render pages. Asserting that you must manually register each route in svelte.config.js or vite.config.js.

What usually comes next

How would you create a restful API endpoint in SvelteKit? What is the difference between +page.js and +page.server.js? How do you handle catch-all or optional route parameters using double brackets or spread syntax? When would you use a layout reset? How does SvelteKit handle trailing slashes and why does it matter for SEO?

A concrete example

If your project has src/routes/+page.svelte for the home page, src/routes/about/+page.svelte for a static about page, and src/routes/blog/[slug]/+page.svelte for blog posts, then the generated URLs are slash, slash about, and slash blog slash hello dash world respectively. Visiting slash blog slash hello dash world would render the blog post page and make the slug parameter available to the load function in +page.js and to the page component through the data or params props.

Interview question

You place a helper component named utils.svelte inside src/routes/dashboard/. What is true about its routing behavior?

  • a.It does not create a route because SvelteKit requires the plus prefix for route filesCorrect
  • b.It is ignored unless it exports a load function from +page.js
  • c.It automatically creates a public page at /dashboard/utils
  • d.It only works if the folder is renamed to [utils] with brackets
Why?

SvelteKit only treats files starting with + as route files, so utils.svelte is ignored for routing and does not create a page. Option C reflects the common misconception that any .svelte file inside src/routes automatically becomes a public route.

Just read this? Test yourself on what you have been reading.

Read the original → svelte.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles