:lightning: lazy load archive

This commit is contained in:
mi
2025-11-15 20:01:06 +10:00
parent bbd8e0a96d
commit 61aa0aaba7
3 changed files with 297 additions and 5 deletions

71
app.py
View File

@@ -262,14 +262,22 @@ def group_comics_by_section(comics_list):
@app.route('/archive')
def archive():
"""Archive page showing all comics"""
# Initial batch size for server-side rendering
initial_batch = 24
# Reverse order to show newest first
comics = [enrich_comic(comic) for comic in reversed(COMICS)]
all_comics = [enrich_comic(comic) for comic in reversed(COMICS)]
# Only take the first batch for initial render
initial_comics = all_comics[:initial_batch]
# Group by section if enabled
sections = group_comics_by_section(comics)
sections = group_comics_by_section(initial_comics)
return render_template('archive.html', title='Archive',
sections=sections)
sections=sections,
total_comics=len(COMICS),
initial_batch=initial_batch)
@app.route('/about')
@@ -323,6 +331,63 @@ def api_comic(comic_id):
return jsonify(comic)
@app.route('/api/archive')
def api_archive():
"""API endpoint - returns paginated archive data"""
page = request.args.get('page', 1, type=int)
per_page = request.args.get('per_page', 24, type=int)
# Limit per_page to reasonable values
per_page = min(max(per_page, 1), 100)
# Reverse order to show newest first
all_comics = [enrich_comic(comic) for comic in reversed(COMICS)]
# Group by section if enabled
sections = group_comics_by_section(all_comics)
# Calculate pagination
total_comics = len(all_comics)
start_idx = (page - 1) * per_page
end_idx = start_idx + per_page
# Handle section-aware pagination
result_sections = []
current_idx = 0
for section_title, section_comics in sections:
section_start = current_idx
section_end = current_idx + len(section_comics)
# Check if this section overlaps with our requested page
if section_end > start_idx and section_start < end_idx:
# Calculate which comics from this section to include
comics_start = max(0, start_idx - section_start)
comics_end = min(len(section_comics), end_idx - section_start)
paginated_comics = section_comics[comics_start:comics_end]
if paginated_comics:
result_sections.append({
'section_title': section_title,
'comics': paginated_comics
})
current_idx = section_end
# Stop if we've gone past the requested range
if current_idx >= end_idx:
break
return jsonify({
'sections': result_sections,
'page': page,
'per_page': per_page,
'total_comics': total_comics,
'has_more': end_idx < total_comics
})
@app.route('/sitemap.xml')
def sitemap():
"""Serve the static sitemap.xml file"""