Skip to content

Commit

Permalink
Merge pull request #509 from hackclub/alert-user-to-reload-on-failure
Browse files Browse the repository at this point in the history
Alert user to reload page on failed asset loading
  • Loading branch information
polytroper authored May 8, 2023
2 parents 4c925f3 + 00dd3c4 commit ba5c497
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Types/Assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function Assets(spec) {
let loadTotal = 0
let loadCount = 0
let loaded = false
let failed = false

const imageExtensions = ['svg', 'png', 'jpg', 'jpeg', 'webp']
const soundExtensions = ['m4a', 'mp3', 'ogg', 'wav']
Expand Down Expand Up @@ -50,21 +51,21 @@ function Assets(spec) {
asset.loading = 'eager'
asset.src = path
asset.onload = () => assetLoaded(path)
asset.onerror = (error) => {console.error('Asset request failed', error, path)}
asset.onerror = (error) => handleFailure(error, path)
} else if (isSound) {
;(assetSpec.src = path),
(asset = new Howl({
...assetSpec,
onload: () => assetLoaded(path),
onloaderror: (error) => {console.error('Asset request failed', error, path)}
onloaderror: (error) => handleFailure(error, path)
}))
} else if (isShader) {
fetch(path)
.then((raw) => raw.text())
.then((text) => {
object[key] = text
assetLoaded(path)
}).catch((error) => {console.log('Asset request failed', error, path)})
}).catch((error) => handleFailure(error, path))
} else {
return
}
Expand Down Expand Up @@ -93,6 +94,14 @@ function Assets(spec) {
}
}

function handleFailure(error, path) {
console.error('Asset request failed', error, path)
if (!failed) {
alert("Something failed to load. Try refreshing the page.")
failed = true
}
}

return _.mixIn(self, {
get loaded() {
return loaded
Expand Down

1 comment on commit ba5c497

@vercel
Copy link

@vercel vercel bot commented on ba5c497 May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sinerider – ./

sinerider.hackclub.dev
sinerider-git-main.hackclub.dev
www.sinerider.com

Please sign in to comment.