From eded8388b7a8ffdc94d852ee54b7e074d1d8415b Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Sun, 7 Jan 2018 20:25:49 +0100 Subject: [PATCH] [examples] Add a Gatsby example --- examples/gatsby/.gitignore | 2 + examples/gatsby/README.md | 21 ++++++++ examples/gatsby/gatsby-config.js | 5 ++ examples/gatsby/gatsby-ssr.js | 32 ++++++++++++ examples/gatsby/package.json | 18 +++++++ examples/gatsby/src/getPageContext.js | 42 +++++++++++++++ examples/gatsby/src/pages/index.js | 73 +++++++++++++++++++++++++++ examples/gatsby/src/withRoot.js | 45 +++++++++++++++++ examples/nextjs/README.md | 2 - 9 files changed, 238 insertions(+), 2 deletions(-) create mode 100644 examples/gatsby/.gitignore create mode 100644 examples/gatsby/README.md create mode 100644 examples/gatsby/gatsby-config.js create mode 100644 examples/gatsby/gatsby-ssr.js create mode 100644 examples/gatsby/package.json create mode 100644 examples/gatsby/src/getPageContext.js create mode 100644 examples/gatsby/src/pages/index.js create mode 100644 examples/gatsby/src/withRoot.js diff --git a/examples/gatsby/.gitignore b/examples/gatsby/.gitignore new file mode 100644 index 00000000000000..6a8ed7b05985c5 --- /dev/null +++ b/examples/gatsby/.gitignore @@ -0,0 +1,2 @@ +/public/ +/.cache/ diff --git a/examples/gatsby/README.md b/examples/gatsby/README.md new file mode 100644 index 00000000000000..c84211a3fd0b01 --- /dev/null +++ b/examples/gatsby/README.md @@ -0,0 +1,21 @@ +# Gatsby example + +## How to use + +Download the example [or clone the repo](https://github.com/mui-org/material-ui): + +```bash +curl https://codeload.github.com/mui-org/material-ui/tar.gz/v1-beta | tar -xz --strip=2 material-ui-1-beta/examples/gatsby +cd gatsby +``` + +Install it and run: + +```bash +npm install +npm run develop +``` + +## The idea behind the example + +[Gatsby](https://github.com/gatsbyjs/gatsby) is a static site generator for React. diff --git a/examples/gatsby/gatsby-config.js b/examples/gatsby/gatsby-config.js new file mode 100644 index 00000000000000..d165f99e9c626f --- /dev/null +++ b/examples/gatsby/gatsby-config.js @@ -0,0 +1,5 @@ +module.exports = { + siteMetadata: { + title: 'Gatsby Default Starter', + }, +}; diff --git a/examples/gatsby/gatsby-ssr.js b/examples/gatsby/gatsby-ssr.js new file mode 100644 index 00000000000000..afdc61bca2f2c3 --- /dev/null +++ b/examples/gatsby/gatsby-ssr.js @@ -0,0 +1,32 @@ +/* eslint-disable react/no-danger */ + +import React from 'react'; +import { renderToString } from 'react-dom/server'; +import { JssProvider } from 'react-jss'; +import getPageContext from './src/getPageContext'; + +exports.replaceRenderer = ({ bodyComponent, replaceBodyHTMLString, setHeadComponents }) => { + // Get the context of the page to collected side effects. + const pageContext = getPageContext(); + + const bodyHTML = renderToString( + + {React.cloneElement(bodyComponent, { + pageContext, + })} + , + ); + + replaceBodyHTMLString(bodyHTML); + setHeadComponents([ +