Skip to content

Commit 465e59b

Browse files
authored
Redirect on missing ending slash in documentation URLs. (#8339)
1 parent 8d8b198 commit 465e59b

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

app/lib/frontend/handlers/documentation.dart

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,25 @@ Future<shelf.Response> documentationHandler(shelf.Request request) async {
3636
if (docFilePath.version == null) {
3737
return redirectResponse(pkgDocUrl(docFilePath.package, isLatest: true));
3838
}
39-
if (docFilePath.path == null) {
40-
return redirectResponse('${request.requestedUri}/');
39+
final detectedPath = docFilePath.path;
40+
if (detectedPath == null) {
41+
return redirectResponse(
42+
pkgDocUrl(docFilePath.package, version: docFilePath.version));
43+
}
44+
// 8.3.0 dartdoc links to directories without an ending slash.
45+
// This breaks base-uri, sidebar does not load, links do not work.
46+
// Redirecting to the proper directory ending with slash.
47+
if (detectedPath.split('/').last == 'index.html' &&
48+
!request.requestedUri.path.endsWith('/') &&
49+
!request.requestedUri.path.endsWith('/index.html')) {
50+
// removes last segment `index.html` and adds `/` at the end of the url.
51+
return redirectResponse(
52+
pkgDocUrl(
53+
docFilePath.package,
54+
version: docFilePath.version,
55+
relativePath: detectedPath,
56+
),
57+
);
4158
}
4259
final String requestMethod = request.method.toUpperCase();
4360

@@ -59,10 +76,10 @@ Future<shelf.Response> documentationHandler(shelf.Request request) async {
5976
return redirectResponse(pkgDocUrl(
6077
package,
6178
version: resolved.urlSegment,
62-
relativePath: docFilePath.path,
79+
relativePath: detectedPath,
6380
));
6481
} else {
65-
return await handleDartDoc(request, package, resolved, docFilePath.path!);
82+
return await handleDartDoc(request, package, resolved, detectedPath);
6683
}
6784
}
6885

app/test/frontend/handlers/documentation_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void main() {
6060
});
6161

6262
test('version with a path', () {
63+
testUri('/documentation/angular/4.0.0+2/subdir', 'angular', '4.0.0+2',
64+
'subdir/index.html');
6365
testUri('/documentation/angular/4.0.0+2/subdir/', 'angular', '4.0.0+2',
6466
'subdir/index.html');
6567
testUri('/documentation/angular/4.0.0+2/file.html', 'angular', '4.0.0+2',
@@ -98,6 +100,21 @@ void main() {
98100
testWithProfile('trailing slash redirect', fn: () async {
99101
await expectRedirectResponse(await issueGet('/documentation/oxygen'),
100102
'/documentation/oxygen/latest/');
103+
await expectRedirectResponse(await issueGet('/documentation/oxygen/'),
104+
'/documentation/oxygen/latest/');
105+
await expectRedirectResponse(
106+
await issueGet('/documentation/oxygen/latest'),
107+
'/documentation/oxygen/latest/');
108+
await expectRedirectResponse(
109+
await issueGet('/documentation/oxygen/1.0.0'),
110+
'/documentation/oxygen/1.0.0/');
111+
112+
await expectRedirectResponse(
113+
await issueGet('/documentation/oxygen/1.0.0/abc'),
114+
'/documentation/oxygen/1.0.0/abc/');
115+
await expectRedirectResponse(
116+
await issueGet('/documentation/oxygen/latest/abc/def'),
117+
'/documentation/oxygen/latest/abc/def/');
101118
});
102119

103120
testWithProfile(

0 commit comments

Comments
 (0)