static_javascript.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "miniflux.app/v2/internal/http/request"
  10. "miniflux.app/v2/internal/http/response"
  11. "miniflux.app/v2/internal/http/response/html"
  12. "miniflux.app/v2/internal/http/route"
  13. "miniflux.app/v2/internal/ui/static"
  14. )
  15. const licensePrefix = "//@license magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt Apache-2.0\n"
  16. const licenseSuffix = "\n//@license-end"
  17. func (h *handler) showJavascript(w http.ResponseWriter, r *http.Request) {
  18. filename := request.RouteStringParam(r, "name")
  19. js, found := static.JavascriptBundles[filename]
  20. if !found {
  21. html.NotFound(w, r)
  22. return
  23. }
  24. response.New(w, r).WithCaching(js.Checksum, 48*time.Hour, func(b *response.Builder) {
  25. contents := js.Data
  26. if filename == "service-worker" {
  27. variables := fmt.Sprintf(`const OFFLINE_URL=%q;`, route.Path(h.router, "offline"))
  28. contents = append([]byte(variables), contents...)
  29. }
  30. // cloning the prefix since `append` mutates its first argument
  31. contents = append([]byte(strings.Clone(licensePrefix)), contents...)
  32. contents = append(contents, []byte(licenseSuffix)...)
  33. b.WithHeader("Content-Type", "text/javascript; charset=utf-8")
  34. b.WithBody(contents)
  35. b.Write()
  36. })
  37. }