mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-02-25 18:55:27 -06:00
Cache requests for plugin version menu (#1533)
Add caching support to reduce the number of requests to lookup the github APIs when generating the plugin version select menu.
This commit is contained in:
@@ -1,31 +1,46 @@
|
||||
changeVersion = function handleVersionedDocs() {
|
||||
{%- comment %}handle development serving site on root{% endcomment %}
|
||||
{%- if site.baseurl.size == 0 %}
|
||||
const basePath = '';
|
||||
{%- else %}
|
||||
const basePath = '/{{ site.github.repository_name }}';
|
||||
{%- endif %}
|
||||
{%- comment %}handle development serving site on root{% endcomment %}
|
||||
{%- if site.baseurl.size == 0 %}
|
||||
const basePath = '';
|
||||
{%- else %}
|
||||
const basePath = '/{{ site.github.repository_name }}';
|
||||
{%- endif %}
|
||||
|
||||
{%- if site.repository_nwo != nil %}
|
||||
const repository_nwo = '{{ site.repository_nwo }}';
|
||||
{%- else %}
|
||||
const repository_nwo = '{{ site.github.repository_nwo }}';
|
||||
{%- endif %}
|
||||
|
||||
const { buildWebStorage, setupCache } = window.AxiosCacheInterceptor;
|
||||
const storage = buildWebStorage(sessionStorage, 'axios-cache:');
|
||||
const axiosCached = setupCache(axios.create(), { storage });
|
||||
|
||||
changeVersion = function handleVersionedDocs(repository_nwo, basePath) {
|
||||
async function loadOptions(select) {
|
||||
const defaultBranchPromise = axios.get(
|
||||
'https://api.github.com/repos/{{ site.repository_nwo or site.github.repository_nwo }}',
|
||||
const defaultBranchPromise = axiosCached.get(
|
||||
`https://api.github.com/repos/${repository_nwo}`,
|
||||
).then(res => {
|
||||
return res.data.default_branch;
|
||||
});
|
||||
|
||||
const versionDir = await axios.get(
|
||||
'https://api.github.com/repos/{{ site.repository_nwo or site.github.repository_nwo }}/git/trees/gh-pages',
|
||||
const statusPredicate = (status) => status === 404 || status >= 200 && status < 400
|
||||
const versionDir = await axiosCached.get(
|
||||
`https://api.github.com/repos/${repository_nwo}/git/trees/gh-pages`, {
|
||||
cache: {
|
||||
cachePredicate: {
|
||||
statusCheck: statusPredicate
|
||||
}
|
||||
},
|
||||
validateStatus: statusPredicate
|
||||
}
|
||||
).then(res => {
|
||||
return res.data.tree.find(t => {
|
||||
return t.path.toLowerCase() === 'version';
|
||||
});
|
||||
|
||||
}).catch(e => {
|
||||
if (e.response.status == "404") {
|
||||
if (res.status === 404) {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw(e);
|
||||
return res.data.tree.find(t => {
|
||||
return t.path.toLowerCase() === 'version';
|
||||
});
|
||||
});
|
||||
|
||||
if (versionDir === undefined || versionDir === null) {
|
||||
@@ -75,7 +90,7 @@ changeVersion = function handleVersionedDocs() {
|
||||
window.location.pathname = targetPath;
|
||||
};
|
||||
|
||||
loadOptions(document.getElementById("docs-version"));
|
||||
loadOptions(document.getElementById("plugin-version"));
|
||||
|
||||
return changeVersion;
|
||||
}();
|
||||
}(repository_nwo, basePath);
|
||||
|
||||
Reference in New Issue
Block a user