Skip to content

Add 1-day local cache for style files fetched from GitHub#12

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/add-cdn-or-caching-style-files
Draft

Add 1-day local cache for style files fetched from GitHub#12
Copilot wants to merge 2 commits intomainfrom
copilot/add-cdn-or-caching-style-files

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 18, 2026

CSS files were fetched from GitHub on every page load and every browser startup/navigation with no caching. This adds a 1-day TTL cache using chrome.storage.local.

Key changes

  • getCachedOrFetch(url) — new helper in both service_worker.js and content.js that checks chrome.storage.local before hitting the network. Cache entries are stored as cssCache_<url> → { css, cachedAt } and expire after 24h.
  • getThemes() cachingthemes.json metadata is now cached in local storage (themesJson + themesJsonCachedAt), re-fetched only after 1 day.
  • chrome.storage.syncchrome.storage.local for themes — sync storage has a 100KB total quota, which is insufficient for CSS content. Local storage (10MB) is used instead. activeThemes user preference remains in sync storage.
  • Deferred DOM injectionactivateTheme() now uses Promise.all() to wait for all CSS files before appending the <style> element, preventing partial styling.
  • Fixed CSS concatenation bug — service worker was overwriting theme.css on each file instead of appending; also now saves to storage only after all fetches complete.
function getCachedOrFetch(url) {
  return new Promise((resolve, reject) => {
    const cacheKey = 'cssCache_' + url;
    chrome.storage.local.get([cacheKey], (result) => {
      const cached = result[cacheKey];
      if (cached && cached.cachedAt && (Date.now() - cached.cachedAt) < CACHE_DURATION) {
        resolve(cached.css);
      } else {
        fetch(url)
          .then((res) => res.text())
          .then((css) => {
            chrome.storage.local.set({ [cacheKey]: { css, cachedAt: Date.now() } });
            resolve(css);
          })
          .catch(reject);
      }
    });
  });
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Add CDN or caching to style files</issue_title>
<issue_description></issue_description>

<agent_instructions>Download files from github and store at each user locally. Instead of fetching everytime. Store it for 1 day currently. Do not ship files inside browser extensions.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: despokd <23119848+despokd@users.noreply.github.com>
Copilot AI changed the title [WIP] Add CDN or caching to style files Add 1-day local cache for style files fetched from GitHub Mar 18, 2026
Copilot AI requested a review from despokd March 18, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CDN or caching to style files

2 participants