21. 21
import { match, RoutingContext } from ‘react-router';
import routes from '../src/components/routes';
/*
* For the `indexRoute` plus all
* `childRoutes`, do the following
*/
const location = '/'.concat(path || '');
match(
{ routes, location },
async function handleMatch(err, redirectLocation, renderProps) {
/*
* Render component markup, create output directories
* and write markup to a file whose filename is
* extracted from the route's path.
* Provide fallback of `index.html` for IndexRoute.
*/
}
);
match each location
22. 22
import { exec } from 'child_process';
import React from 'react';
import { renderToString } from ‘react-dom/server';
import fsp from ‘fs-promise';
const html = renderToString(<RoutingContext {...renderProps} />),
directory = determineDirectory(path),
filePath = path || 'index.html';
await exec(`mkdir -p _site/${directory}`, execCallback);
await fsp.writeFile(
`_site/${filePath}`,
`<!DOCTYPE html>${componentHTML}`,
'utf8',
fspCallback
);
render react and write files
23. 23
1. define structure
of pages
2. create HTML files
from rendered
react
3. bundle client-side
JS and provide
instant page linking
36. 36
1. create CLI
3. dev server that
watches and reloads
2. copy file structure
defaults to consumer
37. 37
#!/usr/bin/env node
import program from ‘commander';
program
.command('new [path]')
.description('create a new react-static project')
.action(function handleNew(path, options) {
if (path) {
// copy over defaults
} else {
// handle no path
}
});
$ react-static new portfolio/
38. 38
1. create CLI
3. dev server that
watches and reloads
2. copy file structure
defaults to consumer
40. 40
program
.command('serve')
.description('run the dev server and have it watch for changes')
.action(function handleServe() {
execSync('node_modules/.bin/nodemon', { stdio: [0,1,2] });
});
$ react-static serve
// build HTML & JS & then...
const app = express();
app.use('/', express.static('_site'));
app.listen(port);
console.log(`=> A development server is running at http://
localhost:${port}`);