Skip to content

Commit

Permalink
feat!: upgrade to solid-router v0.10.1
Browse files Browse the repository at this point in the history
  • Loading branch information
oedotme committed Dec 12, 2023
1 parent 517febf commit d466ef2
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 77 deletions.
2 changes: 1 addition & 1 deletion examples/solid-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@generouted/solid-router": "^1.16.1",
"@solidjs/router": "^0.9.1",
"@solidjs/router": "^0.10.1",
"solid-js": "^1.8.7"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/solid-router/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from 'solid-js/web'
import { Routes } from '@generouted/solid-router'
import { Routes } from '@generouted/solid-router/lazy'

render(Routes, document.getElementById('app') as HTMLElement)
6 changes: 3 additions & 3 deletions examples/solid-router/src/pages/(auth)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Outlet } from '@solidjs/router'
import { ParentProps } from 'solid-js'

export default function Layout() {
export default function Layout(props: ParentProps) {
return (
<div>
<h1>Auth Layout</h1>
<Outlet />
{props.children}
</div>
)
}
10 changes: 4 additions & 6 deletions examples/solid-router/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Outlet } from '@solidjs/router'
import { Modals } from '@generouted/solid-router'
import { ParentProps } from 'solid-js'
import { Modals } from '@generouted/solid-router/lazy'

import { A, useModals, useNavigate } from '@/router'

Expand All @@ -15,7 +15,7 @@ export const Catch = (props: { error: Error; reset: () => void }) => {

export const Pending = () => <div>Loading from _app...</div>

export default function App() {
export default function App(props: ParentProps) {
const navigate = useNavigate()
const modals = useModals()

Expand All @@ -33,9 +33,7 @@ export default function App() {
<button onClick={() => modals.open('/modal', { at: '/posts' })}>Open global modal</button>
</header>

<main>
<Outlet />
</main>
<main>{props.children}</main>

<Modals />
</section>
Expand Down
8 changes: 5 additions & 3 deletions examples/solid-router/src/pages/posts/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Outlet } from '@solidjs/router'
import { ParentProps } from 'solid-js'

export default function About() {
export const Catch = () => 'Error'

export default function Posts(props: ParentProps) {
return (
<div>
<h1>Posts Layout</h1>
<Outlet />
{props.children}
</div>
)
}
2 changes: 1 addition & 1 deletion packages/generouted/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"type-check": "tsc --noEmit"
},
"devDependencies": {
"@solidjs/router": "^0.9.1",
"@solidjs/router": "^0.10.1",
"@tanstack/react-location": "^3.7.4",
"@types/react": "^18.2.42",
"@types/react-dom": "^18.2.17",
Expand Down
25 changes: 13 additions & 12 deletions packages/generouted/src/solid-router-lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/** @jsxImportSource solid-js */
import { Component, createMemo, ErrorBoundary, lazy, ParentProps, Suspense } from 'solid-js'
import { Outlet, Router, useLocation, useRoutes } from '@solidjs/router'
import type { RouteDataFunc, RouteDataFuncArgs, RouteDefinition } from '@solidjs/router'
import { RouteDefinition, RouteLoadFunc, RouteLoadFuncArgs, Router, useLocation } from '@solidjs/router'

import { generateModalRoutes, generatePreservedRoutes, generateRegularRoutes } from './core'

type CatchProps = { error: any; reset: () => void }
type Module = { default: Component; Loader?: RouteDataFunc; Catch?: Component<CatchProps>; Pending?: Component }
type Module = { default: Component; Loader?: RouteLoadFunc; Catch?: Component<CatchProps>; Pending?: Component }
type Route = { path?: string; component?: Component; children?: Route[] }

const PRESERVED = import.meta.glob<Module>('/src/pages/(_app|404).{jsx,tsx}', { eager: true })
Expand All @@ -20,12 +19,14 @@ const regularRoutes = generateRegularRoutes<Route, () => Promise<Module>>(ROUTES
const Element = lazy(module)
const Pending = lazy(() => module().then((module) => ({ default: module?.Pending || Fragment })))
const Catch = lazy(() => module().then((module) => ({ default: module?.Catch || Fragment })))
const Page = () => <Suspense fallback={<Pending />} children={<Element />} />
const Component = () => <ErrorBoundary fallback={(error, reset) => Catch({ error, reset })} children={<Page />} />
const Page = (props: any) => <Suspense fallback={<Pending />} children={<Element {...props} />} />
const Component = (props: any) => (
<ErrorBoundary fallback={(error, reset) => Catch({ error, reset })} children={<Page {...props} />} />
)

return {
component: Component,
data: (args: RouteDataFuncArgs) => module().then((mod) => mod?.Loader?.(args) || null),
load: (args: RouteLoadFuncArgs) => module().then((mod) => mod?.Loader?.(args) || undefined),
}
})
const _app = preservedRoutes?.['_app']
Expand All @@ -36,15 +37,15 @@ const Element = _app?.default || Fragment
const Pending = _app?.Pending || Fragment
const Catch = preservedRoutes?.['_app']?.Catch

const App = () => (
const App = (props: any) => (
<ErrorBoundary fallback={(error, reset) => Catch?.({ error, reset })}>
{_app?.Pending ? <Suspense fallback={<Pending />} children={<Element />} /> : <Element />}
{_app?.Pending ? <Suspense fallback={<Pending />} children={<Element {...props} />} /> : <Element {...props} />}
</ErrorBoundary>
)

const app = { path: '', component: _app?.default ? App : Outlet, data: _app?.Loader || null }
const fallback = { path: '*', component: _404?.default || Fragment }
const app: RouteDefinition = { path: '', component: _app?.default ? App : Fragment, load: _app?.Loader || undefined }
const fallback: RouteDefinition = { path: '*', component: _404?.default || Fragment }

export const routes = [{ ...app, children: [...regularRoutes, fallback] }] as RouteDefinition[]
export const Routes = () => <Router children={useRoutes(routes)()} />
export const routes = [{ ...app, children: [...regularRoutes, fallback] }] as RouteDefinition[] // @ts-expect-error
export const Routes = () => <Router children={routes} />
export const Modals = () => createMemo(() => modalRoutes[useLocation<any>().state?.modal || ''] || Fragment) as any
34 changes: 19 additions & 15 deletions packages/generouted/src/solid-router.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/** @jsxImportSource solid-js */
import { Component, createMemo, ErrorBoundary, ParentProps, Suspense } from 'solid-js'
import { Outlet, RouteDataFunc, RouteDefinition, Router, useLocation, useRoutes } from '@solidjs/router'
import { RouteDefinition, RouteLoadFunc, Router, useLocation } from '@solidjs/router'

import { generateModalRoutes, generatePreservedRoutes, generateRegularRoutes } from './core'

type CatchProps = { error: any; reset: () => void }
type Module = { default: Component; Loader?: RouteDataFunc; Catch?: Component<CatchProps>; Pending?: Component }
type Module = { default: Component; Loader?: RouteLoadFunc; Catch?: Component<CatchProps>; Pending?: Component }
type RouteDef = { path?: string; component?: Component; children?: RouteDef[] }

const PRESERVED = import.meta.glob<Module>('/src/pages/(_app|404).{jsx,tsx}', { eager: true })
Expand All @@ -15,13 +15,17 @@ const ROUTES = import.meta.glob<Module>(['/src/pages/**/[\\w[-]*.{jsx,tsx}', '!*
const preservedRoutes = generatePreservedRoutes<Module>(PRESERVED)
const modalRoutes = generateModalRoutes<Element>(MODALS)

const regularRoutes = generateRegularRoutes<RouteDef, Module>(ROUTES, (module) => {
const Element = module?.default || Fragment
const Page = () => (module?.Pending ? <Suspense fallback={<module.Pending />} children={<Element />} /> : <Element />)
const Component = module?.Catch
? () => <ErrorBoundary fallback={(error, reset) => module.Catch?.({ error, reset })} children={<Page />} />
: Page
return { component: Component, data: module?.Loader || null }
const regularRoutes = generateRegularRoutes<RouteDef, Module>(ROUTES, (mod) => {
const Element = mod?.default || Fragment
const Page = (props: any) =>
mod?.Pending ? <Suspense fallback={<mod.Pending />} children={<Element {...props} />} /> : <Element {...props} />
const Component = (props: any) =>
mod?.Catch ? (
<ErrorBoundary fallback={(error, reset) => mod.Catch?.({ error, reset })} children={<Page {...props} />} />
) : (
<Page {...props} />
)
return { component: Component, load: mod?.Loader || undefined }
})

const _app = preservedRoutes?.['_app']
Expand All @@ -32,15 +36,15 @@ const Element = _app?.default || Fragment
const Pending = _app?.Pending || Fragment
const Catch = preservedRoutes?.['_app']?.Catch

const App = () => (
const App = (props: any) => (
<ErrorBoundary fallback={(error, reset) => Catch?.({ error, reset })}>
{_app?.Pending ? <Suspense fallback={<Pending />} children={<Element />} /> : <Element />}
{_app?.Pending ? <Suspense fallback={<Pending />} children={<Element {...props} />} /> : <Element {...props} />}
</ErrorBoundary>
)

const app = { path: '', component: _app?.default ? App : Outlet, data: _app?.Loader || null }
const fallback = { path: '*', component: _404?.default || Fragment }
const app: RouteDefinition = { path: '', component: _app?.default ? App : Fragment, load: _app?.Loader || undefined }
const fallback: RouteDefinition = { path: '*', component: _404?.default || Fragment }

export const routes = [{ ...app, children: [...regularRoutes, fallback] }] as RouteDefinition[]
export const Routes = () => <Router children={useRoutes(routes)()} />
export const routes: RouteDefinition[] = [{ ...app, children: [...regularRoutes, fallback] }] // @ts-expect-error
export const Routes = () => <Router>{routes}</Router>
export const Modals = () => createMemo(() => modalRoutes[useLocation<any>().state?.modal || ''] || Fragment) as any
2 changes: 1 addition & 1 deletion packages/solid-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
},
"devDependencies": {
"@generouted/core": "workspace:*",
"@solidjs/router": "^0.9.1",
"@solidjs/router": "^0.10.1",
"esbuild-plugin-solid": "^0.5.0",
"solid-js": "^1.8.7",
"tsup": "^8.0.1",
Expand Down
14 changes: 5 additions & 9 deletions packages/solid-router/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ export default function Home() {
```tsx
// src/pages/_app.tsx

import { Outlet } from '@solidjs/router'
import { ParentProps } from 'solid-js'

export default function App() {
export default function App(props: ParentProps) {
return (
<section>
<header>
<nav>...</nav>
</header>

<main>
<Outlet />
</main>
<main>{props.children}</main>
</section>
)
}
Expand Down Expand Up @@ -134,7 +132,7 @@ To navigate to a modal use `useModals` hook exported from `src/router.ts`:
```tsx
// src/pages/_app.tsx

import { Outlet } from '@solidjs/router'
import { ParentProps } from 'solid-js'
import { Modals } from '@generouted/solid-router'

import { useModals } from '../router'
Expand All @@ -149,9 +147,7 @@ export default function App(props: ParentProps) {
<button onClick={() => modals.open('/login')}>Open modal</button>
</header>

<main>
<Outlet />
</main>
<main>{props.children}</main>

<Modals />
</section>
Expand Down
Loading

0 comments on commit d466ef2

Please sign in to comment.