After.js
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 4 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.
Getting Started with After.js
After.js enables Next.js-like data fetching with any React SSR app that uses React Router 4.
Razzle 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.
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
.
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) A Express.js request objectres?: Request
: (server-only) An Express.js response objectmatch
: React Router 4'smatch
object.history
: React Router 4'shistory
object.location
: (client-only) React Router 4'slocation
object.
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'sgetInitialProps
, 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 callgetInitialProps
again
Routing
As you have probably figured out, React Router 4 powers all of After.js's
routing. You can use any and all parts of RR4.
Parameterized Routing
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.
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:
Custom <Document>
After.js works similarly to Next.js with respect to overriding HTML document structure. This comes in handy if you are using a CSS-in-JS library or just want to collect data out of react context before or after render. To do this, create a file in ./src/Document.js
like so:
If you were using something like styled-components
, and you need to wrap you entire app with some sort of additional provider or function, you can do this with renderPage()
.
To use your custom <Document>
, pass it to the Document
option of your After.js render
function.
Custom/Async Rendering
You can provide a custom (potentially async) rendering function as an option to After.js render
function.
If present, it will be used instead of the default ReactDOMServer renderToString function.
It has to return an object of shape { html : string!, ...otherProps }
, in which html
will be used as the rendered string
Thus, setting customRenderer = (node) => ({ html: ReactDOMServer.renderToString(node) })
is the the same as default option.
otherProps
will be passed as props to the rendered Document
Example :