Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Fix /versions GitHub API rate limitation #19223

Merged
merged 3 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}),
Expand Down
9 changes: 7 additions & 2 deletions docs/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ function AppWrapper(props) {
))}
</NextHead>
<ReduxProvider store={redux}>
<PageContext.Provider value={{ activePage, pages }}>
<PageContext.Provider value={{ activePage, pages, versions: pageProps.versions }}>
<StylesProvider jss={jss}>
<ThemeProvider>{children}</ThemeProvider>
</StylesProvider>
Expand Down Expand Up @@ -344,16 +344,21 @@ 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: {
userLanguage: ctx.query.userLanguage,
},
});
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(),
Expand Down
40 changes: 40 additions & 0 deletions docs/pages/versions.js
Original file line number Diff line number Diff line change
@@ -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)$/);
Expand All @@ -8,3 +10,41 @@ const reqPrefix = 'pages/versions';
export default function Page() {
return <MarkdownDocs req={req} reqSource={reqSource} reqPrefix={reqPrefix} />;
}

async function getBranches() {
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;
}

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 };
};
1 change: 1 addition & 0 deletions docs/src/modules/components/PageContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const PageContext = React.createContext({
pathname: '',
},
pages: [],
versions: [],
});

if (process.env.NODE_ENV !== 'production') {
Expand Down
53 changes: 3 additions & 50 deletions docs/src/pages/versions/StableVersions.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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';
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/';
const FILTERED_BRANCHES = ['latest', 'staging', 'l10n', 'next'];

const styles = {
root: {
Expand All @@ -22,59 +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([]);

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);
})();
}, []);
const { versions } = React.useContext(PageContext);

return (
<div className={classes.root}>
<Table>
<TableBody>
{docs.map(doc => (
{versions.map(doc => (
<TableRow key={doc.version}>
<TableCell>
<Typography variant="body2">
Expand Down