Skip to content

Commit

Permalink
Added -forceReload flag which forces page reload on any file change
Browse files Browse the repository at this point in the history
I'm developing a blog which is using fetch to lazy load html pages. So
currently it doesnt refresh whenever the lazy loaded html is changed
since origins html page won't ever be the same.

So this is a quick and dirty way of solving the problem, might also
solve some similar problems down the line.
  • Loading branch information
baalimago committed Aug 23, 2024
1 parent e031aad commit 225eabf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
16 changes: 9 additions & 7 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ type Fileserver interface {
type command struct {
binPath string
// master, as in adjective 'master record' non-slavery kind
masterPath string
mirrorPath string
port *int
wsPath *string
flagset *flag.FlagSet
fileserver Fileserver
masterPath string
mirrorPath string
port *int
wsPath *string
forceReload *bool
flagset *flag.FlagSet
fileserver Fileserver
}

func Command() *command {
Expand All @@ -52,7 +53,7 @@ func (c *command) Setup() error {
c.masterPath = path.Clean(relPath)

if c.masterPath != "" {
c.fileserver = wsinject.NewFileServer(*c.port, *c.wsPath)
c.fileserver = wsinject.NewFileServer(*c.port, *c.wsPath, *c.forceReload)
mirrorPath, err := c.fileserver.Setup(c.masterPath)
if err != nil {
return fmt.Errorf("failed to setup websocket injected mirror filesystem: %v", err)
Expand Down Expand Up @@ -121,6 +122,7 @@ func (c *command) Flagset() *flag.FlagSet {
fs := flag.NewFlagSet("server", flag.ExitOnError)
c.port = fs.Int("port", 8080, "port to serve http server on")
c.wsPath = fs.String("wsPort", "/delta-streamer-ws", "the path which the delta streamer websocket should be hosted on")
c.forceReload = fs.Bool("forceReload", false, "set to true if you wish to reload all attached browser pages on any file change")
c.flagset = fs
return fs
}
8 changes: 6 additions & 2 deletions internal/wsinject/delta_streamer.ws.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package wsinject

const DeltaStreamerSourceCode = `/**
const deltaStreamerSourceCode = `/**
* This file has been injected by the wd-41 web development
* hot reload tool.
*/
Expand Down Expand Up @@ -33,7 +33,11 @@ function startWebsocket() {
if(event.data === fileName ||
// Always reload on js and css files since its difficult to know where these are used
event.data.includes(".js") ||
event.data.includes(".css")) {
event.data.includes(".css") ||
// This funny-looking comparison is set using string interpolation from the -forceReload flag
// when writing this script
%v === true
) {
location.reload();
}
});
Expand Down
19 changes: 12 additions & 7 deletions internal/wsinject/wsinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
)

type Fileserver struct {
masterPath string
mirrorPath string
wsPort int
wsPath string
watcher *fsnotify.Watcher
masterPath string
mirrorPath string
forceReload bool
wsPort int
wsPath string
watcher *fsnotify.Watcher

pageReloadChan chan string
wsDispatcher sync.Map
Expand All @@ -36,7 +37,7 @@ var ErrNoHeaderTagFound = errors.New("no header tag found")
const deltaStreamer = `<!-- This script has been injected by wd-41 and allows hot reloads -->
<script type="module" src="delta-streamer.js"></script>`

func NewFileServer(wsPort int, wsPath string) *Fileserver {
func NewFileServer(wsPort int, wsPath string, forceReload bool) *Fileserver {
mirrorDir, err := os.MkdirTemp("", "wd-41_*")
if err != nil {
panic(err)
Expand All @@ -46,6 +47,7 @@ func NewFileServer(wsPort int, wsPath string) *Fileserver {
mirrorPath: mirrorDir,
wsPort: wsPort,
wsPath: wsPath,
forceReload: forceReload,
pageReloadChan: make(chan string),
wsDispatcher: sync.Map{},
wsDispatcherStarted: &started,
Expand Down Expand Up @@ -95,7 +97,10 @@ func (fs *Fileserver) mirrorMaker(p string, info os.DirEntry, err error) error {
}

func (fs *Fileserver) writeDeltaStreamerScript() error {
err := os.WriteFile(path.Join(fs.mirrorPath, "delta-streamer.js"), []byte(fmt.Sprintf(DeltaStreamerSourceCode, fs.wsPort, fs.wsPath)), 0o755)
err := os.WriteFile(
path.Join(fs.mirrorPath, "delta-streamer.js"),
[]byte(fmt.Sprintf(deltaStreamerSourceCode, fs.wsPort, fs.wsPath, fs.forceReload)),
0o755)
if err != nil {
return fmt.Errorf("failed to write delta-streamer.js: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/wsinject/wsinject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Test_Setup(t *testing.T) {
}
nestedFile := path.Join(nestedDir, "nested.html")
os.WriteFile(nestedFile, []byte(mockHtml), 0o777)
fs := NewFileServer(8080, "/delta-streamer-ws.js")
fs := NewFileServer(8080, "/delta-streamer-ws.js", false)
_, err = fs.Setup(tmpDir)
if err != nil {
t.Fatalf("failed to setup: %v", err)
Expand Down Expand Up @@ -145,7 +145,7 @@ func Test_Start(t *testing.T) {
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
return NewFileServer(8080, "/delta-streamer-ws.js"), testFileSystem{
return NewFileServer(8080, "/delta-streamer-ws.js", false), testFileSystem{
root: tmpDir,
nestedDir: nestedDir,
}
Expand Down

0 comments on commit 225eabf

Please sign in to comment.