static_javascript.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package ui // import "miniflux.app/ui"
  5. import (
  6. "fmt"
  7. "net/http"
  8. "time"
  9. "miniflux.app/http/request"
  10. "miniflux.app/http/response"
  11. "miniflux.app/http/response/html"
  12. "miniflux.app/http/route"
  13. "miniflux.app/ui/static"
  14. )
  15. func (h *handler) showJavascript(w http.ResponseWriter, r *http.Request) {
  16. filename := request.RouteStringParam(r, "name")
  17. etag, found := static.JavascriptBundleChecksums[filename]
  18. if !found {
  19. html.NotFound(w, r)
  20. return
  21. }
  22. response.New(w, r).WithCaching(etag, 48*time.Hour, func(b *response.Builder) {
  23. contents := static.JavascriptBundles[filename]
  24. if filename == "service-worker" {
  25. variables := fmt.Sprintf(`const OFFLINE_URL="%s";`, route.Path(h.router, "offline"))
  26. contents = append([]byte(variables)[:], contents[:]...)
  27. }
  28. b.WithHeader("Content-Type", "text/javascript; charset=utf-8")
  29. b.WithBody(contents)
  30. b.Write()
  31. })
  32. }