Skip to content

Commit

Permalink
Merge pull request #579 from adobe/issue-565
Browse files Browse the repository at this point in the history
feat: cache help content for 4 hours
  • Loading branch information
rofe authored Nov 1, 2023
2 parents ab777eb + 4ac6ab0 commit e314d6c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
19 changes: 13 additions & 6 deletions src/extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
getGitHubSettings,
setConfig,
getConfig,
updateProjectConfigs,
populateUrlCache,
queryUrlCache,
setDisplay,
Expand Down Expand Up @@ -338,8 +337,13 @@ function getHelpLanguage() {
* while respecting previous user acknowledgements.
*/
async function updateHelpContent() {
const hlxSidekickHelpContent = await getConfig('sync', 'hlxSidekickHelpContent') || [];
log.debug('existing help content', hlxSidekickHelpContent);
// don't fetch new help content for at least 4 hours
const helpContentFetched = await getConfig('local', 'hlxSidekickHelpContentFetched');
if ((helpContentFetched || 0) > Date.now() - 14400000) {
return;
}
const helpContent = await getConfig('sync', 'hlxSidekickHelpContent') || [];
log.debug('existing help content', helpContent);
const lang = getHelpLanguage();
const resp = await fetch(`https://www.hlx.live/tools/sidekick/${lang}/help.json`);
if (resp.ok) {
Expand All @@ -363,9 +367,9 @@ async function updateHelpContent() {
return true;
})
.map((incoming) => {
const index = hlxSidekickHelpContent.findIndex((existing) => existing.id === incoming.id);
const index = helpContent.findIndex((existing) => existing.id === incoming.id);
return {
...(index >= 0 ? hlxSidekickHelpContent[index] : {}),
...(index >= 0 ? helpContent[index] : {}),
...incoming,
steps: incoming.steps
.split(',')
Expand All @@ -377,6 +381,10 @@ async function updateHelpContent() {
await setConfig('sync', {
hlxSidekickHelpContent: updatedHelpContent,
});
// remember when help content was last fetched
await setConfig('local', {
hlxSidekickHelpContentFetched: Date.now(),
});
} catch (e) {
log.error('failed to update help content', e);
}
Expand Down Expand Up @@ -733,7 +741,6 @@ const internalActions = {
// });

await updateHelpContent();
await updateProjectConfigs();
await setUserAgentHeader();
await updateAdminAuthHeaderRules();
log.info('sidekick extension initialized');
Expand Down
20 changes: 0 additions & 20 deletions src/extension/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,23 +711,3 @@ export function toggleDisplay(cb) {
setDisplay(!display, cb);
});
}

/**
* Updates the legacy project configurations to the new format.
* @deprecated
* @todo remove
*/
export async function updateProjectConfigs() {
const configs = await getConfig('sync', 'hlxSidekickConfigs');
const projects = await getConfig('sync', 'hlxSidekickProjects');
if (configs && !projects) {
// migrate old to new project configs
for (let i = 0; i < configs.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await setProject(configs[i]);
}
// TODO: remove old project configs
// await removeConfig('sync', 'hlxSidekickConfigs');
log.info('project config updated');
}
}
11 changes: 0 additions & 11 deletions test/extension/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,17 +335,6 @@ describe('Test extension utils', () => {
})).to.be.true;
});

it('updateProjectConfigs', async () => {
sandbox.spy(window.chrome.storage.sync, 'set');
sandbox.spy(window.chrome.storage.sync, 'remove');
await utils.removeConfig('sync', 'hlxSidekickProjects');
await utils.updateProjectConfigs();
// expect(chrome.storage.sync.remove.calledWith('hlxSidekickConfigs')).to.be.true;
expect(chrome.storage.sync.set.calledWith({
hlxSidekickProjects: ['test/legacy-project'],
})).to.be.true;
});

it('setDisplay', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'set');
await utils.setDisplay(true);
Expand Down

0 comments on commit e314d6c

Please sign in to comment.