Last active
November 8, 2024 21:10
-
-
Save itkrt2y/5382038f2fa041e1c7eae38cb2b4f273 to your computer and use it in GitHub Desktop.
File based routing for vite, preact and wouter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Route, Switch } from "wouter-preact"; | |
const pageFiles = import.meta.globEager("./pages/**/*.tsx"); | |
const pages = Object.keys(pageFiles) | |
.sort() | |
.reverse() // Ensure the order of page file names. Put `:<filename>` to the end of the array. | |
.map((filePath) => { | |
const path = filePath.slice(8, -4).replace(/\/?index$/, ""); | |
const { Page } = pageFiles[filePath]; | |
return ( | |
<Route key={path} path={`/${path}`}> | |
{(params) => <Page {...params} />} | |
</Route> | |
); | |
}) | |
.concat(<div>404: Not Found!</div>); | |
export const App = () => <Switch>{pages}</Switch>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment