Skip to content

Preact support#202

Open
pax-k wants to merge 2 commits intooedotme:mainfrom
pax-k:main
Open

Preact support#202
pax-k wants to merge 2 commits intooedotme:mainfrom
pax-k:main

Conversation

@pax-k
Copy link

@pax-k pax-k commented Jul 31, 2025

I wanted to use filesystem based routing with Preact + Vite (bun create preact), so I found Genrouted which is super cool, but it lacked preact-iso support, so this is my attempt to provide it.

It still needs cleanup and proper testing, but if you are kind enough, please take a look and let me know if you spot anything wrong.

cc @oedotme

@@ -0,0 +1,110 @@
import { Fragment, JSX, h } from 'preact'
import { Suspense } from 'preact/compat'
import { LocationProvider, Router, Route, lazy } from 'preact-iso'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use ErrorBoundary from preact-iso rather than forcing every one into compat mode

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

// Create a component that handles pending and error states
const LazyComponent = (props: any) => {
console.log('LAZY - Rendering component for:', key)
const Page = () => (mod?.Pending ? h(Suspense, { fallback: h(mod.Pending, {}) }, h(Default, props)) : h(Default, props))
Copy link

@rschristian rschristian Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want to be wrapping pages w/ <Suspense> or <ErrorBoundary>; components within a page, sure, but you should probably let iso's <Router> handle suspending children directly.

Off the top of my head, I think this will break the loading states.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

export const components = <Path extends string, Params extends Record<string, any>>() => {
return {
// @ts-expect-error - Type complexity with forwardRef and generics
Link: forwardRef(({ to, params, ...props }: LinkProps<Path | To<Path>, Params>, ref: any) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may need to add a click handler on links to stop event propagation; iso attaches a delegated handler to the window to capture any bubbling link clicks and it's likely the route patterns it uses don't align with the patterns used here (most libs do things a bit differently).

@oedotme
Copy link
Owner

oedotme commented Aug 18, 2025

Hey @pax-k, thanks for submitting this!

It's looking good overall, but I'll take a proper look as soon as I can and will get back to you.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants