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

Redirect on missing ending slash in documentation URLs. #8339

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 21 additions & 4 deletions app/lib/frontend/handlers/documentation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,25 @@ Future<shelf.Response> documentationHandler(shelf.Request request) async {
if (docFilePath.version == null) {
return redirectResponse(pkgDocUrl(docFilePath.package, isLatest: true));
}
if (docFilePath.path == null) {
return redirectResponse('${request.requestedUri}/');
final detectedPath = docFilePath.path;
if (detectedPath == null) {
return redirectResponse(
pkgDocUrl(docFilePath.package, version: docFilePath.version));
}
// 8.3.0 dartdoc links to directories without an ending slash.
// This breaks base-uri, sidebar does not load, links do not work.
// Redirecting to the proper directory ending with slash.
if (detectedPath.split('/').last == 'index.html' &&
!request.requestedUri.path.endsWith('/') &&
!request.requestedUri.path.endsWith('/index.html')) {
// removes last segment `index.html` and adds `/` at the end of the url.
return redirectResponse(
pkgDocUrl(
docFilePath.package,
version: docFilePath.version,
relativePath: detectedPath,
),
);
}
final String requestMethod = request.method.toUpperCase();

Expand All @@ -59,10 +76,10 @@ Future<shelf.Response> documentationHandler(shelf.Request request) async {
return redirectResponse(pkgDocUrl(
package,
version: resolved.urlSegment,
relativePath: docFilePath.path,
relativePath: detectedPath,
));
} else {
return await handleDartDoc(request, package, resolved, docFilePath.path!);
return await handleDartDoc(request, package, resolved, detectedPath);
}
}

Expand Down
17 changes: 17 additions & 0 deletions app/test/frontend/handlers/documentation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void main() {
});

test('version with a path', () {
testUri('/documentation/angular/4.0.0+2/subdir', 'angular', '4.0.0+2',
'subdir/index.html');
testUri('/documentation/angular/4.0.0+2/subdir/', 'angular', '4.0.0+2',
'subdir/index.html');
testUri('/documentation/angular/4.0.0+2/file.html', 'angular', '4.0.0+2',
Expand Down Expand Up @@ -98,6 +100,21 @@ void main() {
testWithProfile('trailing slash redirect', fn: () async {
await expectRedirectResponse(await issueGet('/documentation/oxygen'),
'/documentation/oxygen/latest/');
await expectRedirectResponse(await issueGet('/documentation/oxygen/'),
'/documentation/oxygen/latest/');
await expectRedirectResponse(
await issueGet('/documentation/oxygen/latest'),
'/documentation/oxygen/latest/');
await expectRedirectResponse(
await issueGet('/documentation/oxygen/1.0.0'),
'/documentation/oxygen/1.0.0/');

await expectRedirectResponse(
await issueGet('/documentation/oxygen/1.0.0/abc'),
'/documentation/oxygen/1.0.0/abc/');
await expectRedirectResponse(
await issueGet('/documentation/oxygen/latest/abc/def'),
'/documentation/oxygen/latest/abc/def/');
});

testWithProfile(
Expand Down