Skip to content

Commit 0dd5dfc

Browse files
committed
always show the latest version of nvm
1 parent b12daaa commit 0dd5dfc

File tree

6 files changed

+98
-11
lines changed

6 files changed

+98
-11
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import provideNvmData from '@/next-data/providers/nvmData';
2+
import { defaultLocale } from '@/next.locales.mjs';
3+
4+
// This is the Route Handler for the `GET` method which handles the request
5+
// for generating static data for the latest nvm version
6+
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
7+
export const GET = async () => {
8+
const nvmData = provideNvmData();
9+
10+
return Response.json(nvmData);
11+
};
12+
13+
// This function generates the static paths that come from the dynamic segments
14+
// `[locale]/next-data/nvm-data/` and returns an array of all available static paths
15+
// This is used for ISR static validation and generation
16+
export const generateStaticParams = async () => [
17+
{ locale: defaultLocale.code },
18+
];
19+
20+
// Enforces that only the paths from `generateStaticParams` are allowed, giving 404 on the contrary
21+
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams
22+
export const dynamicParams = false;
23+
24+
// Enforces that this route is used as static rendering
25+
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic
26+
export const dynamic = 'error';
27+
28+
// Ensures that this endpoint is invalidated and re-executed every X minutes
29+
// so that when new deployments happen, the data is refreshed
30+
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate
31+
export const revalidate = 300;

apps/site/components/Downloads/Release/ReleaseCodeBox.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ const ReleaseCodeBox: FC = () => {
2121
const t = useTranslations();
2222

2323
useEffect(() => {
24-
const updatedCode = getNodeDownloadSnippet(release, os, t)[platform];
25-
// Docker and NVM support downloading tags/versions by their full release number
26-
// but usually we should recommend users to download "major" versions
27-
// since our Download Buttons get the latest minor of a major, it does make sense
28-
// to request installation of a major via a package manager
29-
memoizedShiki.then(shiki => shiki(updatedCode, 'bash')).then(setCode);
30-
// Only react when the specific release number changed
24+
async function getSnippet() {
25+
const [shiki, { [platform]: updatedCode }] = await Promise.all([
26+
memoizedShiki,
27+
getNodeDownloadSnippet(release, os, t),
28+
]);
29+
30+
// Docker and nvm support downloading tags/versions by their full release number
31+
// but usually we should recommend users to download "major" versions since
32+
// our Download Buttons get the latest minor of a major, it does make sense
33+
// to request installation of a major via a package manager
34+
setCode(await shiki(updatedCode, 'bash'));
35+
// Only react when the specific release number changed
36+
}
37+
getSnippet();
3138
// eslint-disable-next-line react-hooks/exhaustive-deps
3239
}, [release.versionWithPrefix, os, platform]);
3340

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const latestKnownVersion = 'v0.40.1';
4+
5+
/**
6+
* Fetches the latest NVM version
7+
* @returns {Promise<`v${string}`>} Latest NVM version
8+
*/
9+
export default async function generateNvmData() {
10+
return fetch('https://latest.nvm.sh', { redirect: 'manual' })
11+
.then(({ headers }) => {
12+
const url = headers.get('location');
13+
if (!url) {
14+
throw new Error('No redirect location found');
15+
}
16+
return fetch(url, { redirect: 'manual' });
17+
})
18+
.then(x => {
19+
const url = x.headers.get('location');
20+
const version = url?.slice(url.lastIndexOf('/') + 1);
21+
return version || latestKnownVersion;
22+
})
23+
.catch(() => latestKnownVersion);
24+
}

apps/site/next-data/nvmData.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {
2+
ENABLE_STATIC_EXPORT,
3+
IS_DEVELOPMENT,
4+
NEXT_DATA_URL,
5+
VERCEL_ENV,
6+
} from '@/next.constants.mjs';
7+
8+
export default async function getNvmData(): Promise<`v${string}`> {
9+
if (ENABLE_STATIC_EXPORT || (!IS_DEVELOPMENT && !VERCEL_ENV)) {
10+
const { default: provideNvmData } = await import(
11+
'@/next-data/providers/nvmData'
12+
);
13+
provideNvmData();
14+
}
15+
16+
return fetch(`${NEXT_DATA_URL}nvm-data`).then(r => r.json());
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { cache } from 'react';
2+
3+
import generateNvmData from '@/next-data/generators/nvmData.mjs';
4+
5+
const nvmData = await generateNvmData();
6+
7+
export default cache(() => nvmData);

apps/site/util/getNodeDownloadSnippet.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import dedent from 'dedent';
22
import type { TranslationValues } from 'next-intl';
33

4+
import getNvmData from '@/next-data/nvmData';
45
import type { NodeRelease } from '@/types';
56
import type { PackageManager } from '@/types/release';
67
import type { UserOS } from '@/types/userOS';
78

8-
export const getNodeDownloadSnippet = (
9+
export async function getNodeDownloadSnippet(
910
release: NodeRelease,
1011
os: UserOS,
1112
t: (key: string, values?: TranslationValues) => string
12-
) => {
13+
) {
1314
const snippets: Record<PackageManager, string> = {
1415
NVM: '',
1516
FNM: '',
@@ -39,7 +40,7 @@ export const getNodeDownloadSnippet = (
3940
if (os === 'MAC' || os === 'LINUX') {
4041
snippets.NVM = dedent`
4142
# ${t('layouts.download.codeBox.installsNvm')}
42-
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
43+
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/${await getNvmData()}/install.sh | bash
4344
4445
# ${t('layouts.download.codeBox.downloadAndInstallNodejsRestartTerminal')}
4546
nvm install ${release.major}
@@ -118,4 +119,4 @@ export const getNodeDownloadSnippet = (
118119
}
119120

120121
return snippets;
121-
};
122+
}

0 commit comments

Comments
 (0)