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:
Darragh Bailey
2022-07-29 14:56:57 +01:00
committed by GitHub
parent bec7fdc462
commit 8a2bae3535
3 changed files with 42 additions and 22 deletions

View File

@@ -1,31 +1,46 @@
changeVersion = function handleVersionedDocs() { {%- comment %}handle development serving site on root{% endcomment %}
{%- comment %}handle development serving site on root{% endcomment %} {%- if site.baseurl.size == 0 %}
{%- if site.baseurl.size == 0 %} const basePath = '';
const basePath = ''; {%- else %}
{%- else %} const basePath = '/{{ site.github.repository_name }}';
const basePath = '/{{ site.github.repository_name }}'; {%- endif %}
{%- 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) { async function loadOptions(select) {
const defaultBranchPromise = axios.get( const defaultBranchPromise = axiosCached.get(
'https://api.github.com/repos/{{ site.repository_nwo or site.github.repository_nwo }}', `https://api.github.com/repos/${repository_nwo}`,
).then(res => { ).then(res => {
return res.data.default_branch; return res.data.default_branch;
}); });
const versionDir = await axios.get( const statusPredicate = (status) => status === 404 || status >= 200 && status < 400
'https://api.github.com/repos/{{ site.repository_nwo or site.github.repository_nwo }}/git/trees/gh-pages', const versionDir = await axiosCached.get(
`https://api.github.com/repos/${repository_nwo}/git/trees/gh-pages`, {
cache: {
cachePredicate: {
statusCheck: statusPredicate
}
},
validateStatus: statusPredicate
}
).then(res => { ).then(res => {
return res.data.tree.find(t => { if (res.status === 404) {
return t.path.toLowerCase() === 'version';
});
}).catch(e => {
if (e.response.status == "404") {
return null; return null;
} }
throw(e); return res.data.tree.find(t => {
return t.path.toLowerCase() === 'version';
});
}); });
if (versionDir === undefined || versionDir === null) { if (versionDir === undefined || versionDir === null) {
@@ -75,7 +90,7 @@ changeVersion = function handleVersionedDocs() {
window.location.pathname = targetPath; window.location.pathname = targetPath;
}; };
loadOptions(document.getElementById("docs-version")); loadOptions(document.getElementById("plugin-version"));
return changeVersion; return changeVersion;
}(); }(repository_nwo, basePath);

View File

@@ -1,3 +1,8 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.27.2/axios.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.27.2/axios.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/axios-cache-interceptor@0.10.6/dist/index.bundle.js"
integrity="sha256-yJbSlTxKmgU+sjlMx48OSjoiUsboy18gXTxUBniEEO0="
crossorigin="anonymous"
></script>
<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-light.css' | relative_url }}" media="(prefers-color-scheme: light)"> <link rel="stylesheet" href="{{ '/assets/css/just-the-docs-light.css' | relative_url }}" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-dark.css' | relative_url }}" media="(prefers-color-scheme: dark)"> <link rel="stylesheet" href="{{ '/assets/css/just-the-docs-dark.css' | relative_url }}" media="(prefers-color-scheme: dark)">

View File

@@ -1,6 +1,6 @@
<div class="site-footer"> <div class="plugin-version-select site-footer">
Plugin Version: Plugin Version:
<select id="docs-version" onChange="changeVersion(this)"> <select id="plugin-version" onChange="changeVersion(this)">
</select> </select>
</div> </div>
<script src="{% asset "js/version_switcher.js" @path %}"></script> <script src="{% asset "js/version_switcher.js" @path %}"></script>