-
-
Notifications
You must be signed in to change notification settings - Fork 244
perf: load markdown source code on copy instead of on main request #1386
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
Merged
danielroe
merged 4 commits into
npmx-dev:main
from
alexdln:perf/load-source-on-copy-request
Feb 11, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ddb37be
perf: load markdown source code on copy instead of on main request
alexdln 1ef80c0
perf: change package source caching method
alexdln 163dd1d
perf: update tests for readme routes
alexdln 262ed5e
perf: improve readme-loaders coverage
alexdln File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
| import type { H3Event } from 'h3' | ||
| import { ERROR_NPM_FETCH_FAILED } from '#shared/utils/constants' | ||
| import { resolvePackageReadmeSource } from '#server/utils/readme-loaders' | ||
|
|
||
| export default async function getMarkdownReadme(event: H3Event) { | ||
| try { | ||
| const packagePath = getRouterParam(event, 'pkg') ?? '' | ||
| return await resolvePackageReadmeSource(packagePath) | ||
|
Comment on lines
+7
to
+8
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalise Trailing slashes or whitespace can create duplicate cache entries and may lead to validation failures. Normalise once before calling the resolver. Proposed fix- const packagePath = getRouterParam(event, 'pkg') ?? ''
- return await resolvePackageReadmeSource(packagePath)
+ const packagePath = (getRouterParam(event, 'pkg') ?? '').replace(/\/+$/, '').trim()
+ return await resolvePackageReadmeSource(packagePath) |
||
| } catch (error: unknown) { | ||
| handleApiError(error, { | ||
| statusCode: 502, | ||
| message: ERROR_NPM_FETCH_FAILED, | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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,112 @@ | ||
| import * as v from 'valibot' | ||
| import { PackageRouteParamsSchema } from '#shared/schemas/package' | ||
| import { CACHE_MAX_AGE_ONE_HOUR, NPM_MISSING_README_SENTINEL } from '#shared/utils/constants' | ||
|
|
||
| /** Standard README filenames to try when fetching from jsdelivr (case-sensitive CDN) */ | ||
| const standardReadmeFilenames = [ | ||
| 'README.md', | ||
| 'readme.md', | ||
| 'Readme.md', | ||
| 'README', | ||
| 'readme', | ||
| 'README.markdown', | ||
| 'readme.markdown', | ||
| ] | ||
|
|
||
| /** Matches standard README filenames (case-insensitive, for checking registry metadata) */ | ||
| const standardReadmePattern = /^readme(?:\.md|\.markdown)?$/i | ||
|
|
||
| export function isStandardReadme(filename: string | undefined): boolean { | ||
| return !!filename && standardReadmePattern.test(filename) | ||
| } | ||
|
|
||
| /** | ||
| * Fetch README from jsdelivr CDN for a specific package version. | ||
| * Falls back through common README filenames. | ||
| */ | ||
| export async function fetchReadmeFromJsdelivr( | ||
| packageName: string, | ||
| readmeFilenames: string[], | ||
| version?: string, | ||
| ): Promise<string | null> { | ||
| const versionSuffix = version ? `@${version}` : '' | ||
|
|
||
| for (const filename of readmeFilenames) { | ||
| try { | ||
| const url = `https://cdn.jsdelivr.net/npm/${packageName}${versionSuffix}/${filename}` | ||
| const response = await fetch(url) | ||
| if (response.ok) { | ||
| return await response.text() | ||
| } | ||
| } catch { | ||
| // Try next filename | ||
| } | ||
| } | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| export const resolvePackageReadmeSource = defineCachedFunction( | ||
| async (packagePath: string) => { | ||
| const pkgParamSegments = packagePath.split('/') | ||
|
|
||
| const { rawPackageName, rawVersion } = parsePackageParams(pkgParamSegments) | ||
|
|
||
| const { packageName, version } = v.parse(PackageRouteParamsSchema, { | ||
| packageName: rawPackageName, | ||
| version: rawVersion, | ||
| }) | ||
|
|
||
| const packageData = await fetchNpmPackage(packageName) | ||
|
|
||
| let readmeContent: string | undefined | ||
| let readmeFilename: string | undefined | ||
|
|
||
| if (version) { | ||
| const versionData = packageData.versions[version] | ||
| if (versionData) { | ||
| readmeContent = versionData.readme | ||
| readmeFilename = versionData.readmeFilename | ||
| } | ||
| } else { | ||
| readmeContent = packageData.readme | ||
| readmeFilename = packageData.readmeFilename | ||
| } | ||
|
|
||
| const hasValidNpmReadme = readmeContent && readmeContent !== NPM_MISSING_README_SENTINEL | ||
|
|
||
| if (!hasValidNpmReadme || !isStandardReadme(readmeFilename)) { | ||
| const jsdelivrReadme = await fetchReadmeFromJsdelivr( | ||
| packageName, | ||
| standardReadmeFilenames, | ||
| version, | ||
| ) | ||
| if (jsdelivrReadme) { | ||
| readmeContent = jsdelivrReadme | ||
| } | ||
| } | ||
|
|
||
| if (!readmeContent || readmeContent === NPM_MISSING_README_SENTINEL) { | ||
| return { | ||
| packageName, | ||
| version, | ||
| markdown: undefined, | ||
| repoInfo: undefined, | ||
| } | ||
| } | ||
|
|
||
| const repoInfo = parseRepositoryInfo(packageData.repository) | ||
|
|
||
| return { | ||
| packageName, | ||
| version, | ||
| markdown: readmeContent, | ||
| repoInfo, | ||
| } | ||
| }, | ||
| { | ||
| maxAge: CACHE_MAX_AGE_ONE_HOUR, | ||
| swr: true, | ||
| getKey: (packagePath: string) => packagePath, | ||
| }, | ||
| ) |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normalise
packagePathbefore resolving.Keep resolver input consistent with cache key normalisation to avoid duplicate cache entries and edge-case validation errors.
Proposed fix
📝 Committable suggestion