Skip to content

Commit

Permalink
✨ Enhance page-reload events with full page updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Feb 26, 2025
1 parent 0f50c88 commit 8194063
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
23 changes: 23 additions & 0 deletions cmd/ui-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,29 @@ func (s *Server) registerComponentRenderers() {
return "", fmt.Errorf("component not found: %s", componentID)
})

// Register page-template renderer
s.sseHandler.RegisterRenderer("page-template", func(pageID string, _ interface{}) (string, error) {
log.Debug().Str("pageID", pageID).Msg("Rendering full page template")

// Get the page definition
s.mu.RLock()
def, ok := s.pages[pageID]
s.mu.RUnlock()

if !ok {
return "", fmt.Errorf("page not found: %s", pageID)
}

// Render the page template
var buf bytes.Buffer
err := pageTemplate(pageID, def).Render(context.Background(), &buf)
if err != nil {
return "", fmt.Errorf("failed to render page template: %w", err)
}

return buf.String(), nil
})

// Register yaml-update renderer
s.sseHandler.RegisterRenderer("yaml-update", func(componentID string, data interface{}) (string, error) {
log.Debug().Str("componentID", componentID).Interface("data", data).Msg("Rendering YAML")
Expand Down
4 changes: 2 additions & 2 deletions examples/pages/cow/cow-quiz.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
components:
- title:
id: quiz-title
content: "🎯 Test Your Cow Knowledge! yo"
content: "🎯 Test Your Cow Knowledge!"

- text:
id: quiz-intro
Expand Down Expand Up @@ -64,4 +64,4 @@ components:
- button:
id: reset-quiz
text: "Start Over"
type: secondary
type: secondary
28 changes: 25 additions & 3 deletions pkg/server/sse/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,31 @@ func (h *SSEHandler) handleComponentUpdate(conn *connection, event events.UIEven

// handlePageReload handles a page reload event
func (h *SSEHandler) handlePageReload(conn *connection, event events.UIEvent) error {
// For page reloads, we might want to send a special event that triggers a full page reload
// or just notify the client that the page should be reloaded
return h.sendRawEvent(conn, "page-reload", "{}")
// First, send a page-reload event notification
if err := h.sendRawEvent(conn, "page-reload", "{}"); err != nil {
return fmt.Errorf("failed to send page-reload event: %w", err)
}

// Then, check if we have a page renderer to render the full page
h.mu.RLock()
pageRenderer, ok := h.renderers["page-template"]
h.mu.RUnlock()

if !ok {
h.logger.Debug().
Str("eventType", "page-template").
Msg("No page renderer registered, skipping full page update")
return nil
}

// Render the full page template
html, err := pageRenderer(conn.pageID, nil)
if err != nil {
return fmt.Errorf("failed to render page template: %w", err)
}

// Send the rendered page as a component-update event
return h.sendRawEvent(conn, "component-update", html)
}

// handleYamlUpdate handles a YAML update event
Expand Down
2 changes: 1 addition & 1 deletion ttmp/2025-02-22/02-ui-server-sse-refactor.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ This plan outlines the refactoring of the SSE/Watermill functionality in the UI
## 5. Implementation Steps

### 5.1 Event System Implementation
- [ ] Create base event types and interfaces
- [ ] Create base event types and interfacees
- [ ] Define UIEvent struct with proper JSON tags
- [ ] Define EventManager interface with documentation
- [ ] Add helper methods for event creation
Expand Down
3 changes: 2 additions & 1 deletion ttmp/2025-02-22/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Updated the SSE implementation to properly support the htmx SSE extension API. T
- Implemented proper event formatting according to htmx SSE extension requirements
- Added support for different event types (component-update, page-reload, yaml-update)
- Added ping events to keep connections alive
- Registered component renderers in the server
- Registered component renderers in the server
- Enhanced page-reload events to also send a full page update for seamless refreshes

0 comments on commit 8194063

Please sign in to comment.