Skip to content

Commit

Permalink
fix: Variable redeclaration in injected js
Browse files Browse the repository at this point in the history
Wrap the injected script into an immediately invoked
function expression to avoid variables being redeclared.
  • Loading branch information
romshark committed Jul 8, 2024
1 parent b0bfb9a commit be01249
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 85 deletions.
168 changes: 84 additions & 84 deletions internal/server/templates.templ
Original file line number Diff line number Diff line change
Expand Up @@ -62,105 +62,105 @@ templ jsInjection(printDebugLogs bool, wsEventsEndpoint string) {
WSEventsEndpoint: wsEventsEndpoint,
})
<script type="text/javascript">
// This script is injected by the templier proxy for automatic reload
const params = JSON.parse(
document.getElementById('_templier__jsInjection').textContent
)
(() => {
let params = JSON.parse(
document.getElementById('_templier__jsInjection').textContent
)

let reloadIndicator
function showLoadingIndicator() {
if (reloadIndicator != null) {
return
let reloadIndicator
function showLoadingIndicator() {
if (reloadIndicator != null) {
return
}
displayingLoadingIndicator = true
reloadIndicator = document.createElement('p')
reloadIndicator.innerHTML = 'Reloading...'
reloadIndicator.style.top = 0
reloadIndicator.style.left = 0
reloadIndicator.style.width = '100%'
reloadIndicator.style.padding = '.25rem'
reloadIndicator.style.position = 'fixed'
reloadIndicator.style.background = 'rgba(0,0,0,.75)'
reloadIndicator.style.color = 'white'
document.body.appendChild(reloadIndicator)
}
displayingLoadingIndicator = true
reloadIndicator = document.createElement('p')
reloadIndicator.innerHTML = 'Reloading...'
reloadIndicator.style.top = 0
reloadIndicator.style.left = 0
reloadIndicator.style.width = '100%'
reloadIndicator.style.padding = '.25rem'
reloadIndicator.style.position = 'fixed'
reloadIndicator.style.background = 'rgba(0,0,0,.75)'
reloadIndicator.style.color = 'white'
document.body.appendChild(reloadIndicator)
}
function hideLoadingIndicator() {
if (reloadIndicator == null) {
return
function hideLoadingIndicator() {
if (reloadIndicator == null) {
return
}
reloadIndicator.remove()
reloadIndicator = null
}
reloadIndicator.remove()
reloadIndicator = null
}

let reconnectingOverlay
function showReconnecting() {
if (reconnectingOverlay != null) {
return
let reconnectingOverlay
function showReconnecting() {
if (reconnectingOverlay != null) {
return
}
reconnectingOverlay = document.createElement('p')
reconnectingOverlay.innerHTML = '🔌 reconnecting Templier...'
reconnectingOverlay.style.position = 'fixed'
reconnectingOverlay.style.top = 0
reconnectingOverlay.style.left = 0
reconnectingOverlay.style.fontSize = '1.25rem'
reconnectingOverlay.style.width = '100%'
reconnectingOverlay.style.height = '100%'
reconnectingOverlay.style.padding = '2rem'
reconnectingOverlay.style.textAlign = 'center'
reconnectingOverlay.style.background = 'rgba(0,0,0,.8)'
reconnectingOverlay.style.color = 'white'
document.body.appendChild(reconnectingOverlay)
}
reconnectingOverlay = document.createElement('p')
reconnectingOverlay.innerHTML = '🔌 reconnecting Templier...'
reconnectingOverlay.style.position = 'fixed'
reconnectingOverlay.style.top = 0
reconnectingOverlay.style.left = 0
reconnectingOverlay.style.fontSize = '1.25rem'
reconnectingOverlay.style.width = '100%'
reconnectingOverlay.style.height = '100%'
reconnectingOverlay.style.padding = '2rem'
reconnectingOverlay.style.textAlign = 'center'
reconnectingOverlay.style.background = 'rgba(0,0,0,.8)'
reconnectingOverlay.style.color = 'white'
document.body.appendChild(reconnectingOverlay)
}
function hideReconnecting() {
if (reconnectingOverlay == null) {
return
function hideReconnecting() {
if (reconnectingOverlay == null) {
return
}
reconnectingOverlay.remove()
reconnectingOverlay = null
}
reconnectingOverlay.remove()
reconnectingOverlay = null
}

const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'

function connectWebsocket() {
const wsURL = `${protocol}//${window.location.host}${params.WSEventsEndpoint}`
ws = new WebSocket(wsURL)
ws.onopen = function (e) {
if (reconnectingOverlay != null) {
window.location.reload()
function connectWebsocket() {
const wsURL = `${protocol}//${window.location.host}${params.WSEventsEndpoint}`
ws = new WebSocket(wsURL)
ws.onopen = function (e) {
if (reconnectingOverlay != null) {
window.location.reload()
}
hideReconnecting()
hideLoadingIndicator()
if (params.PrintDebugLogs) {
console.debug('templier: connectWebsocket connected')
}
}
hideReconnecting()
hideLoadingIndicator()
if (params.PrintDebugLogs) {
console.debug('templier: connectWebsocket connected')
}
}
if (params.PrintDebugLogs) {
ws.onerror = function (e) {
console.debug('templier: websocket connection error: ' + e.data)
ws.onerror = function (e) {
console.debug('templier: websocket connection error: ' + e.data)
}
}
}
ws.onmessage = function (e) {
switch (e.data) {
case 'r': // Reload
window.location.reload()
case 'ri': // Reload initiated
showLoadingIndicator()
return
case 's': // Shutdown
ws.onmessage = function (e) {
switch (e.data) {
case 'r': // Reload
window.location.reload()
case 'ri': // Reload initiated
showLoadingIndicator()
return
case 's': // Shutdown

}
}
}
ws.onclose = function (e) {
showReconnecting()
hideLoadingIndicator()
if (params.PrintDebugLogs) {
console.debug('templier: websocket disconnected, reconnecting...')
ws.onclose = function (e) {
showReconnecting()
hideLoadingIndicator()
if (params.PrintDebugLogs) {
console.debug('templier: websocket disconnected, reconnecting...')
}
setTimeout(() => connectWebsocket(), 300)
}
setTimeout(() => connectWebsocket(), 300)
}
}

connectWebsocket()

connectWebsocket()
})();
</script>
}
2 changes: 1 addition & 1 deletion internal/server/templates_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be01249

Please sign in to comment.