Skip to content

Commit

Permalink
feat: Cache sqlite-database-integration versions for app session
Browse files Browse the repository at this point in the history
Avoid repeatedly fetching the latest versions each time a site starts.
  • Loading branch information
dcalhoun committed May 17, 2024
1 parent 8afbeb0 commit f0aa6f2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lib/sqlite-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,30 @@ async function getLatestSqliteVersion() {
return sqliteVersions.latest;
}

// Cache version fetches for the duration of the app session
let sqliteVersionsCache: { versions: ( semver.SemVer | null )[]; latest: string } = {
versions: [],
latest: '',
};

async function fetchSqliteVersions() {
if ( sqliteVersionsCache.versions.length ) {
return sqliteVersionsCache;
}

try {
const response = await fetch(
'https://api.github.com/repos/WordPress/sqlite-database-integration/releases'
);
const data: Record< string, string >[] = await response.json();
const versions = data.map( ( release ) => semver.coerce( release.tag_name ) );
return {
sqliteVersionsCache = {
versions,
latest: versions[ 0 ]?.version || '',
};
} catch ( _error ) {
return { versions: [], latest: '' };
// Discard the failed fetch, return the cache
}

return sqliteVersionsCache;
}

0 comments on commit f0aa6f2

Please sign in to comment.