Skip to content

Instantly share code, notes, and snippets.

@itkrt2y
Last active April 14, 2024 15:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itkrt2y/5382038f2fa041e1c7eae38cb2b4f273 to your computer and use it in GitHub Desktop.
Save itkrt2y/5382038f2fa041e1c7eae38cb2b4f273 to your computer and use it in GitHub Desktop.
File based routing for vite, preact and wouter
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