GithubHelp home page GithubHelp logo

mechanize-systems / asap Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 2.0 684 KB

An opinionated application server for React Single Page Applications

License: MIT License

Shell 0.19% Makefile 2.04% TypeScript 97.13% CSS 0.63%
nodejs http reactjs framework esbuild

asap's Introduction

ASAP

An opinionated application server for React Single Page Applications.

Motivation

Next.js and others are great but they favor use cases of landing pages and ecommerce websites, providing them with SSR, "on the edge" deployment etc.

These are all great features but we don't always need such complexity if all we want to implement is a simple React app and a few API routes along.

Therefore there's ASAP which:

  • ... is a simple application server built on top of tinyhttp, esbuild and React
  • ... strives to enable fast iterative development
  • ... and get out of your way in production

Features

  • ASAP is opinionated

  • ASAP builds both client and server code with esbuild, you don't need to mess with build configuration (also because you can't)

  • In development mode ASAP automatically reloads server code on changes, you don't need to restart the asap serve to iterate

  • ASAP provides a very thin client side application framework on top of React with type safe routing

Quickstart

Initialize a new project:

mkdir example && cd example && pnpm init

Add dependencies to the project:

pnpm add react react-dom @mechanize/asap

Let's create a simple app of two pages.

First of all create app.js in the root of your repository:

import * as React from "react";
import * as ASAP from "@mechanize/asap";

export let routes = {
  index: ASAP.route("/", async () => ({ default: Index })),
  hello: ASAP.route("/hello/:name", async () => ({ default: Hello })),
};

function Index() {
  return (
    <div>
      <p>Welcome!</p>
      <p>
        Go to{" "}
        <ASAP.Link route={routes.hello} params={{ name: "World" }}>
          hello page
        </ASAP.Link>
      </p>
    </div>
  );
}

function Hello({ name }) {
  return <div>Hello, {name}!</div>;
}

ASAP.boot({ routes });

Let's add a few simple API methods, create api.js file also in the root of the repo:

import * as api from "@mechanize/asap/api";

export let listTodos = api.endpoint({
  path: "/todo",
  handle() {
    return [{ id: 1 }];
  },
});

export let getTodo = api.endpoint({
  path: "/todo/:id",
  handle(params) {
    return [{ id: params.id }];
  },
});

Now we can serve the app:

pnpm asap serve

Now for production you'd want to the optimized bundle first:

pnpm asap build --env production

And finally serve the app in production environment:

pnpm asap serve --env production

Design Overview

  • ASAP runs a tinyhttp server to serve HTML page skeletons (an empty page with <script> tags to launch the client application) and API requests
  • in development ASAP compiles client code with esbuild
  • in development ASAP compiles server code with esbuild, this allows to hot reload server code (on changes the bundle will be rebuilt and the next request will be served using newly built code)
  • in production esbuild is not used and built bundles are used instead
  • on client there's @mechanize/asap library which provides suspense-enabled typesafe routing

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.