MCPcopy
hub / github.com/jaredpalmer/after.js

github.com/jaredpalmer/after.js @v3.2.0 sqlite

repository ↗ · DeepWiki ↗ · release v3.2.0 ↗
235 symbols 538 edges 135 files 1 documented · 0%
README

repo-banner

After.js

npm bundle size (scoped) npm Known Vulnerabilities Github Actions GitHub version After-status license Discord

If Next.js and React Router had a baby...

Project Goals / Philosophy / Requirements

Next.js is awesome. However, its routing system isn't for me. IMHO React Router is a better foundation upon which such a framework should be built....and that's the goal here:

  • Routes are just components and don't / should not have anything to do with folder structure. Static route configs are fine.
  • Next.js's getInitialProps was/is a brilliant idea.
  • Route-based code-splitting should come for free or be easy to opt into.
  • Route-based transitions / analytics / data loading / preloading etc. , should either come for free or be trivial to implement on your own.

Table of Contents

Getting Started with After.js

After.js enables Next.js-like data fetching with any React SSR app that uses React Router.

Quickstart

You can quickly bootstrap an SSR React app with After.js using Razzle. While Razzle is not required, this documentation assumes you have the tooling setup for an isomorphic React application.

yarn global add create-after-app
create-after-app myapp
cd myapp
yarn start

Refer to Razzle's docs for tooling, babel, and webpack customization.

Data Fetching

For page components, you can add a static async getInitialProps function. This will be called on both initial server render, and then client mounts. Results are made available on this.props.

// ./src/About.js
import React from 'react';
import { NavLink } from 'react-router-dom';

class About extends React.Component {
  static async getInitialProps({ req, res, match }) {
    const stuff = await CallMyApi();
    return { stuff };
  }

  render() {
    return (



        <NavLink to="/">Home</NavLink>
        <NavLink to="/about">About</NavLink>
        <h1>About</h1>
        {this.props.stuff}



    );
  }
}

export default About;

getInitialProps: (ctx) => Data

Within getInitialProps, you have access to all you need to fetch data on both the client and the server:

  • req?: Request: (server-only) An Express.js request object.
  • res?: Response: (server-only) An Express.js response object.
  • match: React Router's match object.
  • history: React Router's history object.
  • location: (client-only) React Router's location object (you can only use location.pathname on server).
  • scrollToTop: React Ref object that controls scroll behavior when URL changes.

Add Params to getInitialProps: (ctx) => Data

You can extend ctx, and pass your custom params to it. this is useful when you want to fetch some data by condition or store fetched data in a global state managment system (like redux) or you may need to pass those params as props to your component from server.js (e.g result of user agent parsing).

// ./src/server.js
...
try {
  const html = await render({
    req,
    res,
    routes,
    chunks,
    // Anything else you add here will be made available
    // within getInitialProps(ctx)
    // e.g a redux store...
    customThing: 'thing',
  });
  res.send(html);
} catch (error) {
  console.error(error);
  res.json({ message: error.message, stack: error.stack });
}
...

Don't forget to pass your custom params to <After/> in client.js:

// ./src/client.js
...
ensureReady(routes).then(data =>
  hydrate(
    <BrowserRouter>
      {/*
        Anything else you pass to <After/> will be made available
        within getInitialProps(ctx)
        e.g a redux store...
      */}
      <After data={data} routes={routes} customThing="thing" />
    </BrowserRouter>,
    document.getElementById('root')
  )
);
...

Injected Page Props

  • Whatever you have returned in getInitialProps
  • prefetch: (pathname: string) => void - Imperatively prefetch and cache data for a path. Under the hood this will map through your route tree, call the matching route's getInitialProps, store it, and then provide it to your page component. If the user ultimately navigates to that path, the data and component will be ready ahead of time. In the future, there may be more options to control cache behavior in the form of a function or time in milliseconds to keep that data around.
  • refetch: (nextCtx?: any) => void - Imperatively call getInitialProps again
  • isLoading - It shows that if the returned promise from getInitialProps is in the pending state or not

Routing

As you have probably figured out, React Router powers all of After.js's routing. You can use any and all parts of RR.

Parameterized Routing

// ./src/routes.js
import Home from './Home';
import About from './About';
import Detail from './Detail';

// Internally these will become:
// <Route path={path} exact={exact} render={props => <component {...props} data={data} />} />
const routes = [
  {
    path: '/',
    exact: true,
    component: Home,
  },
  {
    path: '/about',
    component: About,
  },
  {
    path: '/detail/:id',
    component: Detail,
  },
];

export default routes;
// ./src/Detail.js
import React from 'react';
import { Route } from 'react-router-dom';

class Detail extends React.Component {
  // Notice that this will be called for
  // /detail/:id
  // /detail/:id/more
  // /detail/:id/other
  static async getInitialProps({ req, res, match }) {
    const item = await CallMyApi(`/v1/item${match.params.id}`);
    return { item };
  }

  render() {
    return (



        <h1>Detail</h1>
        {this.props.item}
        <Route
          path="/detail/:id/more"
          exact
          render={() => 

{this.props.item.more}

}
        />
        <Route
          path="/detail/:id/other"
          exact
          render={() => 

{this.props.item.other}

}
        />



    );
  }
}

export default Detail;

Client Only Data and Routing

In some parts of your application, you may not need server data fetching at all (e.g. settings). With After.js, you just use React Router 4 as you normally would in client land: You can fetch data (in componentDidMount) and do routing the same exact way.

Transition Behavior

By default, after.js will wait for getInitialProps to get resolved or rejected, so when the getInitialProps job is complete, it will show the next page. We call this behavior blocked.

You may want to show the next page with a skeleton or a spinner while getInitialProps is pending. We call this behavior instant.

you can switch to instant behavior by passing a prop to <After />.

// ./src/client.js

// transitionBehavior = blocked | instant

ensureReady(routes).then(data =>
  hydrate(
    <BrowserRouter>
      <After data={data} routes={routes} transitionBehavior="instant" />
    </BrowserRouter>,
    document.getElementById('root')
  )
);

Dynamic 404 and Redirects

404 Page

React Router can detect No Match (404) Routes and show a fallback component, you can define your custom fallback component in routes.js file.

// ./src/routes.js

import React from 'react';
import Home from './Home';
import Notfound from './Notfound';
import { asyncComponent } from '@jaredpalmer/after';

export default [
  // normal route
  {
    path: '/',
    exact: true,
    component: Home,
  },
  // 404 route
  {
    // there is no need to declare path variable
    // react router will pick this component as fallback
    component: Notfound,
  },
];

Notfound component must set staticContext.statusCode to 404 so express can set response status code more info.

// ./src/Notfound.js

import React from 'react';
import { Route } from 'react-router-dom';

function NotFound() {
  return (
    <Route
      render={({ staticContext }) => {
        if (staticContext) staticContext.statusCode = 404;
        return 

The Page You Were Looking For Was Not Found

;
      }}
    />
  );
}

export default NotFound;

if you don't declare 404 component in routes.js After.js will use its default fallback.

Dynamic 404

Sometimes you may need to send a 404 response based on some API response, in this case, react-router don't show fallback and you have to check for that in your component.

import Notfound from './Notfound';

function ProductPage({ product, error }) {
  if (error) {
    if (error.response.status === 404) {
      return <Notfound />;
    }

    return 

Something went Wrong !

;
  }
  {
    /* if there were no errors we have our data */
  }
  return <h1>{product.name}</h1>;
}

ProductPage.getInitialProps = async ({ match }) => {
  try {
    const { data } = await fetchProduct(match.params.slug);
    return { product: data };
  } catch (error) {
    return { error };
  }
};

this makes code unreadable and hard to maintain. after.js makes this easy by providing an API for handling Dynamic 404 pages. you can return { statusCode: 404 } from getInitialProps and after.js will show 404 fallback components that you defined in routes.js for you.

function ProductPage({ product }) {
  return <h1>{product.name}</h1>;
}

ProductPage.getInitialProps = async ({ match }) => {
  try {
    const { data } = await fetchProduct(match.params.slug);
    return { product: data };
  } catch (error) {
    if (error.response.status === 404) return { statusCode: 404 };
    return { error };
  }
};

Redirect

You can redirect the user to another route by using Redirect from react-router, but it can make your code unreadable and hard to maintain. with after.js you can redirect client to other route by returning { redirectTo: "/new-location" } from getInitialProps. this can become handy for authorization when user does not have permission to access a specific route and you can redirect him/her to the login page.

Dashboard.getInitialProps = async ({ match }) => {
  try {
    const { data } = await fetchProfile();
    return { data };
  } catch (error) {
    if (error.response.status === 401) return { redirectTo: '/login' };
    return { error };
  }
};

The redirect will happen before after.js start renders react to string soo it's fast. when using redirectTo default value for statusCode is 301, but you can use any numeric value you want.

Code Splitting

After.js lets you easily define lazy-loaded or code-split routes in your _routes.js file. To do this, you'll need to modify the relevant route's component definition like so:

// ./src/_routes.js
import React from 'react';
import Home from './Home';
import { asyncComponent } from '@jaredpalmer/after';

export default [
  // normal route
  {
    path: '/',
    exact: true,
    component: Home,
  },
  // codesplit route
  {
    path: '/about',
    exact: true,
    component: asyncComponent({
      loader: () => import('./About'), // required
      Placeholder: () => 

...LOADING...

, // this is optional, just returns null by default
    }),
  },
];

Static Site Generation (SSG)

After.js has first class support for SSG and allows you to create super fast static webapps and serve them over CDN.

renderStatic will return the data from getInitialProps and this data will get saved by razzle into a file called page-data.json. After.js won't call getInitialProps at runtime, instead it will read the page-data.json

Extension points exported contracts — how you extend this code

AsyncGetInitialPropsProps (Interface)
(no doc)
packages/after.js/test/components/AsyncGetInitialProps.tsx
AfterpartyProps (Interface)
(no doc)
packages/after.js/src/After.tsx
AfterpartyState (Interface)
(no doc)
packages/after.js/src/After.tsx
SerializeData (Interface)
(no doc)
packages/after.js/src/serializeData.tsx
CtxBase (Interface)
(no doc)
packages/after.js/src/types.ts

Core symbols most depended-on inside this repo

asyncComponent
called by 27
packages/after.js/src/asyncComponent.tsx
render
called by 19
packages/after.js/src/render.tsx
ensureReady
called by 10
packages/after.js/src/ensureReady.ts
renderPage
called by 8
packages/after.js/src/renderApp.tsx
loadInitialProps
called by 6
packages/after.js/src/loadInitialProps.tsx
useAfterContext
called by 4
packages/after.js/src/Document.tsx
getAssets
called by 4
packages/after.js/src/getAssets.ts
load
called by 3
packages/after.js/src/asyncComponent.tsx

Shape

Function 111
Class 55
Method 49
Interface 20

Languages

TypeScript100%

Modules by API surface

packages/after.js/src/types.ts16 symbols
packages/after.js/src/utils.ts12 symbols
packages/after.js/src/asyncComponent.tsx8 symbols
packages/after.js/src/Document.tsx8 symbols
packages/babel-plugin-after/src/helpers.js7 symbols
packages/babel-plugin-after/lib/index.js7 symbols
packages/babel-plugin-after/lib/helpers.js7 symbols
packages/after.js/src/After.tsx7 symbols
packages/after.js/test/components/AsyncGetInitialProps.tsx6 symbols
packages/babel-plugin-after/src/index.js5 symbols
examples/with-redux/src/common/actions/index.js5 symbols
packages/after.js/test/components/NonDynamicExport.tsx4 symbols

Dependencies from manifests, versioned

@babel/cli7.8.4 · 1×
@babel/core7.9.0 · 1×
@babel/plugin-syntax-dynamic-import7.8.3 · 1×
@babel/preset-env7.9.0 · 1×
@jaredpalmer/afterlatest · 1×
@material-ui/core4.12.3 · 1×
@size-limit/preset-small-lib6.0.4 · 1×
@types/express4.17.3 · 1×
@types/jest25.2.1 · 1×
@types/node13.13.2 · 1×
@types/react16.9.34 · 1×
@types/react-dom16.9.6 · 1×

For agents

$ claude mcp add after.js \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact