Skip to content

Commit a9b1e80

Browse files
authored
[clang-doc] add async loading (#93276)
Fixes #93273 This patch changes the way clang-doc loads html indexes. Previously clang-doc loaded the index via an index_json.js file which uses JSON.parse to parse the file. This patches changes that to use an async function called LoadIndex which enables asynchronous loading making the initial page load not be blocked by loading a large JavaScript object.
1 parent ac84ada commit a9b1e80

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

clang-tools-extra/clang-doc/HTMLGenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,9 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) {
993993
});
994994
});
995995
};
996-
OS << "var JsonIndex = `\n";
996+
OS << "async function LoadIndex() {\nreturn";
997997
IndexToJSON(CDCtx.Idx);
998-
OS << "`;\n";
998+
OS << ";\n}";
999999
return llvm::Error::success();
10001000
}
10011001

clang-tools-extra/clang-doc/assets/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ function createIndex(Index) {
8080

8181
// Runs after DOM loads
8282
document.addEventListener("DOMContentLoaded", function() {
83-
// JsonIndex is a variable from another file that contains the index
84-
// in JSON format
85-
var Index = JSON.parse(JsonIndex);
86-
createIndex(Index);
83+
// LoadIndex is an asynchronous function that will be generated clang-doc.
84+
// It ensures that the function call will not block as soon the page loads,
85+
// since the index object are often huge and can contain thousands of lines.
86+
LoadIndex().then((Index) => { createIndex(Index); });
8787
});

clang-tools-extra/test/clang-doc/basic-project.test

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// RUN: FileCheck %s -input-file=%t/docs/GlobalNamespace/Rectangle.html -check-prefix=HTML-RECTANGLE
88
// RUN: FileCheck %s -input-file=%t/docs/GlobalNamespace/Circle.html -check-prefix=HTML-CIRCLE
99

10-
// JSON-INDEX: var JsonIndex = `
11-
// JSON-INDEX-NEXT: {
10+
// JSON-INDEX: async function LoadIndex() {
11+
// JSON-INDEX-NEXT: return{
1212
// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}",
1313
// JSON-INDEX-NEXT: "Name": "",
1414
// JSON-INDEX-NEXT: "RefType": "default",
@@ -51,7 +51,8 @@
5151
// JSON-INDEX-NEXT: ]
5252
// JSON-INDEX-NEXT: }
5353
// JSON-INDEX-NEXT: ]
54-
// JSON-INDEX-NEXT: }`;
54+
// JSON-INDEX-NEXT: };
55+
// JSON-INDEX-NEXT: }
5556

5657
// HTML-SHAPE: <!DOCTYPE html>
5758
// HTML-SHAPE-NEXT: <meta charset="utf-8"/>

0 commit comments

Comments
 (0)