diff --git a/cmd/templ/generatecmd/cmd.go b/cmd/templ/generatecmd/cmd.go index 89ec7662b..aa78840c6 100644 --- a/cmd/templ/generatecmd/cmd.go +++ b/cmd/templ/generatecmd/cmd.go @@ -49,6 +49,10 @@ type GenerationEvent struct { } func (cmd Generate) Run(ctx context.Context) (err error) { + if cmd.Args.NotifyProxy { + proxy.NotifyProxy(cmd.Args.ProxyBind, cmd.Args.ProxyPort) + return nil + } if cmd.Args.Watch && cmd.Args.FileName != "" { return fmt.Errorf("cannot watch a single file, remove the -f or -watch flag") } diff --git a/cmd/templ/generatecmd/main.go b/cmd/templ/generatecmd/main.go index 0f0b781f6..e4e302d54 100644 --- a/cmd/templ/generatecmd/main.go +++ b/cmd/templ/generatecmd/main.go @@ -21,6 +21,7 @@ type Arguments struct { ProxyBind string ProxyPort int Proxy string + NotifyProxy bool WorkerCount int GenerateSourceMapVisualisations bool IncludeVersion bool diff --git a/cmd/templ/generatecmd/proxy/proxy.go b/cmd/templ/generatecmd/proxy/proxy.go index 27667d722..4073eb176 100644 --- a/cmd/templ/generatecmd/proxy/proxy.go +++ b/cmd/templ/generatecmd/proxy/proxy.go @@ -118,8 +118,17 @@ func (p *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } if r.URL.Path == "/_templ/reload/events" { - // Provides a list of messages including a reload message. - p.sse.ServeHTTP(w, r) + switch r.Method { + case http.MethodGet: + // Provides a list of messages including a reload message. + p.sse.ServeHTTP(w, r) + return + case http.MethodPost: + // Send a reload message to all connected clients. + p.sse.Send("message", "reload") + return + } + http.Error(w, "only GET or POST method allowed", http.StatusMethodNotAllowed) return } p.p.ServeHTTP(w, r) @@ -180,3 +189,13 @@ func (rt *roundTripper) RoundTrip(r *http.Request) (*http.Response, error) { return nil, fmt.Errorf("max retries reached") } + +func NotifyProxy(host string, port int) error { + urlStr := fmt.Sprintf("http://%s:%d/_templ/reload/events", host, port) + req, err := http.NewRequest(http.MethodPost, urlStr, nil) + if err != nil { + return err + } + _, err = http.DefaultClient.Do(req) + return err +} diff --git a/cmd/templ/main.go b/cmd/templ/main.go index ff0b0a5fe..0a9cff5d5 100644 --- a/cmd/templ/main.go +++ b/cmd/templ/main.go @@ -91,6 +91,8 @@ Args: The port the proxy will listen on. (default 7331) -proxybind The address the proxy will listen on. (default 127.0.0.1) + -notify-proxy + If present, the command will issue a reload event to the proxy 127.0.0.1:7331, or use proxyport and proxybind to specify a different address. -w Number of workers to use when generating code. (default runtime.NumCPUs) -pprof @@ -134,6 +136,7 @@ func generateCmd(w io.Writer, args []string) (code int) { proxyFlag := cmd.String("proxy", "", "") proxyPortFlag := cmd.Int("proxyport", 7331, "") proxyBindFlag := cmd.String("proxybind", "127.0.0.1", "") + notifyProxyFlag := cmd.Bool("notify-proxy", false, "") workerCountFlag := cmd.Int("w", runtime.NumCPU(), "") pprofPortFlag := cmd.Int("pprof", 0, "") keepOrphanedFilesFlag := cmd.Bool("keep-orphaned-files", false, "") @@ -169,6 +172,7 @@ func generateCmd(w io.Writer, args []string) (code int) { Proxy: *proxyFlag, ProxyPort: *proxyPortFlag, ProxyBind: *proxyBindFlag, + NotifyProxy: *notifyProxyFlag, WorkerCount: *workerCountFlag, GenerateSourceMapVisualisations: *sourceMapVisualisationsFlag, IncludeVersion: *includeVersionFlag,