Skip to content

Commit 929949e

Browse files
committed
Introduce cacheBust option
Closes #2124
1 parent 6a7323f commit 929949e

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
- TypeDoc will now produce more informative error messages for options which cannot be set from the cli, #2022.
4545
- TypeDoc will now attempt to guess what option you may have meant if given an invalid option name.
4646
- TypeDoc options may now be set under the `typedocOptions` key in `package.json`, #2112.
47-
- Moved sidebar to left of content for consistency with most other websites, #2189
47+
- Moved sidebar to left of content for consistency with most other websites, #2189.
48+
- Added `--cacheBust` option to tell TypeDoc to include include the generation time in files, #2124.
4849

4950
### Bug Fixes
5051

src/lib/output/renderer.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ export class Renderer extends ChildableComponent<
179179
@BindOption("githubPages")
180180
githubPages!: boolean;
181181

182+
/** @internal */
183+
@BindOption("cacheBust")
184+
cacheBust!: boolean;
185+
182186
/** @internal */
183187
@BindOption("lightHighlightTheme")
184188
lightTheme!: ShikiTheme;
@@ -187,6 +191,8 @@ export class Renderer extends ChildableComponent<
187191
@BindOption("darkHighlightTheme")
188192
darkTheme!: ShikiTheme;
189193

194+
renderStartTime = -1;
195+
190196
/**
191197
* Define a new theme that can be used to render output.
192198
* This API will likely be changing at some point, to allow more easily overriding parts of the theme without
@@ -212,10 +218,12 @@ export class Renderer extends ChildableComponent<
212218
outputDirectory: string
213219
): Promise<void> {
214220
const momento = this.hooks.saveMomento();
215-
const start = Date.now();
221+
this.renderStartTime = Date.now();
216222
await loadHighlighter(this.lightTheme, this.darkTheme);
217223
this.application.logger.verbose(
218-
`Renderer: Loading highlighter took ${Date.now() - start}ms`
224+
`Renderer: Loading highlighter took ${
225+
Date.now() - this.renderStartTime
226+
}ms`
219227
);
220228
if (
221229
!this.prepareTheme() ||

src/lib/output/themes/default/DefaultThemeRenderContext.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ export class DefaultThemeRenderContext {
6060
this.theme.owner.hooks.emit(name, this);
6161

6262
/** Avoid this in favor of urlTo if possible */
63-
relativeURL = (url: string | undefined) => {
64-
return url ? this.theme.markedPlugin.getRelativeUrl(url) : url;
63+
relativeURL = (url: string, cacheBust = false) => {
64+
const result = this.theme.markedPlugin.getRelativeUrl(url);
65+
if (cacheBust && this.theme.owner.cacheBust) {
66+
return result + `?cache=${this.theme.owner.renderStartTime}`;
67+
}
68+
return result;
6569
};
6670

67-
urlTo = (reflection: Reflection) => this.relativeURL(reflection.url)!;
71+
urlTo = (reflection: Reflection) => {
72+
return reflection.url ? this.relativeURL(reflection.url) : "";
73+
};
6874

6975
markdown = (
7076
md: readonly CommentDisplayPart[] | NeverIfInternal<string | undefined>

src/lib/output/themes/default/layouts/default.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export const defaultLayout = (context: DefaultThemeRenderContext, props: PageEve
1717
<meta name="description" content={"Documentation for " + props.project.name} />
1818
<meta name="viewport" content="width=device-width, initial-scale=1" />
1919

20-
<link rel="stylesheet" href={context.relativeURL("assets/style.css")} />
21-
<link rel="stylesheet" href={context.relativeURL("assets/highlight.css")} />
20+
<link rel="stylesheet" href={context.relativeURL("assets/style.css", true)} />
21+
<link rel="stylesheet" href={context.relativeURL("assets/highlight.css", true)} />
2222
{context.options.getValue("customCss") && (
23-
<link rel="stylesheet" href={context.relativeURL("assets/custom.css")} />
23+
<link rel="stylesheet" href={context.relativeURL("assets/custom.css", true)} />
2424
)}
25-
<script async src={context.relativeURL("assets/search.js")} id="search-script"></script>
25+
<script async src={context.relativeURL("assets/search.js", true)} id="search-script"></script>
2626
{context.hook("head.end")}
2727
</head>
2828
<body>
@@ -49,7 +49,7 @@ export const defaultLayout = (context: DefaultThemeRenderContext, props: PageEve
4949
{context.footer()}
5050

5151
<div class="overlay"></div>
52-
<script src={context.relativeURL("assets/main.js")}></script>
52+
<script src={context.relativeURL("assets/main.js", true)}></script>
5353

5454
{context.analytics()}
5555
{context.hook("body.end")}

src/lib/utils/options/declaration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export interface TypeDocOptionMap {
130130
githubPages: boolean;
131131
gaID: string;
132132
hideGenerator: boolean;
133+
cacheBust: boolean;
133134
searchInComments: boolean;
134135
cleanOutputDir: boolean;
135136
titleLink: string;

src/lib/utils/options/sources/typedoc.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
362362
help: "Do not print the TypeDoc link at the end of the page.",
363363
type: ParameterType.Boolean,
364364
});
365+
options.addDeclaration({
366+
name: "cacheBust",
367+
help: "Include the generation time in links to static assets.",
368+
type: ParameterType.Boolean,
369+
});
365370
options.addDeclaration({
366371
name: "searchInComments",
367372
help: "If set, the search index will also include comments. This will greatly increase the size of the search index.",

0 commit comments

Comments
 (0)