-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
always show the latest version of nvm
- Loading branch information
Showing
6 changed files
with
98 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import provideNvmData from '@/next-data/providers/nvmData'; | ||
import { defaultLocale } from '@/next.locales.mjs'; | ||
|
||
// This is the Route Handler for the `GET` method which handles the request | ||
// for generating static data for the latest nvm version | ||
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers | ||
export const GET = async () => { | ||
const nvmData = provideNvmData(); | ||
|
||
return Response.json(nvmData); | ||
}; | ||
|
||
// This function generates the static paths that come from the dynamic segments | ||
// `[locale]/next-data/nvm-data/` and returns an array of all available static paths | ||
// This is used for ISR static validation and generation | ||
export const generateStaticParams = async () => [ | ||
{ locale: defaultLocale.code }, | ||
]; | ||
|
||
// Enforces that only the paths from `generateStaticParams` are allowed, giving 404 on the contrary | ||
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams | ||
export const dynamicParams = false; | ||
|
||
// Enforces that this route is used as static rendering | ||
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic | ||
export const dynamic = 'error'; | ||
|
||
// Ensures that this endpoint is invalidated and re-executed every X minutes | ||
// so that when new deployments happen, the data is refreshed | ||
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate | ||
export const revalidate = 300; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const latestKnownVersion = 'v0.40.1'; | ||
|
||
/** | ||
* Fetches the latest NVM version | ||
* @returns {Promise<`v${string}`>} Latest NVM version | ||
*/ | ||
export default async function generateNvmData() { | ||
return fetch('https://latest.nvm.sh', { redirect: 'manual' }) | ||
.then(({ headers }) => { | ||
const url = headers.get('location'); | ||
if (!url) { | ||
throw new Error('No redirect location found'); | ||
} | ||
return fetch(url, { redirect: 'manual' }); | ||
}) | ||
.then(x => { | ||
const url = x.headers.get('location'); | ||
const version = url?.slice(url.lastIndexOf('/') + 1); | ||
return version || latestKnownVersion; | ||
}) | ||
.catch(() => latestKnownVersion); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
ENABLE_STATIC_EXPORT, | ||
IS_DEVELOPMENT, | ||
NEXT_DATA_URL, | ||
VERCEL_ENV, | ||
} from '@/next.constants.mjs'; | ||
|
||
export default async function getNvmData(): Promise<`v${string}`> { | ||
if (ENABLE_STATIC_EXPORT || (!IS_DEVELOPMENT && !VERCEL_ENV)) { | ||
const { default: provideNvmData } = await import( | ||
'@/next-data/providers/nvmData' | ||
); | ||
provideNvmData(); | ||
} | ||
|
||
return fetch(`${NEXT_DATA_URL}nvm-data`).then(r => r.json()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { cache } from 'react'; | ||
|
||
import generateNvmData from '@/next-data/generators/nvmData.mjs'; | ||
|
||
const nvmData = await generateNvmData(); | ||
|
||
export default cache(() => nvmData); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters