Skip to content

Commit 7a9df65

Browse files
authored
Add fallback text for sidebar failing to load (#3643)
* Add fallback text for sidebar failing to load * Update for specific troubleshoot fragment
1 parent 9bcabb5 commit 7a9df65

11 files changed

+3217
-3231
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 8.0.6-wip
2+
3+
* Add troubleshooting information when the sidebars failed to load.
4+
15
## 8.0.5
26

37
* Require `analyzer: ^6.4.1`.

dartdoc_options.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dartdoc:
22
linkToSource:
33
root: '.'
4-
uriTemplate: 'https://github.com/dart-lang/dartdoc/blob/v8.0.5/%f%#L%l%'
4+
uriTemplate: 'https://github.com/dart-lang/dartdoc/blob/v8.0.6-wip/%f%#L%l%'

lib/resources/docs.dart.js

+3,154-3,181
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/resources/docs.dart.js.map

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/version.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const packageVersion = '8.0.5';
1+
const packageVersion = '8.0.6-wip';

pubspec.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: dartdoc
2-
version: 8.0.5
2+
version: 8.0.6-wip
33
description: A non-interactive HTML documentation generator for Dart source code.
44
repository: https://github.com/dart-lang/dartdoc
55

@@ -25,7 +25,6 @@ dependencies:
2525
dev_dependencies:
2626
async: ^2.11.0
2727
dart_style: ^2.3.4
28-
js: ^0.7.0
2928
lints: ^3.0.0
3029
matcher: ^0.12.15
3130
test: ^1.24.2

web/docs.dart

+39-23
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,72 @@ import 'highlight.dart' as highlight;
88
import 'search.dart' as search;
99
import 'sidenav.dart' as sidenav;
1010
import 'theme.dart' as theme;
11+
import 'web_interop.dart';
1112

1213
void main() {
13-
highlight.init();
1414
initializeSidebars();
15-
sidenav.init();
1615
search.init();
16+
sidenav.init();
17+
highlight.init();
1718
theme.init();
1819
}
1920

2021
void initializeSidebars() {
21-
final body = document.querySelector('body');
22+
final body = document.body;
2223
if (body == null) {
2324
return;
2425
}
25-
final dataUsingBaseHref = body.dataset['using-base-href'];
26+
final dataUsingBaseHref = body.getAttribute('data-using-base-href');
2627
if (dataUsingBaseHref == null) {
2728
// This should never happen.
2829
return;
2930
}
30-
var baseHref = '';
31+
final String baseHref;
3132
if (dataUsingBaseHref != 'true') {
32-
final dataBaseHref = body.dataset['base-href'];
33+
final dataBaseHref = body.getAttribute('data-base-href');
3334
if (dataBaseHref == null) {
3435
return;
3536
}
3637
baseHref = dataBaseHref;
38+
} else {
39+
baseHref = '';
3740
}
38-
final mainContent = document.querySelector('#dartdoc-main-content');
41+
42+
final mainContent = document.getElementById('dartdoc-main-content');
3943
if (mainContent == null) {
4044
return;
4145
}
42-
final aboveSidebarPath = mainContent.dataset['above-sidebar'];
43-
final leftSidebar = document.querySelector('#dartdoc-sidebar-left-content');
4446
final sanitizer = _SidebarNodeTreeSanitizer(baseHref);
45-
if (aboveSidebarPath != null &&
46-
aboveSidebarPath.isNotEmpty &&
47-
leftSidebar != null) {
48-
HttpRequest.getString('$baseHref$aboveSidebarPath').then((content) {
49-
leftSidebar.setInnerHtml(content, treeSanitizer: sanitizer);
50-
});
51-
}
52-
final belowSidebarPath = mainContent.dataset['below-sidebar'];
53-
final rightSidebar = document.querySelector('#dartdoc-sidebar-right');
54-
if (belowSidebarPath != null &&
55-
belowSidebarPath.isNotEmpty &&
56-
rightSidebar != null) {
57-
HttpRequest.getString('$baseHref$belowSidebarPath').then((content) {
58-
rightSidebar.setInnerHtml(content, treeSanitizer: sanitizer);
47+
48+
void loadSidebar(String? sidebarPath, Element? sidebarElement) {
49+
if (sidebarPath == null || sidebarPath.isEmpty || sidebarElement == null) {
50+
return;
51+
}
52+
53+
window.fetch('$baseHref$sidebarPath').then((response) async {
54+
final fetchResponse = response as FetchResponse;
55+
if (response.status != 200) {
56+
final errorAnchor = (document.createElement('a') as AnchorElement)
57+
..href = 'https://dart.dev/tools/dart-doc#troubleshoot'
58+
..text = 'Failed to load sidebar. '
59+
'Visit dart.dev for help troubleshooting.';
60+
sidebarElement.append(errorAnchor);
61+
return;
62+
}
63+
64+
final content = await fetchResponse.text;
65+
66+
sidebarElement.setInnerHtml(content, treeSanitizer: sanitizer);
5967
});
6068
}
69+
70+
final aboveSidebarPath = mainContent.getAttribute('data-above-sidebar');
71+
final leftSidebar = document.getElementById('dartdoc-sidebar-left-content');
72+
loadSidebar(aboveSidebarPath, leftSidebar);
73+
74+
final belowSidebarPath = mainContent.getAttribute('data-below-sidebar');
75+
final rightSidebar = document.getElementById('dartdoc-sidebar-right');
76+
loadSidebar(belowSidebarPath, rightSidebar);
6177
}
6278

6379
/// A permissive sanitizer that allows external links (e.g. to api.dart.dev) and

web/highlight.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:js/js.dart';
5+
import 'dart:js_interop';
66

77
void init() {
88
highlight?.highlightAll();
99
}
1010

1111
@JS()
1212
@staticInterop
13-
class HighlightJs {}
13+
final class HighlightJs {}
1414

1515
extension HighlightJsExtension on HighlightJs {
1616
external void highlightAll();

web/search.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ void init() {
3636
}
3737

3838
window.fetch('${_htmlBase}index.json').then((response) async {
39-
response = response as FetchResponse;
40-
var code = response.status;
41-
if (code == 404) {
39+
final fetchResponse = response as FetchResponse;
40+
if (fetchResponse.status != 200) {
4241
disableSearch();
4342
return;
4443
}
4544

46-
var text = await response.text;
45+
final text = await fetchResponse.text;
4746
final index = Index.fromJson(text);
4847

4948
// Navigate to the first result from the 'search' query parameter

web/sig.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8C67B39F09940FC20D8C13F6BBE0B1EE
1+
157D433EE554B00A24CAF73333958091

web/web_interop.dart

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:js_util' as js_util;
6-
7-
import 'package:js/js.dart';
5+
import 'dart:js_interop';
86

97
@JS('Response')
108
@staticInterop
11-
class FetchResponse {}
9+
final class FetchResponse {}
1210

1311
extension FetchResponseExtension on FetchResponse {
1412
external int get status;
1513

1614
@JS('text')
17-
external Promise _text();
18-
Future<String> get text => js_util.promiseToFuture(_text());
15+
external JSPromise _text();
16+
Future<String> get text async =>
17+
((await _text().toDart) as JSString).toString();
1918
}
20-
21-
@JS()
22-
@staticInterop
23-
class Promise {}

0 commit comments

Comments
 (0)