Skip to content

allow config.kit.files.routes to be an array of directories #6031

@sidvishnoi

Description

@sidvishnoi

Describe the problem

We have a multi-tenant SvelteKit project, with a directory structure like following:

src/routes/
 - index.svelte
 - about.svelte
 - terms.svelte
 - [...project1]/
    - index.svelte
 - [...project2]/
    - about.svelte

Here, we've many common routes (index, about, terms). Each project can override the common routes with a few of their custom routes (like project1 has its own index.svelte but uses the common about.svelte; and project2 has a different about.svelte but uses the common index and terms page.

Before we had the new routing system (#5774), we could filter out specific files from routes directory to be considered for routing using the config.kit.routes function. This was done based on build-time env variables like following:

   routes(filepath) {
      const currentProject = process.env.PROJECT_ID;
      // Exclude special folder that doesn't match current project.
      const re = /^\/\[\.\.\.(project1|project2)\]/;
      if (re.test(filepath) && filepath.match(re)[1] !== currentProject) {
        return false;
      }

      // If we've a matching route in currentProject-specific folder, exclude the common
      // filepath route. We'll end up using currentProject-specific one as `params[currentProject]` can
      // be an empty string, which SvelteKit router matches.
      // This does mean `/anything/foo` as well as `/foo resolve to same page, 
      // but we can exclude `/{anything}/foo` using route params. Hacky, but works.
      const specialRoute = path.join(`src/routes/[...${currentProject}]`, filepath);
      return !existsAsFile(specialRoute);
   },

Now, the config.kit.routes option has been removed and we can't upgrade to newer versions without major changes to our approach.

Describe the proposed solution

  • Bring back config.kit.routes option.
  • Support multiple directory in config.kit.files.routes, which can support overrides like above. This is arguably a niche use-case, but would love if this can be supported.

Alternatives considered

We can create multiple top-level routes directories and symlink/hardlink the common routes to project-specific directory, while avoiding the existing non-symlink project-specific files, to have a directory structure like:

src/
  - routes.common/
    - index.svelte
    - about.svelte
    - terms.svelte
  - routes.project1/
    - index.svelte
    - about.svelte <symlink>
    - terms.svelte <symlink>
  - routes.project2/
    - index.svelte <symlink>
    - about.svelte
    - terms.svelte <symlink>

But this might cause confusion, where we update one file and it unexpectedly ends up updating other file too. Also, it will require us to delete the symlink and create a regular file when we want to override a route.

Importance

would make my life easier

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions