Recooler is a server-side-only framework designed around [htmx](https://htmx.org/) and [hono](https://hono.dev/) with async JSX components made to feel like PHP.
npm install farm-plugin-recoolerRecooler is a server-side-only framework designed around htmx and hono with async JSX components made to feel like PHP.
The name recooler is an homage to the htmx predecessor intercooler.
https://github.com/ssttevee/recooler-cloudflare-workers-template
TODO: create a npm create command to scaffold a new project with a basic setup.
By default, an esm module is emitted with a hono instance as the default export and a thin adapter function for the cloudflare pages' onRequest hook.
Alternatively, a custom entrypoint module may be provided using the "recooler::app" to import the app for additional configuration of the hono instance and exports. Once such use case for this is to add additional handlers for extra cloudflare workers features, such as cron triggers, event queues, or even email routing.
``ts
// entrypoint.ts
import app from "recooler::app";
export default {
fetch: app.fetch.bind(app),
async scheduled(event: ScheduledEvent) {
// handle cron job event
},
};
`
- Routing
- Components
- Page Components
- Head Functions
- Request Handlers
- Layouts
- Middleware
- Form Actions
- Event Handlers
- Root Component
- Error Boundaries
- Suspense
A special directory structure is used to build the hono app routes. By default, the route directory is routes inside the source directory, which is src by default. The directory structure is as follows:
``
src
├── components
│ └── ...
├── ...
├── routes
│ ├── (public)
│ │ ├── [[other]]/index.tsx
│ │ ├── about/index.tsx
│ │ ├── blog
│ │ │ ├── [post]/index.tsx
│ │ │ └── index.tsx
│ │ └── layout.tsx
│ ├── (admin)
│ │ ├── admin
│ │ │ ├── index.tsx
│ │ │ ├── middleware@10-db.tsx
│ │ │ └── middleware@20-auth.tsx
│ │ └── layout.tsx
│ ├── index.tsx
│ └── middleware.ts
├── ...
This will generate the following hono routes:
``
/
/:other{.+}
/about
/admin
/blog
/blog/:post
The index.tsx files are route modules used to indicate a route.
The layout.tsx files are used as the default layout for all routes in the directory and sub-directories. Only one layout file may be used per directory, and they are applied additively. (i.e. they build upon each other.)
The middleware[@{name}].ts files are used to define hono middleware for the route. Middleware files may contain JSX and have the .tsx extension. Middleware file name may include a name to use more than one middleware for a particular route using an @ after the word middleware. Middleware is executed in the alphabetical order of the file name. It is convention to add a number to the name to guarantee a certain order of execution.
The (parenthesized) directory names are used to group routes together for layout purposes and are completely removed from the final route. Any duplicate routes resulting from the parenthesized directory names are added to hono like any other route, it is up to hono (i.e. the first one) to pick which one to use.
The [param] directory name are replaced with a path parameter. Please see the hono for more information.
The [[wildcard]] directory name is a special case that is of the path parameter that matches multiple path segments. In hono terms, it is replaced with :wildcard{.+}. Please see the hono for more information.
While not strictly a feature of Recooler, components are optionally async functions that return JSX elements. This is useful for composition and abstractions.
Component are "uncolored" or "colorblind" with a few exceptions. This meaning that async and non-async components may be freely interleaved without any additional effort. However, Suspense may be used as a boundary to defer rendering of long-running async components to improve time-to-first-byte.
Page components are the default export of a route module. Whatever is returned is rendered as the page HTML. They are given RouteProps as the properties.
`ts
declare interface RouteProps
/**
* The request ID for the use with
*/
rid: string | number;
/**
* The current url, pre-parsed for convenience.
*/
url: URL;
/**
* The hono request context.
*/
ctx: TContext;
/**
* The head object used to render the head elements.
*/
head: RouteHead;
}
`
Within in any component (page, layout, root, or other component) or head function, a Response may be thrown to immediately stop rendering and return the response. This is useful for handling redirects, 404s, and other edge cases. One exception to this is when within a boundary, because the response will have already been sent to the client. Note that this will still work through an , but only if imported through "farm-plugin-recooler/helpers" because the catch function is overridden to handle this case.
Route modules, layout modules, and the root component module may optionally export a head object (RouteHead) or function (RouteHeadFn), used to set the head elements for the page. If an object is exported, it will overwrite all prior head elements. If a function is exported, then the implementor will have access to the prior head object and may choose to extend or overwrite it. Head functions are applied out to in, starting from the root component, applying the head function from each layout module, and finally the route module. This means the route module's head export will have the final say on the head elements.
`ts
declare interface RouteHead {
title?: string;
base?: string | JSX.IntrinsicElements["base"];
metas?: JSX.IntrinsicElements["meta"][];
links?: JSX.IntrinsicElements["link"][];
scripts?: JSX.IntrinsicElements["script"][];
}
declare interface RouteHeadFn
(ctx: TContext, prev: RouteHead): Promise
}
`
Request handlers are an escape hatch to use regular hono request handlers. They are defined by exporting onGet, onPost, onPatch, onPut, onDelete, onOptions, or onRequest functions. onRequest will handle all methods, while the other will handle their own respective methods.
Layout are implemented as "Higher Order Components", or HOCs, that wrap the page component. They are used to apply common layout elements to all pages in the directory and sub-directories. Only one layout file may be used per directory, and they are applied additively. (i.e. they build upon each other.) A layout HOC must be exported as the default export of a layout module to take effect.
Regular hono middleware may be exported from a middleware module to take effect for the particular route. The behavior is the same as calling app.use("/route", middleware) on a hono instance. The only special sauce from Recooler is the folder directory structure and ordering by file name.
Middleware files placed directly in a group directory (i.e. a parenthesized directory name) will only apply to the routes in the group. This is unlike regular middleware which will handle all requests matching the prefix regardless of whether there is a route handler.
Any hx-{method} attributes with function values will be converted to a form action. This works for all components within the configured source directory (which is src by default), including layouts and the root component. The function may optionally be async and return a JSX element or a hono response.
This is a little similar to next.js's "use server" functions, but specifically for htmx.
`ts`
declare interface FormActionFn
(
ctx: TContext,
payload: TPayload,
): import("hono/types").HandlerResponse
}
Examples:
`tsx`
return (
hx-swap="outerHTML"
hx-post={() => {
return you clicked me!;
}}
>
click me
);
`tsx`
return (
hx-post={(ctx: HonoContext) => {
ctx.header("hx-push-url", "/pressed");
return ctx.text("you clicked me!");
}}
>
click me
);
Form actions can also read request bodies from form submissions.
Example:
`tsxyou entered ${payload.name} and ${payload.remember}
return (
hx-post={(ctx: HonoContext, payload: { remember?: "on"; name: string }) => {
return ctx.text();`
}}
>
);
NOTE: Routes with path parameters must have ctx available as a prop. See gotchas and workarounds.
Any hx-on:{event} attributes with function values will be extracted and bundled into a specialized client script for each page. Just like form actions, this works for all components within the configured source directory (which is src by default), including layouts and the root component. These function can be expected to run on the client, as if simply added with elem.addEventListener("event", fn). It does not have access to the hono context because it is executed on the client side, but it does have access to the element that triggered the event, as well as the event itself.
`ts`
declare interface EventHandlerFn
(this: TElement, event: TEvent): any;
}
The root component is an optional HOC that wraps the entire page. It is meant to be used for rendering the basic HTML document structure, including the ,
, and elements. The client script is also passed to the root component which should be embedded inline after the content or just before the tag.The default root component will render the content and the head elements in a basic HTML template with the client script (if any) embedded, inline, just before the
tag.Since the client script is only passed to the root component, it's not very straightforward to load it as a separate script or caching purposes. However, since client scripts are fairly likely to be small, no prescriptions will ad be made for this.
``ts`
declare interface RootHOC {
(Content: Component
}
is a special component that is able to handle any errors thrown by its async children. This is a leak in the colorblind abstraction.
If a non-sync component can throw and the error must be caught with an , then it can either be converted to an async component (by adding async to the function declaration) or by wrapping the child in a simple function that will be subsequently be called the .
`tsx`
return (
{() =>
)
For more information about , please see the easy-jsx-html-engine documentation.
works similarly to but is used to defer rendering of long-running async components to improve time-to-first-byte.
When any of the children are async, the fallback component will be immediately rendered and sent to the client. Once all of the async children are resolved, the fallback will be replaced with the final rendered children.
Note that this requires some level of server support and may not work in every server environment.
For more information about , please see the easy-jsx-html-engine documentation.
Hono middleware/handlers have an execution order.
Here is what it should be:
``
1. action handlers
2. middleware
3. onRequest
├── onPost, onPatch, onPut, onDelete, onOptions
└── onGet
└── (page handler)
While recooler is designed around htmx, it is not a requirement. It is fully functional as a standalong routing framework.
This also means that htmx is not imported by default. So please make sure to include it in either your root component or head functions where necessary.
`ts`
export const head: RouteHeadFn = (_ctx, prev) => ({
...prev,
scripts: [
...(prev.scripts ?? []),
{
src: "https://cdn.jsdelivr.net/npm/htmx.org@1.9.12/dist/htmx.min.js",
integrity: "sha256-RJMXreeIHpSVENthSZHhlcOgmcTHkcJNrOxV+fSipFI=",
crossorigin: "anonymous",
},
],
});
Recooler can also be used for static site generation using the "farm-plugin-ssg" module.
`ts
// farm.config.ts
import { defineConfig } from "@farmfe/core";
import ssg from "farm-plugin-ssg";
export default defineConfig({
// ...
plugins: [
// ...
ssg(),
// ...
],
// ...
});
`
Here is a running list of rough edges that you may encounter and how they can be worked around:
``
TypeError: Cannot read properties of undefined (reading 'req')
For pages with a parameterized route (i.e. /article/[id]/index.tsx), there needs a way to get the path parameter value to get the actual path. As such, the plugin assumes that the component containing the form action function to have ctx passed in with the props. This assumption is made because it is true for the page component, which makes it very easy for simple apps.
`tsx
// /article/[id]/index.tsx
export default function (props: RouteProps) {
return (
<>
{/ ... /}
{/ ... /}
>
);
}
function LikeButton({ ...otherProps }: OtherProps & { ctx: HonoContext }) {
return (
hx-get={async () => {
await doSomething();
return <>>;
}}
>
Like +1
);
}
`
NOTE: ctx does not need to be destructured or pulled out in any way. The plugin is able to handle getting the path param, regardless of how the props are used, as long as it is passed into the component with the name/attribute ctx`.
The inspiration for this project came from my nostalgia for my childhood days as a PHP main. I wanted to bring back the experience of writing server-side logic in-line with the client-side markup without the mess of interleaving JS and PHP. Recooler is the manifestation of that idea built on top of modern technologies and tooling for JS.
This is my side project that I'm using for my other personal projects. While I do use it in a "production" capacity, it is nowhere near "battle-tested". I am not responsible for any damage that may occur from using this project. Use at your own risk.
Pull requests are welcome, but please be nice.