Skip to content

Commit

Permalink
internal/frontend: show a page for an empty directory
Browse files Browse the repository at this point in the history
Currently, it returns 404 for an empty directory.
However if the directory has nested modules, it would be useful
to show a page containing them.
So this change is to redirect requests for an empty directory
that is above nested modules to the search result page.

Fixes golang/go#43725

Change-Id: Icb0aae8c857f44931a999a74d45b29aa12a0c56c
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/290049
Reviewed-by: Julie Qiu <julie@golang.org>
Trust: Jonathan Amsterdam <jba@google.com>
Trust: Julie Qiu <julie@golang.org>
  • Loading branch information
ya5u authored and jba committed Feb 12, 2021
1 parent 56e2739 commit 1763954
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/frontend/404.go
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"html"
"net/http"
"net/url"
"regexp"
"strings"

Expand Down Expand Up @@ -69,6 +70,13 @@ func (s *Server) servePathNotFoundPage(w http.ResponseWriter, r *http.Request,
if !errors.Is(err, derrors.NotFound) {
log.Error(ctx, err)
}
// Redirect to the search result page for an empty directory that is above nested modules.
// For golang/go#43725
nm, err := ds.GetNestedModules(ctx, fullPath)
if err == nil && len(nm) > 0 {
http.Redirect(w, r, "/search?q="+url.QueryEscape(fullPath), http.StatusFound)
return nil
}
return pathNotFoundError(fullPath, requestedVersion)
}
switch fr.status {
Expand All @@ -84,6 +92,13 @@ func (s *Server) servePathNotFoundPage(w http.ResponseWriter, r *http.Request,
http.Redirect(w, r, u, http.StatusFound)
return
}
// Redirect to the search result page for an empty directory that is above nested modules.
// For golang/go#43725
nm, err := ds.GetNestedModules(ctx, fullPath)
if err == nil && len(nm) > 0 {
http.Redirect(w, r, "/search?q="+url.QueryEscape(fullPath), http.StatusFound)
return nil
}
return &serverError{
status: fr.status,
epage: &errorPage{
Expand Down

0 comments on commit 1763954

Please sign in to comment.