Skip to content

Commit

Permalink
Show link to the docs for route segment config options (#42779)
Browse files Browse the repository at this point in the history
This PR adds links to the docs when hovering on the option names and
values. It will be helpful especially when there's an invalid value:


![option-links](https://user-images.githubusercontent.com/3676859/201303933-8a8bcfd3-4e6b-4191-a0b0-2944539ccf66.gif)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
shuding committed Nov 11, 2022
1 parent 4240dcc commit 872c8d5
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions packages/next/server/next-typescript.ts
Expand Up @@ -41,6 +41,7 @@ const API_DOCS: Record<
{
description: string
options: Record<string, string>
link: string
type?: string
isValid?: (value: string) => boolean
getHint?: (value: any) => string
Expand All @@ -59,6 +60,7 @@ const API_DOCS: Record<
'"force-static"':
'This forces caching of all fetches and returns empty values from `useCookies`, `useHeaders` and `useSearchParams`.',
},
link: 'https://beta.nextjs.org/docs/api-reference/segment-config#dynamic',
},
fetchCache: {
description:
Expand All @@ -79,6 +81,7 @@ const API_DOCS: Record<
'"force-cache"':
"This lets you intentionally opt-in to all caching of data. This option forces all fetches to be cache even if the `cache: 'no-store'` option is passed to `fetch()`.",
},
link: 'https://beta.nextjs.org/docs/api-reference/segment-config#fetchcache',
},
preferredRegion: {
description:
Expand All @@ -89,6 +92,7 @@ const API_DOCS: Record<
'"home"': 'Prefer deploying to the Home region.',
'"edge"': 'Prefer deploying to the Edge globally.',
},
link: 'https://beta.nextjs.org/docs/api-reference/segment-config#preferredregion',
},
revalidate: {
description:
Expand All @@ -100,6 +104,7 @@ const API_DOCS: Record<
0: 'Specifying `0` implies that this layout or page should never be static.',
30: 'Set the revalidation time to `30` seconds. The value can be `0` or any positive number.',
},
link: 'https://beta.nextjs.org/docs/api-reference/segment-config#revalidate',
isValid: (value: string) => {
return value === 'false' || Number(value) >= 0
},
Expand All @@ -115,6 +120,7 @@ const API_DOCS: Record<
false:
'Disallow rendering dynamic params that are not generated by `generateStaticParams`.',
},
link: 'https://beta.nextjs.org/docs/api-reference/segment-config#dynamicparams',
},
runtime: {
description:
Expand All @@ -123,6 +129,7 @@ const API_DOCS: Record<
'"nodejs"': 'Prefer the Node.js runtime.',
'"experimental-edge"': 'Prefer the experimental Edge runtime.',
},
link: 'https://beta.nextjs.org/docs/api-reference/segment-config#runtime',
},
}

Expand Down Expand Up @@ -217,8 +224,7 @@ export function createTSPlugin(modules: {
messageText:
'The `"use client"` directive must be put at the top of the file.',
start: node.expression.getStart(),
length:
node.expression.getEnd() - node.expression.getStart(),
length: node.expression.getWidth(),
}
throw e
}
Expand Down Expand Up @@ -428,6 +434,13 @@ export function createTSPlugin(modules: {
const name = declarartion.name
const value = declarartion.initializer

const docsLink = {
kind: 'text',
text:
`\n\nRead more about the "${entryConfig}" option: ` +
API_DOCS[entryConfig].link,
}

if (
value &&
value.getFullStart() <= position &&
Expand Down Expand Up @@ -459,28 +472,41 @@ export function createTSPlugin(modules: {
API_DOCS[entryConfig].getHint?.(key) ||
'',
},
docsLink,
],
}
}
} else {
// Hovers the name of the config
if (API_DOCS[entryConfig]) {
} else {
// Wrong value, display the docs link
overriden = {
kind: ts.ScriptElementKind.enumElement,
kindModifiers: ts.ScriptElementKindModifier.none,
textSpan: {
start: name.getStart(),
length: name.getWidth(),
start: value.getStart(),
length: value.getWidth(),
},
displayParts: [],
documentation: [
{
kind: 'text',
text: getAPIDescription(entryConfig),
},
],
documentation: [docsLink],
}
}
} else {
// Hovers the name of the config

overriden = {
kind: ts.ScriptElementKind.enumElement,
kindModifiers: ts.ScriptElementKindModifier.none,
textSpan: {
start: name.getStart(),
length: name.getWidth(),
},
displayParts: [],
documentation: [
{
kind: 'text',
text: getAPIDescription(entryConfig),
},
docsLink,
],
}
}
})
if (overriden) return overriden
Expand Down Expand Up @@ -529,8 +555,7 @@ export function createTSPlugin(modules: {
code: NEXT_TS_ERRORS.INVALID_SERVER_API,
messageText: `"${name}" is not allowed in Server Components.`,
start: element.name.getStart(),
length:
element.name.getEnd() - element.name.getStart(),
length: element.name.getWidth(),
})
}
}
Expand All @@ -556,7 +581,7 @@ export function createTSPlugin(modules: {
code: NEXT_TS_ERRORS.INVALID_ENTRY_EXPORT,
messageText: `"${name.text}" is not a valid Next.js entry export value.`,
start: name.getStart(),
length: name.getEnd() - name.getStart(),
length: name.getWidth(),
})
} else if (API_DOCS[name.text]) {
// Check if the value is valid
Expand Down Expand Up @@ -612,7 +637,8 @@ export function createTSPlugin(modules: {
ts.isBigIntLiteral(value) ||
ts.isArrayLiteralExpression(value) ||
ts.isObjectLiteralExpression(value) ||
ts.isRegularExpressionLiteral(value)
ts.isRegularExpressionLiteral(value) ||
ts.isPrefixUnaryExpression(value)
) {
isInvalid = true
displayedValue = value.getText()
Expand All @@ -632,7 +658,7 @@ export function createTSPlugin(modules: {
errorMessage ||
`"${displayedValue}" is not a valid value for the "${name.text}" option.`,
start: value.getStart(),
length: value.getEnd() - value.getStart(),
length: value.getWidth(),
})
}
}
Expand Down

0 comments on commit 872c8d5

Please sign in to comment.