Skip to content
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

[docs] use CodeExample props as cache key #26859

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions docs/docs-beta/src/components/CodeExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ function filterNoqaComments(lines: string[]): string[] {
const contentCache: Record<string, {content?: string; error?: string | null}> = {};

function processModule({
cacheKey,
module,
lineStart,
lineEnd,
path,
}: {
path: string;
lineEnd?: number;
lineStart?: number;
cacheKey: string;
module: any;
lineStart?: number;
lineEnd?: number;
}) {
var lines = module.default.split('\n');

Expand All @@ -48,62 +48,70 @@ function processModule({

lines = filterNoqaComments(lines);
lines = trimMainBlock(lines);
contentCache[path] = {content: lines.join('\n')};
contentCache[cacheKey] = {content: lines.join('\n')};
}

const CodeExample: React.FC<CodeExampleProps> = ({...props}) => {
return (
<Suspense>
<CodeExampleInner {...props} />
</Suspense>
);
};

const CodeExampleInner: React.FC<CodeExampleProps> = ({
filePath,
title,
lineStart,
lineEnd,
language = 'python',
pathPrefix = 'docs_beta_snippets/docs_beta_snippets',
...props
}) => {
const path = pathPrefix + '/' + filePath;
function useLoadModule(cacheKey: string, path: string, lineStart: number, lineEnd: number) {
const isServer = typeof window === 'undefined';
if (isServer) {
/**
* Note: Remove the try/catch to cause a hard error on build once all of the bad code examples are cleaned up.
*/
try {
const module = require(`!!raw-loader!/../../examples/${path}`);
processModule({module, lineStart, lineEnd, path});
processModule({cacheKey, module, lineStart, lineEnd});
} catch (e) {
console.error(e);
contentCache[path] = {error: e.toString()};
contentCache[cacheKey] = {error: e.toString()};
}
}
if (!contentCache[path]) {

if (!contentCache[cacheKey]) {
/**
* We only reach this path on the client.
* Throw a promise to suspend in order to avoid un-rendering the codeblock that we SSR'd
*/
throw import(`!!raw-loader!/../../examples/${path}`)
.then((module) => {
processModule({module, lineStart, lineEnd, path});
processModule({cacheKey, module, lineStart, lineEnd});
})
.catch((e) => {
contentCache[filePath] = {error: e.toString()};
contentCache[cacheKey] = {error: e.toString()};
});
}

const {content, error} = contentCache[path];
return contentCache[cacheKey];
}

const CodeExample: React.FC<CodeExampleProps> = ({...props}) => {
return (
<Suspense>
<CodeExampleInner {...props} />
</Suspense>
);
};

const CodeExampleInner: React.FC<CodeExampleProps> = (props) => {
const {
filePath,
title,
lineStart,
lineEnd,
language = 'python',
pathPrefix = 'docs_beta_snippets/docs_beta_snippets',
...extraProps
} = props;

const path = pathPrefix + '/' + filePath;
const cacheKey = JSON.stringify(props);
const {content, error} = useLoadModule(cacheKey, path, lineStart, lineEnd);

if (error) {
return <div style={{color: 'red', padding: '1rem', border: '1px solid red'}}>{error}</div>;
}

return (
<CodeBlock language={language} title={title} {...props}>
<CodeBlock language={language} title={title} {...extraProps}>
{content || 'Loading...'}
</CodeBlock>
);
Expand Down
Loading