1

I am modifying my google chrome extension and researching a way to cache data. The extension displays 2 types of content, they both can be treated as arrays of objects (less than 5kb in total), say news list from RSS feed and list of recipes.

To save loading time first I could use chrome.storage.sync to store data once and check cache life every other time and renew if needed.

But the difference is that it's a new tab type extension, so it' being loaded every time user opens a new tab - so load speed of the data for the UI is even more critical. So I was thinking about using an extra background page or background script (https://developer.chrome.com/extensions/background_pages) to store that data there just as simple objects in memory.

Can the background script be considered a way to go or there are any restrictions with access (or speed of access) or data transfer between new tab script and the background?

I've also checked that question, but that didn't help How do I cache data in Chrome Extension?

Community
  • 1
  • 1
shershen
  • 9,875
  • 11
  • 39
  • 60
  • I'll comment on NTP load speed. From what I saw researching it, a _persistent_ background page is the _only_ way to ensure near-instantaneous NTP loading, otherwise I always see an annoying lag before the content appears in an empty tab. The majority of NTP extensions, as far as I know, exhibit the lag and still are popular, so I guess it doesn't bother people too much. – wOxxOm Jun 26 '16 at 11:52
  • When you are saying "new tab type extension", do you mean you extension overrides the newtab page? If so, to my knowledge, content scripts can't be injected into **overrided** newtab page, so how do you transfer data from background page? Through a middleware server? – Haibara Ai Jun 27 '16 at 02:42
  • @HaibaraAi "do you mean you extension overrides the newtab page" - yes exactly. "to my knowledge, content scripts can't be injected into overrided newtab page" - I didn't know that. thanks a lot I'll dive into that question – shershen Jun 27 '16 at 15:00
  • @shershen, silly me... You could just use `chrome.runtime.sendMessage` to exchange data between overrided newtab and background page – Haibara Ai Jun 28 '16 at 00:39
  • I know this thread is super old, but with manifest v3 coming out, do you have any suggestions for fast NTP loads @wOxxOm? I find that loading from chrome storage takes like 100ms and that is where I store my initial page data. I think indexeddb might be a bit faster. Also considering switching to preact to speed up load times... – nahtnam Jan 24 '22 at 03:20

1 Answers1

1

Yes, background script can be considered a way.

You could

  1. load the storage in advance in background page,
  2. then every time the overrided newtab is opened, you could start message passing via chrome.runtime.sendMessage,
  3. then once background page receives it, package all the info needed and send it via sendResponse,
  4. finally you could get the cached info through the response in new page.

Sample code: Injecting Javascript into Newly Created Tab in Chrome Extension

Community
  • 1
  • 1
Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
  • 1
    Is there any actual evidence that `sendMessage`-`sendResponse` dance is faster than `storage` API loads in this case? I mean, both are relatively "slow". It needs careful measurement to see if savings on JSON parsing outweigh involving another piece of non-native code. – Xan Jun 28 '16 at 12:11
  • @Xan according to my raw test case it worth doing! The "sendMessage-sendResponse dance" makes it in 0.01 ms instead of the chrome.history lookup which takes 300-500 ms – shershen Jul 25 '16 at 22:19