npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

benbun-react-router

v0.1.8

Published

Ben Bun React Router is a library for delivering server side rendered content via server only routes while maintaining React's ability to rehydrate the DOM.

Downloads

20

Readme

Ben Bun - React Router

Ben Bun React Router is a Bun library for delivering server side rendered content via server only routes while maintaining React's ability to rehydrate the DOM.

Ben Bun will automatically handle all of your React page's routes as well as your GET and POST API routes or any other route you want using the correct path syntax.

Setup

Simply create a "react-routes.js" file at the root directory of your Bun project to automatically manage the React Pages of your frontend.

The React component that you export from your tsx file must be a "Page" component as this is what Ben Bun uses to bind your pages to routes using your react-routes.js file.

Your "react-routes.js" should be formatted like this and located in your root directory:

export default {
  "/": require("./pages/index.tsx"),
  "/secondRoute": require("./pages/index2.tsx"),
  "/thirdRoute": require("./pages/index3.tsx"),
};

You can include as many routes and pages as you like.

Here is an example of a Ben Bun React Page:

//Import readable stream from react for server side rendering
import { renderToReadableStream } from "react-dom/server";
import React, { useState } from 'react';

export function CounterButton() {
    const [count, setCount] = useState(0);

    return (
        <button onClick={() => setCount(count + 1)}>
            Clicked {count} times
        </button>
    );
}
export function Page(){
    return(
        <html>
            <head>
                <title>Button Page</title>
            </head>
            <body>
                <CounterButton />
            </body>
        </html>
    )
}

//Create a pipable stream that is read by the client
export function GetStream() {
    return renderToReadableStream(
        <Page />, {
            bootstrapModules: ['./public/js/hydrate.js'],
        });
}

Every parent component added to the "react-routes.js" must be a "Page" and inside the "GetStream()" function otherwise Ben Bun will not be able to route to the component properly. The GetStream function is used to send the html data to the client.

How to use

Example Usage:

import {Router, Build} from "benbun-react-router";

//Router must inject the routes before building frontend to sync react
export const router = new Router();
//Build the needed frontend file for react hydration
await Build();

const server = Bun.serve({
    fetch(req, server) {

        const headers = {"Set-Cookie": "I generally send the session cookie here!", "Content-Type": "text/html; charset=utf-8"}
        
        //BenBun routing applied here
        return router.route(req, headers);
    },
    error(error) {
        return new Response('404 page not found.', {headers: {"Content-Type": "text/html; charset=utf-8"}, status: 404});
    }
});

Use as you like, I made this because I couldn't find something that did what Ben Bun does. I just wanted server side routing with automatic client side hydration.

You can also add any kind of route you want:

router.add("GET", "/your-route", (request,params,urlPatternResult)=>{
    return new Response("The GET Route");
});
router.add("POST", "/post-route", (request,params,urlPatternResult)=>{
    return new Response({key: "value"} as any);
});

Ben Bun automatically opens a public route at "/public/" to serve all your public files that you don't want to explicitly route to. If you want to explicitly route everything then you don't have to use it. Explicitly designating routes may be faster due to Ben Bun's route matching algorithm.

Disclaimer

I am not a web developer (anymore) by trade, I'm a game developer. Therefor I may have overlooked some small things. Do not hesitate to get in touch and let me know if there are any improvements that can be made!