Skip to content

Commit

Permalink
warn when phx-update="stream" is missing
Browse files Browse the repository at this point in the history
This logs an error to the JS console when debug mode is enabled to warn
users when they forgot to add phx-update="stream" to the direct parent of
an inserted stream item.
  • Loading branch information
SteffenDE committed Jan 20, 2025
1 parent cde2804 commit 36e8d89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/js/phoenix_live_view/dom_patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {

import {
detectDuplicateIds,
detectInvalidStreamInserts,
isCid
} from "./utils"

Expand Down Expand Up @@ -341,6 +342,7 @@ export default class DOMPatch {

if(liveSocket.isDebugEnabled()){
detectDuplicateIds()
detectInvalidStreamInserts(this.streamInserts)
// warn if there are any inputs named "id"
Array.from(document.querySelectorAll("input[name=id]")).forEach(node => {
if(node.form){
Expand Down
11 changes: 11 additions & 0 deletions assets/js/phoenix_live_view/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ export function detectDuplicateIds(){
}
}

export function detectInvalidStreamInserts(inserts){
const errors = new Set()
Object.keys(inserts).forEach((id) => {
const streamEl = document.getElementById(id)
if(streamEl && streamEl.parentElement && streamEl.parentElement.getAttribute("phx-update") !== "stream"){
errors.add(`The stream container with id "${streamEl.parentElement.id}" is missing the phx-update="stream" attribute. Ensure it is set for streams to work properly.`)
}
})
errors.forEach(error => console.error(error))
}

export let debug = (view, kind, msg, obj) => {
if(view.liveSocket.isDebugEnabled()){
console.log(`${view.id} ${kind}: ${msg} - `, obj)
Expand Down

0 comments on commit 36e8d89

Please sign in to comment.