diff --git a/middleware/browse/browse.go b/middleware/browse/browse.go
index a86c5f02..5a8c229b 100644
--- a/middleware/browse/browse.go
+++ b/middleware/browse/browse.go
@@ -242,24 +242,31 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
listing.Sort, listing.Order = r.URL.Query().Get("sort"), r.URL.Query().Get("order")
// If the query 'sort' or 'order' is empty, check the cookies
- if listing.Sort == "" || listing.Order == "" {
+ if listing.Sort == "" {
sortCookie, sortErr := r.Cookie("sort")
- orderCookie, orderErr := r.Cookie("order")
-
// if there's no sorting values in the cookies, default to "name" and "asc"
- if sortErr != nil || orderErr != nil {
+ if sortErr != nil {
listing.Sort = "name"
- listing.Order = "asc"
} else { // if we have values in the cookies, use them
listing.Sort = sortCookie.Value
- listing.Order = orderCookie.Value
}
-
} else { // save the query value of 'sort' and 'order' as cookies
http.SetCookie(w, &http.Cookie{Name: "sort", Value: listing.Sort, Path: "/"})
http.SetCookie(w, &http.Cookie{Name: "order", Value: listing.Order, Path: "/"})
}
+ if listing.Order == "" {
+ orderCookie, orderErr := r.Cookie("order")
+ // if there's no sorting values in the cookies, default to "name" and "asc"
+ if orderErr != nil {
+ listing.Order = "asc"
+ } else { // if we have values in the cookies, use them
+ listing.Order = orderCookie.Value
+ }
+ } else { // save the query value of 'sort' and 'order' as cookies
+ http.SetCookie(w, &http.Cookie{Name: "order", Value: listing.Order, Path: "/"})
+ }
+
// Apply the sorting
listing.applySort()
diff --git a/middleware/browse/browse_test.go b/middleware/browse/browse_test.go
index 05ccff14..1e461bea 100644
--- a/middleware/browse/browse_test.go
+++ b/middleware/browse/browse_test.go
@@ -129,9 +129,9 @@ func TestBrowseTemplate(t *testing.T) {
rec := httptest.NewRecorder()
- b.ServeHTTP(rec, req)
- if rec.Code != http.StatusOK {
- t.Fatalf("Wrong status, expected %d, got %d", http.StatusOK, rec.Code)
+ code, err := b.ServeHTTP(rec, req)
+ if code != http.StatusOK {
+ t.Fatalf("Wrong status, expected %d, got %d", http.StatusOK, code)
}
respBody := rec.Body.String()
@@ -149,6 +149,8 @@ func TestBrowseTemplate(t *testing.T) {
test2.html
+test3.html
+