Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

New: Use ETAG in header for reading releases from the cache #12

Merged
merged 2 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ export default {
].join(",")
}
}),
VERSIONS_PATH: "versions"
VERSIONS_PATH: "versions",
LOCAL_STORAGE: {
GET_RELEASES_ETAG: "getReleasesETAG",
RELEASES_DATA: "releasesData",
}
};
18 changes: 15 additions & 3 deletions src/helpers/releasesInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@ export const retrieveReleases = gitHubURL =>
constants.GITHUB_API_RELEASE_URL.replace(
"{repo_name}",
repositoryName
)
),
{
headers: {
"If-None-Match": localStorage.getItem(constants.LOCAL_STORAGE.GET_RELEASES_ETAG) || ""
}
}
)
.then(response => {
if (response.status === 200) {
localStorage.setItem(constants.LOCAL_STORAGE.GET_RELEASES_ETAG, response.headers.etag.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if we should clear the localStorage on unmount of the app? Not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is necessary because the data in local storage is not dependent on anything happening in the DOM

localStorage.setItem(constants.LOCAL_STORAGE.RELEASES_DATA, JSON.stringify(response.data));
resolve(response.data);
} else {
resolve([]);
}
})
.catch(err => {
reject(err);
if (err.response && err.response.status === 304) {
resolve(JSON.parse(localStorage.getItem(constants.LOCAL_STORAGE.RELEASES_DATA)));
}
else {
reject(err);
}
});
});

Expand All @@ -31,4 +43,4 @@ export const getLatestRelease = releases => {
};

export const isLatestRelease = (versions, targetRelease) =>
getLatestRelease(versions) === targetRelease.tag_name;
getLatestRelease(versions) === targetRelease.tag_name;