Different routes using different layouts, while doing file based routing please?
For example route /a uses layout ./src/lib/layouts/One.tsx and routes /b and /c use layout /Two.tsx
I'd rather not just wrap the layout component in the route b/c then if we swap between pages that use the same layout state is lost
Thanks!
12 Replies
https://docs.solidjs.com/solid-start/building-your-application/routing#route-groups
+
https://docs.solidjs.com/solid-start/building-your-application/routing#nested-layouts
Routing - SolidDocs
Documentation for SolidJS, the signals-powered UI framework
Brilliant! ty!
i didn't see it mentioned in the articles (maybe it's not recommended?). the approach i've used is:
<Router>
<Route path="/" component={Home}>
<Route path="/" component={LandingPage} />
<Route path="/login" component={Login} />
</Route>
</Router>
in my Home component I have the line below where I want the children to be:
<div>{props.children}</div>
You can create multiple group like this
That’s also absolutely fine.
You are using the component based approach.
Peer showed the file routing approach.
Advantage of file based routing is automatic code splitting for each route
src/routes/(one).tsx --- Layout for (one) routes
src/routes/(one)/a.tsx. --- UI for ./a (which uses the one layout)
src/routes/(two).tsx. --- Layout for (two) routes
src/routes/(two)/b.tsx --- UI for ./b (which uses the two layout)
src/routes/(two)/c.tsx --- UI for ./c (which uses the two layout)
Is this the expected behavior please? if yes I'll make a small example b/c in my more complex current example I'm not seeing this behavior but I wanna ensure I'm interpreting this correctly first please, thanks!
Yep
Thanks! So ./b should be a route in the following example please?
./routes/a.tsx - route showed up @ ./a
./routes/b.tsx - route showed up @ ./b
./routes/(c)/b.tsx - error @ ./b
TypeError: response.headers is not iterable
at sendWebResponse (file:/node_modules/h3/dist/index.mjs:929:39)
at eval (/Users/chriscarrington/gotcontract/node_modules/vinxi/runtime/http.js:144:12)
at eval (/Users/chriscarrington/gotcontract/node_modules/@solidjs/start/dist/middleware/index.jsx:10:54)
at async _callHandler (file:/node_modules/h3/dist/index.mjs:1831:7)
at async _callHandler (file:/node_modules/h3/dist/index.mjs:1837:16)
at async file:/node_modules/h3/dist/index.mjs:1978:19
at async Object.callAsync (file:/node_modules/unctx/dist/index.mjs:72:16)
at async Server.toNodeHandle (file:/node_modules/h3/dist/index.mjs:2270:7)
Yes, but now you have two matched for the same route ./b
Thanks I see my confusion, idk yet how to do layouts correctly with parens. My goal is (auth) folder w/ child files like [ ./routes/(auth)/sign-in.tsx ] and a layout file for the (auth) child files
I tried ./routes/(auth).tsx and ./routes/auth.tsx for the layout file and neither worked yet thanks!
& I just did something simple for the layout like:
import { type JSX } from 'solid-js'
export function Layout (
{ children, ...props }:
{children: JSX.Element } & JSX.IntrinsicElements['div']) {
return <>
<div id="abc" {...props}>
{children}
</div>
</>
}
idk yet how to do layouts correctly with parens.Parenthesis are just a means of using directory names to organize the routes in the context of the file system while excluding (a part of) the directory name from the URL. Most basically: can be expressed as In the first case
index
is understood to be /
by convention, while in the second case home
is suppressed to arrive at /
.
Route layout files have the same name as the folder that holds the nested routes.
so then
and neither workedIf applied in the above manner
src/routes/(auth).tsx
should have worked as the layout for src/routes/(auth)/index.tsx
.
However I don't think that src/routes/(auth)/sign-in.tsx
makes sense given that it needs to be available to as of yet unauthenticated users while I assume that src/routes/(auth).tsx
is a layout to all the authenticated portions of the app.
So this seems more likely
Routing - SolidDocs
Documentation for SolidJS, the signals-powered UI framework
You can still put your layout files in a place of you choosing:
Routes and route layouts receive
RouteSectionProps
by default where children
is optionally included for layouts.
Modules that are part of the routing have to have a default export. But you can also collocate modules that use named exports only and the file system router will ignore them when it is configuring the route.GitHub
solid-router/src/types.ts at 3c214ce2ceb9b7d9d39d143229a8c6145e83e6...
A universal router for Solid inspired by Ember and React Router - solidjs/solid-router
Thanks for the thorough explanation! Super helpful & got it workin!