From 9251d79c449b25218d3a337fcc7ae3c3fbb0b334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Sun, 2 Jul 2017 12:32:46 +0200 Subject: [PATCH] client: Add static html export using next@3 --- client/.gitignore | 1 + client/components/Layout.js | 5 +---- client/next.config.js | 5 +++++ client/package.json | 5 ++++- client/pages/about.js | 3 ++- client/pages/index.js | 7 ++++--- client/util/getUserAgent.js | 10 ++++++++++ 7 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 client/util/getUserAgent.js diff --git a/client/.gitignore b/client/.gitignore index a680367e..2b3533c7 100644 --- a/client/.gitignore +++ b/client/.gitignore @@ -1 +1,2 @@ .next +out diff --git a/client/components/Layout.js b/client/components/Layout.js index 87ee4a42..261811af 100644 --- a/client/components/Layout.js +++ b/client/components/Layout.js @@ -15,10 +15,7 @@ const enhance = withContext({ isMobile: isMobile(userAgent), })); -const Layout = ({ - children, - userAgent, -}) => ( +const Layout = ({ children }) => (
diff --git a/client/next.config.js b/client/next.config.js index 9192dae0..a2e0ab6f 100644 --- a/client/next.config.js +++ b/client/next.config.js @@ -15,3 +15,8 @@ exports.webpack = (config) => { return config } + +exports.exportPathMap = () => ({ + '/': { page: '/' }, + '/about': { page: '/about' } +}) diff --git a/client/package.json b/client/package.json index 26143433..c8230e8d 100644 --- a/client/package.json +++ b/client/package.json @@ -6,6 +6,7 @@ "dependencies": { "babel-plugin-transform-inline-environment-variables": "^6.8.0", "babel-preset-env": "^1.5.2", + "ecstatic": "^2.2.1", "is-mobile": "^0.2.2", "isomorphic-fetch": "^2.2.1", "material-ui": "^1.0.0-alpha.20", @@ -22,6 +23,8 @@ "dev": "next dev", "build": "next build", "start": "next start", - "deploy": "now -e HUB_SERVER=https://announce.u-wave.net/" + "deploy": "now -e HUB_SERVER=https://announce.u-wave.net/", + "export": "next build && next export -o out", + "static": "ecstatic out" } } diff --git a/client/pages/about.js b/client/pages/about.js index 15c6ed5a..4b6a6edd 100644 --- a/client/pages/about.js +++ b/client/pages/about.js @@ -1,5 +1,6 @@ import React from 'react'; import Markdown from 'react-markdown'; +import getUserAgent from '../util/getUserAgent'; import Layout from '../components/Layout'; const text = ` @@ -21,7 +22,7 @@ const Image = props => ( export default class About extends React.Component { static async getInitialProps({ req }) { return { - userAgent: req ? req.headers['user-agent'] : navigator.userAgent, + userAgent: getUserAgent(req), }; } diff --git a/client/pages/index.js b/client/pages/index.js index e6fdbe67..dae1f078 100644 --- a/client/pages/index.js +++ b/client/pages/index.js @@ -2,7 +2,7 @@ import React from 'react'; import fetch from 'isomorphic-fetch'; import ms from 'ms'; import { CircularProgress } from 'material-ui/Progress'; - +import getUserAgent from '../util/getUserAgent'; import Layout from '../components/Layout'; import Loading from '../components/Loading'; import ServerListing from '../components/ServerListing'; @@ -27,11 +27,12 @@ async function loadServers() { export default class App extends React.Component { static async getInitialProps({ req }) { + const isExporting = req && !req.headers && typeof navigator === 'undefined'; return { // If we're serving a new request, preload the servers. // If we're transitioning on the client, show a loading indicator. - servers: req ? await loadServers() : null, - userAgent: req ? req.headers['user-agent'] : navigator.userAgent, + servers: req && !isExporting ? await loadServers() : null, + userAgent: getUserAgent(req), }; } diff --git a/client/util/getUserAgent.js b/client/util/getUserAgent.js new file mode 100644 index 00000000..2d7ebdcd --- /dev/null +++ b/client/util/getUserAgent.js @@ -0,0 +1,10 @@ +export default function getUserAgent(req) { + if (req && req.headers) { + return req.headers['user-agent']; + } + if (typeof navigator !== 'undefined' && navigator.userAgent) { + return navigator.userAgent; + } + return 'all'; +} +