Skip to content

Increase static assets cache max-age for improved performance #11411

@cdrini

Description

@cdrini

Feature Request

Problem / Opportunity

Pagespeed Insights recommends improving cache lifetimes for our static assets to save an estimated 272 KiB and speed up repeat visits. Currently, many assets (images, SVGs, covers, etc.) have cache TTLs of only 1-3 hours, which is much shorter than it could be.

A long cache lifetime can make Open Library pages load faster for users, especially those accessing frequently-used resources such as covers and UI images.

Reference analysis: Pagespeed Insights result

Pagespeed Insights cache report

Justification

Implementing proper caching headers for static assets can reduce transfer size, lower server overhead, and improve perceived speed for users.

Breakdown

Related files

  • Static asset handlers (image, CSS, SVG, GIF uploads) ; not sure where the code for where this happens in the local environment is.
  • in prod, static assets come from here:
    location ~ ^/(images/.*|favicon.ico|robots.txt)$ {
    rewrite ^(.*)$ /static/$1 last;
    }
    location ~ ^/(y_key_[0-9a-f]+.html|LiveSearchSiteAuth.xml)$ {
    root /olsystem/www;
    }
    location ~ ^/static/(docs|tour|sitemaps|jsondumps|images/shelfview|sampledump.txt.gz)(/.*)?$ {
    root /sitemaps;
    autoindex on;
    rewrite ^/static/(.*)$ /$1 break;
    }
    location /static {
    autoindex on;
    expires 1h;
    }
    location /static/build {
    expires max;
    }
  • Covers; see
    if key == 'id':
    etag = f"{d.id}-{size.lower()}"
    if not web.modified(trim_microsecond(d.created), etag=etag):
    return web.notmodified()
    web.header('Cache-Control', 'public')
    # this image is not going to expire in next 100 years.
    web.expires(100 * 365 * 24 * 3600)
    else:
    web.header('Cache-Control', 'public')
    # Allow the client to cache the image for 10 mins to avoid further requests
    web.expires(10 * 60)
    . It seems like this is trying to set it to 100 years with web.expires, but it doesn't appear to be working.

May need to audit current asset serving and add Cache-Control headers or similar to extend cache lifetime for images and other static resources.

Stakeholders

@RayBB

Metadata

Metadata

Assignees

No one assigned

    Labels

    Lead: @RayBBIssues overseen by Ray (Onboarding & Documentation Lead) [manages]Module: Cover ServiceCover Store (book covers service)Needs: BreakdownThis big issue needs a checklist or subissues to describe a breakdown of work. [managed]Priority: 2Important, as time permits. [managed]Theme: PerformanceIssues related to UI or Server performance. [managed]Type: Feature RequestIssue describes a feature or enhancement we'd like to implement. [managed]

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions