Skip to content

Commit e358da0

Browse files
committed
Lazy load workshop items
1 parent 481abcb commit e358da0

1 file changed

Lines changed: 56 additions & 31 deletions

File tree

static/main.js

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,68 @@ fetch( 'https://api.github.com/repositories/42366054/releases/latest' )
3535
}
3636
} );
3737

38-
fetch( 'https://steamdb.info/api/Source2ViewerWorkshop/' )
39-
.then( function( response )
40-
{
41-
if( !response.ok )
38+
function LoadWorkshop()
39+
{
40+
fetch( 'https://steamdb.info/api/Source2ViewerWorkshop/' )
41+
.then( function( response )
4242
{
43-
throw new Error( 'Failed to fetch workshop items' );
44-
}
43+
if( !response.ok )
44+
{
45+
throw new Error( 'Failed to fetch workshop items' );
46+
}
4547

46-
return response.json();
47-
} )
48-
.then( function( response )
49-
{
50-
if( !response.success || !response.data )
48+
return response.json();
49+
} )
50+
.then( function( response )
5151
{
52-
throw new Error( 'Failed to fetch workshop items' );
53-
}
52+
if( !response.success || !response.data )
53+
{
54+
throw new Error( 'Failed to fetch workshop items' );
55+
}
5456

55-
const dateFormatter = new Intl.DateTimeFormat(undefined, {
56-
dateStyle: 'medium',
57-
});
57+
const dateFormatter = new Intl.DateTimeFormat(undefined, {
58+
dateStyle: 'medium',
59+
});
5860

59-
const dom = document.querySelectorAll( '.workshop-item' );
61+
const dom = document.querySelectorAll( '.workshop-item' );
6062

61-
for( let i = 0; i < response.data.length && i < dom.length; i++ )
62-
{
63-
const file = response.data[ i ];
64-
const element = dom[ i ];
65-
const date = new Date( file.time_created * 1000 );
63+
for( let i = 0; i < response.data.length && i < dom.length; i++ )
64+
{
65+
const file = response.data[ i ];
66+
const element = dom[ i ];
67+
const date = new Date( file.time_created * 1000 );
6668

67-
const params = new URLSearchParams();
68-
params.set( 'id', file.id );
69-
params.set( 'utm_source', 'Source 2 Viewer' );
70-
params.set( 'searchtext', 'valveresourceformat' );
69+
const params = new URLSearchParams();
70+
params.set( 'id', file.id );
71+
params.set( 'utm_source', 'Source 2 Viewer' );
72+
params.set( 'searchtext', 'valveresourceformat' );
7173

72-
element.href = `https://steamcommunity.com/sharedfiles/filedetails/?${params}`;
73-
element.querySelector( '.workshop-image' ).src = file.preview_url;
74-
element.querySelector( '.workshop-title' ).textContent = file.title;
75-
element.querySelector( '.workshop-info' ).textContent = `${dateFormatter.format(date)}${file.subscriptions.toLocaleString()} subscribers`;
76-
}
74+
element.href = `https://steamcommunity.com/sharedfiles/filedetails/?${params}`;
75+
element.querySelector( '.workshop-image' ).src = file.preview_url;
76+
element.querySelector( '.workshop-title' ).textContent = file.title;
77+
element.querySelector( '.workshop-info' ).textContent = `${dateFormatter.format(date)}${file.subscriptions.toLocaleString()} subscribers`;
78+
}
79+
} );
80+
}
81+
82+
if( 'IntersectionObserver' in window )
83+
{
84+
const observer = new window.IntersectionObserver( entries =>
85+
{
86+
entries.forEach( entry =>
87+
{
88+
if( entry.isIntersecting )
89+
{
90+
observer.disconnect();
91+
LoadWorkshop();
92+
}
93+
} );
94+
}, {
95+
rootMargin: '200px 0px 0px 0px'
7796
} );
97+
observer.observe( document.querySelector( '.workshop' ) );
98+
}
99+
else
100+
{
101+
LoadWorkshop();
102+
}

0 commit comments

Comments
 (0)