Skip to content

Commit

Permalink
fix: use github api for build (nodejs#7003)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovflowd authored Aug 23, 2024
1 parent 90e5a50 commit 8e3046b
Show file tree
Hide file tree
Showing 5 changed files with 603 additions and 457 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ jobs:
# this should be a last resort in case by any chances the build memory gets too high
# but in general this should never happen
NODE_OPTIONS: '--max_old_space_size=4096'
# Used for API requests that require GitHub API scopes
NEXT_GITHUB_API_KEY: ${{ secrets.GITHUB_TOKEN }}

- name: Build Next.js (Static)
# We only run full static builds within Pull Requests. As they're not needed on `merge_group` or `push` events
Expand All @@ -115,6 +117,8 @@ jobs:
# this should be a last resort in case by any chances the build memory gets too high
# but in general this should never happen
NODE_OPTIONS: '--max_old_space_size=4096'
# Used for API requests that require GitHub API scopes
NEXT_GITHUB_API_KEY: ${{ secrets.GITHUB_TOKEN }}

- name: Sync Orama Cloud
# We only want to sync the Orama Cloud production indexes on `push` events.
Expand Down
14 changes: 12 additions & 2 deletions apps/site/app/[locale]/next-data/api-data/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { deflateSync } from 'node:zlib';

import provideReleaseData from '@/next-data/providers/releaseData';
import { VERCEL_REVALIDATE } from '@/next.constants.mjs';
import { GITHUB_API_KEY, VERCEL_REVALIDATE } from '@/next.constants.mjs';
import { defaultLocale } from '@/next.locales.mjs';
import type { GitHubApiFile } from '@/types';
import { getGitHubApiDocsUrl } from '@/util/gitHubUtils';
import { parseRichTextIntoPlainText } from '@/util/stringUtils';

// Defines if we should use the GitHub API Key for the request
// based on the environment variable `GITHUB_API_KEY`
const authorizationHeaders = GITHUB_API_KEY
? { headers: { Authorization: `Bearer ${GITHUB_API_KEY}` } }
: undefined;

// Formats a pathname for an API file from Markdown file basename
const getPathnameForApiFile = (name: string, version: string) =>
`docs/${version}/api/${name.replace('.md', '.html')}`;

Expand All @@ -20,7 +27,10 @@ export const GET = async () => {
release => release.status === 'LTS'
)!;

const gitHubApiResponse = await fetch(getGitHubApiDocsUrl(versionWithPrefix));
const gitHubApiResponse = await fetch(
getGitHubApiDocsUrl(versionWithPrefix),
authorizationHeaders
);

return gitHubApiResponse.json().then((apiDocsFiles: Array<GitHubApiFile>) => {
// maps over each api file and get the download_url, fetch the content and deflates it
Expand Down
8 changes: 8 additions & 0 deletions apps/site/next.constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,11 @@ export const ORAMA_CLOUD_ENDPOINT =
* This is a public API key and can be shared publicly on the frontend.
*/
export const ORAMA_CLOUD_API_KEY = process.env.NEXT_PUBLIC_ORAMA_API_KEY || '';

/**
* A GitHub Access Token for accessing the GitHub API and not being rate-limited
* The current token is registered on the "nodejs-vercel" GitHub Account.
*
* Note: This has no NEXT_PUBLIC prefix as it should not be exposed to the Browser.
*/
export const GITHUB_API_KEY = process.env.NEXT_GITHUB_API_KEY || '';
Loading

0 comments on commit 8e3046b

Please sign in to comment.