diff --git a/.env.local.example b/.env.local.example new file mode 100644 index 0000000000..951809844f --- /dev/null +++ b/.env.local.example @@ -0,0 +1,8 @@ +# Algolia +NEXT_PUBLIC_ALGOLIA_APP_ID= +NEXT_PUBLIC_ALGOLIA_SEARCH_API_KEY= +NEXT_PUBLIC_ALGOLIA_BASE_SEARCH_INDEX_NAME= + +# GitHub API +# check fine-grained tokens https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#about-personal-access-tokens +GITHUB_TOKEN_READ_ONLY= diff --git a/.gitignore b/.gitignore index 0ad2e445b8..d27aaf7d8c 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ yarn-error.log* .pnpm-debug.log* # local env files +.env .env*.local # vercel diff --git a/src/components/UI/downloads/DownloadsTable.tsx b/src/components/UI/downloads/DownloadsTable.tsx index feb14bc9c4..c21e4865a4 100644 --- a/src/components/UI/downloads/DownloadsTable.tsx +++ b/src/components/UI/downloads/DownloadsTable.tsx @@ -45,7 +45,7 @@ export const DownloadsTable: FC = ({ if (/iPhone/i.test(OS)) return 3; if (/Android/i.test(userAgent)) return 4; return 0; - }, []) + }, []); return ( = ({ : 'none' } > - setTotalReleases(totalReleases[idx])} defaultIndex={getDefaultIndex}> + setTotalReleases(totalReleases[idx])} + defaultIndex={getDefaultIndex} + > {DOWNLOADS_TABLE_TABS.map((tab, idx) => { return ( diff --git a/src/constants.ts b/src/constants.ts index 656859cdb5..9a273ad08a 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -173,6 +173,8 @@ export const LATEST_GETH_RELEASE_URL = 'https://api.github.com/repos/ethereum/go-ethereum/releases/latest'; export const ALL_GETH_COMMITS_URL = 'https://api.github.com/repos/ethereum/go-ethereum/commits/'; export const RELEASE_COMMIT_BASE_URL = 'https://github.com/ethereum/go-ethereum/tree/'; +export const LAST_COMMIT_BASE_URL = + 'https://api.github.com/repos/ethereum/geth-website/commits?path='; // Binaries urls export const BINARIES_BASE_URL = 'https://gethstore.blob.core.windows.net/builds/'; diff --git a/src/pages/404.tsx b/src/pages/404.tsx index 82f268a70a..8fb61d55d6 100644 --- a/src/pages/404.tsx +++ b/src/pages/404.tsx @@ -5,7 +5,7 @@ import NextLink from 'next/link'; import { GopherHomeFront } from '../components/UI/svgs'; import { PageMetadata } from '../components/UI'; -import { METADATA} from '../constants'; +import { METADATA } from '../constants'; const Page404NotFound: NextPage = ({}) => { return ( @@ -13,10 +13,16 @@ const Page404NotFound: NextPage = ({}) => {
- + { 404 { // Reads file data for markdown pages export const getStaticProps: GetStaticProps = async context => { + const navLinks = yaml.load(fs.readFileSync('src/data/documentation-links.yaml', 'utf8')); + const { slug } = context.params as ParsedUrlQuery; const filePath = (slug as string[])!.join('/'); let file; - let lastModified; - - const navLinks = yaml.load(fs.readFileSync('src/data/documentation-links.yaml', 'utf8')); + // read file try { - file = fs.readFileSync(`${filePath}.md`, 'utf-8'); - lastModified = fs.statSync(`${filePath}.md`); - } catch { file = fs.readFileSync(`${filePath}/index.md`, 'utf-8'); - lastModified = fs.statSync(`${filePath}/index.md`); + } catch (error) { + file = fs.readFileSync(`${filePath}.md`, 'utf-8'); } + const lastModified = await getLastModifiedDate(filePath); const { data: frontmatter, content } = matter(file, MATTER_OPTIONS); return { @@ -62,7 +60,7 @@ export const getStaticProps: GetStaticProps = async context => { frontmatter, content, navLinks, - lastModified: getParsedDate(lastModified.mtime, { + lastModified: getParsedDate(lastModified, { month: 'long', day: 'numeric', year: 'numeric' diff --git a/src/utils/fetchLatestReleaseCommit.ts b/src/utils/fetchLatestReleaseCommit.ts index 005abcbb33..2f28465a69 100644 --- a/src/utils/fetchLatestReleaseCommit.ts +++ b/src/utils/fetchLatestReleaseCommit.ts @@ -1,7 +1,12 @@ import { ALL_GETH_COMMITS_URL } from '../constants'; export const fetchLatestReleaseCommit = (versionNumber: string) => { - return fetch(`${ALL_GETH_COMMITS_URL}/${versionNumber}`) + const headers = new Headers({ + // About personal access tokens https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#about-personal-access-tokens + Authorization: 'Token ' + process.env.GITHUB_TOKEN_READ_ONLY + }); + + return fetch(`${ALL_GETH_COMMITS_URL}/${versionNumber}`, { headers }) .then(response => response.json()) .then(commit => commit.sha.slice(0, 8)); }; diff --git a/src/utils/fetchLatestReleaseVersionAndName.ts b/src/utils/fetchLatestReleaseVersionAndName.ts index e022766067..423b89ab62 100644 --- a/src/utils/fetchLatestReleaseVersionAndName.ts +++ b/src/utils/fetchLatestReleaseVersionAndName.ts @@ -1,7 +1,12 @@ import { LATEST_GETH_RELEASE_URL } from '../constants'; export const fetchLatestReleaseVersionAndName = () => { - return fetch(LATEST_GETH_RELEASE_URL) + const headers = new Headers({ + // About personal access tokens https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#about-personal-access-tokens + Authorization: 'Token ' + process.env.GITHUB_TOKEN_READ_ONLY + }); + + return fetch(LATEST_GETH_RELEASE_URL, { headers }) .then(response => response.json()) .then(release => { return { diff --git a/src/utils/getLastModifiedDate.ts b/src/utils/getLastModifiedDate.ts new file mode 100644 index 0000000000..af0598d37e --- /dev/null +++ b/src/utils/getLastModifiedDate.ts @@ -0,0 +1,18 @@ +import { LAST_COMMIT_BASE_URL } from '../constants'; + +export const getLastModifiedDate = async (filePath: string) => { + const headers = new Headers({ + // About personal access tokens https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#about-personal-access-tokens + Authorization: 'Token ' + process.env.GITHUB_TOKEN_READ_ONLY + }); + + return fetch(`${LAST_COMMIT_BASE_URL}${filePath}/index.md&page=1&per_page=1`, { headers }) + .then(res => res.json()) + .then(commits => commits[0].commit.committer.date) + .catch(_ => + fetch(`${LAST_COMMIT_BASE_URL}${filePath}.md&page=1&per_page=1`, { headers }) + .then(res => res.json()) + .then(commits => commits[0].commit.committer.date) + .catch(console.error) + ); +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index f950ec7d46..1616b13b8d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,6 +4,7 @@ export { fetchLatestReleaseVersionAndName } from './fetchLatestReleaseVersionAnd export { fetchXMLData } from './fetchXMLData'; export { getChecksum } from './getChecksum'; export { getKebabCaseFromName } from './getKebabCaseFromName'; +export { getLastModifiedDate } from './getLastModifiedDate'; export { getLatestBinaryURL } from './getLatestBinaryURL'; export { getOS } from './getOS'; export { getParsedDate } from './getParsedDate';