Skip to content

Commit 274f418

Browse files
committed
internal/frontend: add a go doc mode to suppress some UI elements
This change adds an option to the server to suppress the unit header details and the right hand side unit metadata that's shown on pkgsite, when it's operating in go doc mode. In this go doc mode, it will also not fetch the deps.dev url or vulnerability information, so that pkgsite doesn't make unnecessary network calls. The behavior to allow no modules to match the pattern has also been folded into go doc mode. I think we may also want to make some CSS changes too but I think those would belong in another CL. Change-Id: I8b6ebb5594f73684d2db41a84e1b6d46ee95d774 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/675957 kokoro-CI: kokoro <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 0e6de17 commit 274f418

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

cmd/internal/pkgsite/server.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ServerConfig struct {
4141
DevMode bool
4242
DevModeStaticDir string
4343
GoRepoPath string
44-
AllowNoModules bool
44+
GoDocMode bool
4545

4646
Proxy *proxy.Client // client, or nil; controlled by the -proxy flag
4747
}
@@ -69,7 +69,7 @@ func BuildServer(ctx context.Context, serverCfg ServerConfig) (*frontend.Server,
6969
}
7070
} else {
7171
var err error
72-
cfg.dirs, err = getModuleDirs(ctx, serverCfg.Paths, serverCfg.AllowNoModules)
72+
cfg.dirs, err = getModuleDirs(ctx, serverCfg.Paths, serverCfg.GoDocMode)
7373
if err != nil {
7474
return nil, fmt.Errorf("searching modules: %v", err)
7575
}
@@ -114,7 +114,7 @@ func BuildServer(ctx context.Context, serverCfg ServerConfig) (*frontend.Server,
114114
return allModules[i].ModulePath < allModules[j].ModulePath
115115
})
116116

117-
return newServer(getters, allModules, cfg.proxy, serverCfg.DevMode, serverCfg.DevModeStaticDir)
117+
return newServer(getters, allModules, cfg.proxy, serverCfg.GoDocMode, serverCfg.DevMode, serverCfg.DevModeStaticDir)
118118
}
119119

120120
// getModuleDirs returns the set of workspace modules for each directory,
@@ -265,7 +265,7 @@ func buildGetters(ctx context.Context, cfg getterConfig) ([]fetch.ModuleGetter,
265265
return getters, nil
266266
}
267267

268-
func newServer(getters []fetch.ModuleGetter, localModules []frontend.LocalModule, prox *proxy.Client, devMode bool, staticFlag string) (*frontend.Server, error) {
268+
func newServer(getters []fetch.ModuleGetter, localModules []frontend.LocalModule, prox *proxy.Client, goDocMode bool, devMode bool, staticFlag string) (*frontend.Server, error) {
269269
lds := fetchdatasource.Options{
270270
Getters: getters,
271271
ProxyClientForLatest: prox,
@@ -292,6 +292,7 @@ func newServer(getters []fetch.ModuleGetter, localModules []frontend.LocalModule
292292
TemplateFS: template.TrustedFSFromEmbed(static.FS),
293293
StaticFS: staticFS,
294294
DevMode: devMode,
295+
GoDocMode: goDocMode,
295296
LocalMode: true,
296297
LocalModules: localModules,
297298
ThirdPartyFS: thirdparty.FS,

internal/frontend/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Server struct {
4848
staticFS fs.FS
4949
thirdPartyFS fs.FS
5050
devMode bool
51+
goDocMode bool // running to serve documentation for 'go doc'
5152
localMode bool // running locally (i.e. ./cmd/pkgsite)
5253
localModules []LocalModule // locally hosted modules; empty in production
5354
errorPage []byte
@@ -90,6 +91,7 @@ type ServerConfig struct {
9091
ThirdPartyFS fs.FS // for third_party/ directory
9192
DevMode bool
9293
LocalMode bool
94+
GoDocMode bool
9395
LocalModules []LocalModule
9496
Reporter derrors.Reporter
9597
VulndbClient *vuln.Client
@@ -113,6 +115,7 @@ func NewServer(scfg ServerConfig) (_ *Server, err error) {
113115
thirdPartyFS: scfg.ThirdPartyFS,
114116
devMode: scfg.DevMode,
115117
localMode: scfg.LocalMode,
118+
goDocMode: scfg.GoDocMode,
116119
localModules: scfg.LocalModules,
117120
templates: ts,
118121
reporter: scfg.Reporter,

internal/frontend/unit.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ type UnitPage struct {
9696
// Details contains data specific to the type of page being rendered.
9797
Details any
9898

99+
// GoDocMode indicates whether to suppress the unit header and right hand unit metadata.
100+
// If set to true, the page will also not have Vulns or a DepsDevURL.
101+
GoDocMode bool
102+
99103
// Vulns holds vulnerability information.
100104
Vulns []vuln.Vuln
101105

@@ -119,7 +123,7 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
119123
tab = tabMain
120124
}
121125
// Redirect to clean URL path when tab param is invalid.
122-
if _, ok := unitTabLookup[tab]; !ok {
126+
if _, ok := unitTabLookup[tab]; !ok || (s.goDocMode && tab != tabMain) {
123127
http.Redirect(w, r, r.URL.Path, http.StatusFound)
124128
return nil
125129
}
@@ -136,7 +140,10 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
136140
return s.fetchServer.ServePathNotFoundPage(w, r, db, info.FullPath, info.ModulePath, info.RequestedVersion)
137141
}
138142

139-
makeDepsDevURL := depsDevURLGenerator(ctx, s.depsDevHTTPClient, um)
143+
makeDepsDevURL := func() string { return "" }
144+
if !s.goDocMode {
145+
makeDepsDevURL = depsDevURLGenerator(ctx, s.depsDevHTTPClient, um)
146+
}
140147

141148
// Use GOOS and GOARCH query parameters to create a build context, which
142149
// affects the documentation and synopsis. Omitting both results in an empty
@@ -148,7 +155,7 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
148155
if err != nil {
149156
return err
150157
}
151-
if s.shouldServeJSON(r) {
158+
if s.shouldServeJSON(r) && !s.goDocMode {
152159
return s.serveJSONPage(w, r, d)
153160
}
154161

@@ -225,9 +232,13 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
225232
PageLabels: pageLabels(um),
226233
PageType: pageType(um),
227234
RedirectedFromPath: redirectPath,
228-
DepsDevURL: makeDepsDevURL(),
229235
IsGoProject: isGoProject(um.ModulePath),
230236
IsLatestMinor: lv == latestInfo.MinorVersion,
237+
GoDocMode: s.goDocMode,
238+
}
239+
240+
if !s.goDocMode {
241+
page.DepsDevURL = makeDepsDevURL()
231242
}
232243

233244
// Show the banner if there was no error getting the latest major version,
@@ -243,8 +254,10 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
243254
page.MetaDescription = metaDescription(main.DocSynopsis)
244255
}
245256

246-
// Get vulnerability information.
247-
page.Vulns = vuln.VulnsForPackage(ctx, um.ModulePath, um.Version, um.Path, s.vulnClient)
257+
if !s.goDocMode {
258+
// Get vulnerability information.
259+
page.Vulns = vuln.VulnsForPackage(ctx, um.ModulePath, um.Version, um.Path, s.vulnClient)
260+
}
248261

249262
s.servePage(ctx, w, tabSettings.TemplateName, page)
250263
return nil

static/frontend/unit/_header.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
{{template "unit-header-breadcrumbs" .}}
1111
<div class="go-Main-headerContent">
1212
{{template "unit-header-title" .}}
13-
{{template "unit-header-details" .}}
13+
{{if not .GoDocMode}}
14+
{{template "unit-header-details" .}}
15+
{{end}}
1416
</div>
1517
{{end}}
1618

static/frontend/unit/unit.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
<header class="go-Main-header js-mainHeader">
2424
{{- block "main-header" .}}{{end -}}
2525
</header>
26-
<aside class="go-Main-aside {{if not (eq .SelectedTab.Name "")}}go-Main-aside--empty{{end}} js-mainAside">
27-
{{- block "main-aside" .}}{{end -}}
28-
</aside>
26+
{{if not .GoDocMode}}
27+
<aside class="go-Main-aside {{if not (eq .SelectedTab.Name "")}}go-Main-aside--empty{{end}} js-mainAside">
28+
{{- block "main-aside" .}}{{end -}}
29+
</aside>
30+
{{end}}
2931
<nav class="go-Main-nav go-Main-nav--sticky js-mainNav" aria-label="Outline">
3032
{{- block "main-nav" .}}{{end -}}
3133
</nav>

0 commit comments

Comments
 (0)