static_javascript.go 1.5 KB

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