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/route"
  12. "miniflux.app/v2/internal/ui/static"
  13. )
  14. const licensePrefix = "//@license magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt Apache-2.0\n"
  15. const licenseSuffix = "\n//@license-end"
  16. func (h *handler) showJavascript(w http.ResponseWriter, r *http.Request) {
  17. filename := request.RouteStringParam(r, "name")
  18. js, found := static.JavascriptBundles[filename]
  19. if !found {
  20. response.HTMLNotFound(w, r)
  21. return
  22. }
  23. response.NewBuilder(w, r).WithCaching(js.Checksum, 48*time.Hour, func(b *response.Builder) {
  24. contents := js.Data
  25. if filename == "service-worker" {
  26. variables := fmt.Sprintf(`const OFFLINE_URL=%q;`, route.Path(h.router, "offline"))
  27. contents = append([]byte(variables), contents...)
  28. }
  29. // cloning the prefix since `append` mutates its first argument
  30. contents = append([]byte(strings.Clone(licensePrefix)), contents...)
  31. contents = append(contents, []byte(licenseSuffix)...)
  32. b.WithHeader("Content-Type", "text/javascript; charset=utf-8")
  33. b.WithBodyAsBytes(contents)
  34. b.Write()
  35. })
  36. }