static_javascript.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. "time"
  8. "miniflux.app/v2/internal/http/request"
  9. "miniflux.app/v2/internal/http/response"
  10. "miniflux.app/v2/internal/http/response/html"
  11. "miniflux.app/v2/internal/http/route"
  12. "miniflux.app/v2/internal/ui/static"
  13. )
  14. func (h *handler) showJavascript(w http.ResponseWriter, r *http.Request) {
  15. filename := request.RouteStringParam(r, "name")
  16. etag, found := static.JavascriptBundleChecksums[filename]
  17. if !found {
  18. html.NotFound(w, r)
  19. return
  20. }
  21. response.New(w, r).WithCaching(etag, 48*time.Hour, func(b *response.Builder) {
  22. contents := static.JavascriptBundles[filename]
  23. if filename == "service-worker" {
  24. variables := fmt.Sprintf(`const OFFLINE_URL=%q;`, route.Path(h.router, "offline"))
  25. contents = append([]byte(variables), contents...)
  26. }
  27. b.WithHeader("Content-Type", "text/javascript; charset=utf-8")
  28. b.WithBody(contents)
  29. b.Write()
  30. })
  31. }