Skip to content

[Feature?]: Add an option to disable lazy loading in <FileRoutes /> #1575

Closed as not planned

Description

@huseeiin

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

currently, FileRoutes lazy-loades all routes by default which is sometimes undesired.

Adding a sync option (that is false by default) or a async option to enable lazy loading (that is false by default) to FileRoutes to disable lazy loading all routes will fix that.

even better, this could be decided for each route separately.

Examples 🌈

a workaround is using custom <Route />s instead of using <FileRoutes />:

import { MetaProvider, Title } from "@solidjs/meta";
import { Route, Router } from "@solidjs/router";
import { Suspense } from "solid-js";
import Home from "./routes";
import About from "./routes/about";
import { get } from "./lib/api";
import NotFound from "./routes/[...404]";

export default function App() {
  return (
    <Router
      root={(props) => (
        <MetaProvider>
          <Title>SolidStart - Basic</Title>
          <a href="/">Index</a>
          <a href="/about">About</a>
          <Suspense>{props.children}</Suspense>
        </MetaProvider>
      )}
    >
      <Route path="/" component={Home} />
      <Route load={() => get()} path="/about" component={About} />
      <Route path="*404" component={NotFound} />
    </Router>
  );
}

Motivation 🔦

No response

Activity

  1. changed the title [-][Feature?]: Add a `sync` option to `FileRoutes` or a `async` option that is `false` by default to disable lazy loading all routes[/-] [+][Feature?]: Add an option to disable lazy loading in `<FileRoutes />`[/+] on Jul 3, 2024
  2. ryansolid commented on Jul 3, 2024

    @ryansolid
    Member

    I do have some concerns about giving control here when it comes to future routing approaches including lazy route manifest shipping. It might be valuable to see what this feature looks like in other frameworks. I'm curious how Next, Sveltekit, Remix, Nuxt, etc handle this.

  3. doeixd commented on Jul 3, 2024

    @doeixd
    Contributor

    Would this work?

    async function createEagerFileRoutes() {
      const routes = <FileRoutes />;
      const eagerRoutes = []
    
      for (let route of (routes || [])) {
        if (route.info?.eager) {
          const component = await route['$component'].import();
          eagerRoutes.push(<Route path={route.path} component={component} load={route.load} />)
        } else {
          eagerRoutes.push(route)
        }
      }
    
      return (props) => eagerRoutes
    }
    
    const EagerFileRoutes = await createEagerFileRoutes();
  4. lxsmnsyc commented on Oct 14, 2026

    @lxsmnsyc
    Member

    Closing this as not planned. A flag that turns off code splitting for <FileRoutes /> would change how routes are generated, which is what Ryan was concerned would conflict with future lazy route manifests. The underlying need, a route's code being ready before you navigate to it, is already covered on both 1.x and 2.x:

    • Links already preload: Solid Router loads a route's code when a link to it is hovered, focused or touched.

    • Programmatic navigation: usePreloadRoute() from @solidjs/router preloads a route's code (and data) ahead of a navigate() call.

    • All routes up front, while keeping them as separate chunks: on the client, FileRoutes() returns the route objects, and each route's lazy component has a preload():

      import { FileRoutes } from "@solidjs/start/router";
      import { onMount } from "solid-js";
      
      function preloadAll(routes: any[] | undefined) {
        for (const route of routes ?? []) {
          route.component?.preload?.();
          preloadAll(route.children);
        }
      }
      
      // inside the component that renders <Router>:
      onMount(() => preloadAll(FileRoutes() as any));

      I checked this on a production build of the basic fixture on both 1.x and 2.x. Without it, visiting / loads only the index route's chunk. With it, the other route chunks load right after the page does.

    • Truly eager routes, bundled with the entry: define the routes with <Route> and static imports, as in the workaround in the report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions