Skip to content

Commit

Permalink
make sure static assets are cached and not reloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
ddvk committed Nov 17, 2024
1 parent c2bfcf7 commit 736e2eb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ddvk/rmfakecloud/internal/config"
"github.com/ddvk/rmfakecloud/internal/hwr"
"github.com/ddvk/rmfakecloud/internal/storage"

"github.com/ddvk/rmfakecloud/internal/storage/fs"
"github.com/ddvk/rmfakecloud/internal/ui"

Expand Down
41 changes: 41 additions & 0 deletions internal/common/staticfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package common

import (
"fmt"
"net/http"
"os"
"time"
)
type staticFS struct {
http.FileSystem
LastModified time.Time
}
type StaticFileWrapper struct {
http.File
LastModified time.Time
}
type StaticFileInfoWrapper struct {
os.FileInfo
LastModfied time.Time
}
func (f *StaticFileInfoWrapper) ModTime() time.Time {
return f.LastModfied
}
func (f *StaticFileWrapper) Stat() (os.FileInfo, error) {
fmt.Println("calling stat")
fi, err := f.File.Stat()
return &StaticFileInfoWrapper {FileInfo: fi, LastModfied: f.LastModified}, err
}
func (f *staticFS) Open(name string) (http.File, error) {
file, err := f.FileSystem.Open(name)
return &StaticFileWrapper {File: file, LastModified: f.LastModified}, err
}

// NewLastModifiedFS returns a filesystem with a fixed LastModified
// to enable caching of embedded assets
func NewLastModifiedFS(fs http.FileSystem, lastModified time.Time) http.FileSystem {
return &staticFS {
FileSystem: fs,
LastModified: lastModified,
}
}
1 change: 1 addition & 0 deletions internal/ui/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (app *ReactAppWrapper) login(c *gin.Context) {
return
}
log.Debug("cookie expires after: ", expiresAfter)
c.SetSameSite(http.SameSiteStrictMode)
c.SetCookie(cookieName, tokenString, int(expiresAfter.Seconds()), "/", "", app.cfg.HTTPSCookie, true)

c.String(http.StatusOK, tokenString)
Expand Down
3 changes: 0 additions & 3 deletions internal/ui/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ func (app *ReactAppWrapper) RegisterRoutes(router *gin.Engine) {
router.GET("/robots.txt", func(c *gin.Context) {
c.FileFromFS("/robots.txt", app.fs)
})
router.GET("/pdf.worker.js", func(c *gin.Context) {
c.FileFromFS("/pdf.worker.js", app.fs)
})

//hack for index.html
router.NoRoute(func(c *gin.Context) {
Expand Down
5 changes: 4 additions & 1 deletion internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"net/http"
"path"
"time"

"github.com/ddvk/rmfakecloud/internal/app/hub"
"github.com/ddvk/rmfakecloud/internal/common"
Expand Down Expand Up @@ -70,6 +71,8 @@ type ReactAppWrapper struct {
// hack for serving index.html on /
const indexReplacement = "/default"



// New Create a React app
func New(cfg *config.Config,
userStorer storage.UserStorer,
Expand All @@ -91,7 +94,7 @@ func New(cfg *config.Config,
hub: h,
}
staticWrapper := ReactAppWrapper{
fs: http.FS(sub),
fs: common.NewLastModifiedFS(http.FS(sub), time.Now()),
prefix: "/static",
cfg: cfg,
userStorer: userStorer,
Expand Down
8 changes: 6 additions & 2 deletions ui/src/pages/Documents/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import constants from "../../common/constants";
import apiservice from "../../services/api.service"
import NameTag from "../../components/NameTag"

import { pdfjs,Document, Page } from "react-pdf";
import { pdfjs, Document, Page } from "react-pdf";
// import 'react-pdf/dist/Page/AnnotationLayer.css';
// import 'react-pdf/dist/Page/TextLayer.css';
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url,
).toString();

const options = {
worker: new pdfjs.PDFWorker()
}

export default function FileViewer({ file, onSelect }) {
const { data } = file;

Expand Down Expand Up @@ -98,7 +102,7 @@ export default function FileViewer({ file, onSelect }) {

{file && (
<div ref={parent} style={{height: "95%"}}>
<Document file={downloadUrl} onLoadSuccess={onLoadSuccess}>
<Document file={downloadUrl} onLoadSuccess={onLoadSuccess} options={options}>
<Page pageNumber={page}
// width={ width }
height={ height}
Expand Down

0 comments on commit 736e2eb

Please sign in to comment.