From 5f4ae83fa2271922a51e9512f3a61579dd43ef21 Mon Sep 17 00:00:00 2001 From: hitesh Date: Mon, 13 Jan 2020 00:58:32 +0530 Subject: [PATCH 1/3] [docs]: fix Versions page GitHub api issue Resolves #14762 --- docs/pages/_app.js | 11 +++++- docs/pages/versions.js | 44 +++++++++++++++++++++ docs/src/pages/versions/StableVersions.js | 47 ++--------------------- 3 files changed, 58 insertions(+), 44 deletions(-) diff --git a/docs/pages/_app.js b/docs/pages/_app.js index 8c21428fe6d471..04f399d2dfd17e 100644 --- a/docs/pages/_app.js +++ b/docs/pages/_app.js @@ -336,6 +336,10 @@ export default class MyApp extends App { render() { const { Component, pageProps } = this.props; + if (pageProps.versions) { + global.__VERSION__ = pageProps.versions; + } + return ( @@ -344,9 +348,13 @@ export default class MyApp extends App { } } -MyApp.getInitialProps = ({ ctx }) => { +MyApp.getInitialProps = async ({ ctx, Component }) => { let pageProps = {}; + if (Component.getInitialProps) { + pageProps = await Component.getInitialProps(ctx); + } + if (!process.browser) { const redux = initRedux({ options: { @@ -354,6 +362,7 @@ MyApp.getInitialProps = ({ ctx }) => { }, }); pageProps = { + ...pageProps, // No need to include other initial Redux state because when it // initialises on the client-side it'll create it again anyway reduxServerState: redux.getState(), diff --git a/docs/pages/versions.js b/docs/pages/versions.js index 86b836c95adac7..e97c8b34f43879 100644 --- a/docs/pages/versions.js +++ b/docs/pages/versions.js @@ -1,4 +1,6 @@ import React from 'react'; +import orderBy from 'lodash/orderBy'; +import sortedUniqBy from 'lodash/sortedUniqBy'; import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; const req = require.context('docs/src/pages/versions', false, /\.(md|js|tsx)$/); @@ -8,3 +10,45 @@ const reqPrefix = 'pages/versions'; export default function Page() { return ; } +let cacheBranches = null; + +async function getBranches() { + try { + if (!cacheBranches) { + const result = await fetch('https://api.github.com/repos/mui-org/material-ui-docs/branches'); + cacheBranches = await result.json(); + } + } catch (err) { + // Swallow the exceptions. + } + + cacheBranches = cacheBranches || []; + return cacheBranches; +} + +Page.getInitialProps = async () => { + const FILTERED_BRANCHES = ['latest', 'staging', 'l10n', 'next']; + + const branches = await getBranches(); + let versions = branches.map(n => n.name); + versions = versions.filter(value => FILTERED_BRANCHES.indexOf(value) === -1); + versions = versions.map(version => ({ + version, + // Replace dot with dashes for Netlify branch subdomains + url: `https://${version.replace(/\./g, '-')}.material-ui.com`, + })); + // Current version. + versions.push({ + version: `v${process.env.LIB_VERSION}`, + url: 'https://material-ui.com', + }); + // Legacy documentation. + versions.push({ + version: 'v0', + url: 'https://v0.material-ui.com', + }); + versions = orderBy(versions, 'version', 'desc'); + versions = sortedUniqBy(versions, 'version'); + + return { versions }; +}; diff --git a/docs/src/pages/versions/StableVersions.js b/docs/src/pages/versions/StableVersions.js index 1ba5d998b85eb5..1cb7410f52ff75 100644 --- a/docs/src/pages/versions/StableVersions.js +++ b/docs/src/pages/versions/StableVersions.js @@ -1,8 +1,6 @@ import 'isomorphic-fetch'; import React from 'react'; import PropTypes from 'prop-types'; -import orderBy from 'lodash/orderBy'; -import sortedUniqBy from 'lodash/sortedUniqBy'; import { withStyles } from '@material-ui/core/styles'; import Table from '@material-ui/core/Table'; import TableBody from '@material-ui/core/TableBody'; @@ -12,7 +10,6 @@ import Typography from '@material-ui/core/Typography'; import Link from 'docs/src/modules/components/Link'; const GITHUB_RELEASE_BASE_URL = 'https://github.com/mui-org/material-ui/releases/tag/'; -const FILTERED_BRANCHES = ['latest', 'staging', 'l10n', 'next']; const styles = { root: { @@ -22,51 +19,15 @@ const styles = { }, }; -let cacheBranches = null; - -async function getBranches() { - try { - if (!cacheBranches) { - const result = await fetch('https://api.github.com/repos/mui-org/material-ui-docs/branches'); - cacheBranches = await result.json(); - } - } catch (err) { - // Swallow the exceptions. - } - - cacheBranches = cacheBranches || []; - return cacheBranches; -} - function StableVersions(props) { const { classes } = props; - const [docs, setDocs] = React.useState([]); + // eslint-disable-next-line no-underscore-dangle + const [docs, setDocs] = React.useState(global.__VERSION__); React.useEffect(() => { (async () => { - const branches = await getBranches(); - let newDocs = branches.map(n => n.name); - newDocs = newDocs.filter(value => FILTERED_BRANCHES.indexOf(value) === -1); - newDocs = newDocs.map(version => ({ - version, - // Replace dot with dashes for Netlify branch subdomains - url: `https://${version.replace(/\./g, '-')}.material-ui.com`, - })); - // Current version. - newDocs.push({ - version: `v${process.env.LIB_VERSION}`, - url: document.location.origin, - }); - // Legacy documentation. - newDocs.push({ - version: 'v0', - url: 'https://v0.material-ui.com', - }); - newDocs = orderBy(newDocs, 'version', 'desc'); - newDocs = sortedUniqBy(newDocs, 'version'); - // The latest version is always using the naked domain. - newDocs[0].url = 'https://material-ui.com'; - setDocs(newDocs); + // eslint-disable-next-line no-underscore-dangle + setDocs(global.__VERSION__); })(); }, []); From 7685129d44c96382139137bc98f15cf0976d6f8f Mon Sep 17 00:00:00 2001 From: hitesh Date: Tue, 14 Jan 2020 22:27:30 +0530 Subject: [PATCH 2/3] [fix]: Review change - Remove version props from global and use context api instead of that. - Remove useEffect from StableVersion.js - Remove cachebranch variable from global scope --- docs/pages/_app.js | 7 +------ docs/pages/versions.js | 12 +++++------- docs/src/modules/components/PageContext.js | 1 + docs/src/pages/versions/StableVersions.js | 13 +++---------- 4 files changed, 10 insertions(+), 23 deletions(-) diff --git a/docs/pages/_app.js b/docs/pages/_app.js index 04f399d2dfd17e..4301357d0ad96f 100644 --- a/docs/pages/_app.js +++ b/docs/pages/_app.js @@ -269,7 +269,6 @@ function findActivePage(currentPages, pathname) { function AppWrapper(props) { const { children, pageProps } = props; - const router = useRouter(); const [redux] = React.useState(() => initRedux(pageProps.reduxServerState)); @@ -313,7 +312,7 @@ function AppWrapper(props) { ))} - + {children} @@ -336,10 +335,6 @@ export default class MyApp extends App { render() { const { Component, pageProps } = this.props; - if (pageProps.versions) { - global.__VERSION__ = pageProps.versions; - } - return ( diff --git a/docs/pages/versions.js b/docs/pages/versions.js index e97c8b34f43879..137b6f5a3c98b2 100644 --- a/docs/pages/versions.js +++ b/docs/pages/versions.js @@ -10,20 +10,18 @@ const reqPrefix = 'pages/versions'; export default function Page() { return ; } -let cacheBranches = null; async function getBranches() { + let branches = []; try { - if (!cacheBranches) { - const result = await fetch('https://api.github.com/repos/mui-org/material-ui-docs/branches'); - cacheBranches = await result.json(); - } + const result = await fetch('https://api.github.com/repos/mui-org/material-ui-docs/branches'); + branches = await result.json(); } catch (err) { // Swallow the exceptions. } - cacheBranches = cacheBranches || []; - return cacheBranches; + branches = branches || []; + return branches; } Page.getInitialProps = async () => { diff --git a/docs/src/modules/components/PageContext.js b/docs/src/modules/components/PageContext.js index be0eefdcf3656d..bf64eec21471ba 100644 --- a/docs/src/modules/components/PageContext.js +++ b/docs/src/modules/components/PageContext.js @@ -7,6 +7,7 @@ const PageContext = React.createContext({ pathname: '', }, pages: [], + versions: [], }); if (process.env.NODE_ENV !== 'production') { diff --git a/docs/src/pages/versions/StableVersions.js b/docs/src/pages/versions/StableVersions.js index 1cb7410f52ff75..4e2c540948da1a 100644 --- a/docs/src/pages/versions/StableVersions.js +++ b/docs/src/pages/versions/StableVersions.js @@ -8,6 +8,7 @@ import TableCell from '@material-ui/core/TableCell'; import TableRow from '@material-ui/core/TableRow'; import Typography from '@material-ui/core/Typography'; import Link from 'docs/src/modules/components/Link'; +import PageContext from 'docs/src/modules/components/PageContext'; const GITHUB_RELEASE_BASE_URL = 'https://github.com/mui-org/material-ui/releases/tag/'; @@ -21,21 +22,13 @@ const styles = { function StableVersions(props) { const { classes } = props; - // eslint-disable-next-line no-underscore-dangle - const [docs, setDocs] = React.useState(global.__VERSION__); - - React.useEffect(() => { - (async () => { - // eslint-disable-next-line no-underscore-dangle - setDocs(global.__VERSION__); - })(); - }, []); + const { versions } = React.useContext(PageContext); return (
- {docs.map(doc => ( + {versions.map(doc => ( From c072143f5e632f512f78edaaf95ca63558ba7739 Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Mon, 27 Jan 2020 10:02:51 +0100 Subject: [PATCH 3/3] use token --- docs/next.config.js | 3 ++- docs/pages/_app.js | 1 + docs/pages/versions.js | 16 +++++++--------- docs/src/pages/versions/StableVersions.js | 1 - 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/docs/next.config.js b/docs/next.config.js index 6a47016bbffc18..565cf763fc258b 100644 --- a/docs/next.config.js +++ b/docs/next.config.js @@ -18,8 +18,9 @@ module.exports = withTypescript({ const plugins = config.plugins.concat([ new webpack.DefinePlugin({ 'process.env': { - LIB_VERSION: JSON.stringify(pkg.version), ENABLE_AD: JSON.stringify(process.env.ENABLE_AD), + GITHUB_AUTH: JSON.stringify(process.env.GITHUB_AUTH), + LIB_VERSION: JSON.stringify(pkg.version), REACT_MODE: JSON.stringify(reactMode), }, }), diff --git a/docs/pages/_app.js b/docs/pages/_app.js index 4301357d0ad96f..ace4d691fade97 100644 --- a/docs/pages/_app.js +++ b/docs/pages/_app.js @@ -269,6 +269,7 @@ function findActivePage(currentPages, pathname) { function AppWrapper(props) { const { children, pageProps } = props; + const router = useRouter(); const [redux] = React.useState(() => initRedux(pageProps.reduxServerState)); diff --git a/docs/pages/versions.js b/docs/pages/versions.js index 137b6f5a3c98b2..39aec2187d9b8c 100644 --- a/docs/pages/versions.js +++ b/docs/pages/versions.js @@ -12,15 +12,13 @@ export default function Page() { } async function getBranches() { - let branches = []; - try { - const result = await fetch('https://api.github.com/repos/mui-org/material-ui-docs/branches'); - branches = await result.json(); - } catch (err) { - // Swallow the exceptions. - } - - branches = branches || []; + const result = await fetch('https://api.github.com/repos/mui-org/material-ui-docs/branches', { + headers: { + Authorization: `Basic ${Buffer.from(process.env.GITHUB_AUTH).toString('base64')}`, + }, + }); + // console.log('headers', result.headers); + const branches = await result.json(); return branches; } diff --git a/docs/src/pages/versions/StableVersions.js b/docs/src/pages/versions/StableVersions.js index 4e2c540948da1a..3cf7db18e45639 100644 --- a/docs/src/pages/versions/StableVersions.js +++ b/docs/src/pages/versions/StableVersions.js @@ -1,4 +1,3 @@ -import 'isomorphic-fetch'; import React from 'react'; import PropTypes from 'prop-types'; import { withStyles } from '@material-ui/core/styles';