ff-stream-web

git clone git://archive.git.mtrnord.blog/MTRNord/ff-stream-web.git
Log | Files | Refs | README | LICENSE

render.js (2057B)


      1 /**
      2  * React Starter Kit (https://www.reactstarterkit.com/)
      3  *
      4  * Copyright © 2014-present Kriasoft, LLC. All rights reserved.
      5  *
      6  * This source code is licensed under the MIT license found in the
      7  * LICENSE.txt file in the root directory of this source tree.
      8  */
      9 
     10 import path from 'path';
     11 import fetch from 'node-fetch';
     12 import { writeFile, makeDir } from './lib/fs';
     13 import runServer from './runServer';
     14 
     15 // Enter your paths here which you want to render as static
     16 // Example:
     17 // const routes = [
     18 //   '/',           // => build/public/index.html
     19 //   '/page',       // => build/public/page.html
     20 //   '/page/',      // => build/public/page/index.html
     21 //   '/page/name',  // => build/public/page/name.html
     22 //   '/page/name/', // => build/public/page/name/index.html
     23 // ];
     24 const routes = [
     25   '/',
     26   '/contact',
     27   '/login',
     28   '/register',
     29   '/about',
     30   '/privacy',
     31   '/404', // https://help.github.com/articles/creating-a-custom-404-page-for-your-github-pages-site/
     32 ];
     33 
     34 async function render() {
     35   const server = await runServer();
     36 
     37   // add dynamic routes
     38   // const products = await fetch(`http://${server.host}/api/products`).then(res => res.json());
     39   // products.forEach(product => routes.push(
     40   //   `/product/${product.uri}`,
     41   //   `/product/${product.uri}/specs`
     42   // ));
     43 
     44   await Promise.all(routes.map(async (route, index) => {
     45     const url = `http://${server.host}${route}`;
     46     const fileName = route.endsWith('/') ? 'index.html' : `${path.basename(route, '.html')}.html`;
     47     const dirName = path.join('build/public', route.endsWith('/') ? route : path.dirname(route));
     48     const dist = path.join(dirName, fileName);
     49     const timeStart = new Date();
     50     const response = await fetch(url);
     51     const timeEnd = new Date();
     52     const text = await response.text();
     53     await makeDir(dirName);
     54     await writeFile(dist, text);
     55     const time = timeEnd.getTime() - timeStart.getTime();
     56     console.log(`#${index + 1} ${dist} => ${response.status} ${response.statusText} (${time} ms)`);
     57   }));
     58 
     59   server.kill('SIGTERM');
     60 }
     61 
     62 export default render;