summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Federico Brigante <me@fregante.com> 2022-07-20 22:50:31 +0700
committerGravatar GitHub <noreply@github.com> 2022-07-20 22:50:31 +0700
commitf99f34c56f4e73db04816668a375a319d0859b4f (patch)
tree13c8a23f105df2daf7125db697c2a3cf3042611c
parent9a960a597a1b2e50e7361bd01c39563e068ad855 (diff)
downloadrefined-github-f99f34c56f4e73db04816668a375a319d0859b4f.tar.gz
refined-github-f99f34c56f4e73db04816668a375a319d0859b4f.tar.zst
refined-github-f99f34c56f4e73db04816668a375a319d0859b4f.zip
Fix feature cache across ajaxed page loads (#5841)22.7.20
-rw-r--r--source/github-helpers/api.ts12
-rw-r--r--source/github-helpers/get-default-branch.ts22
2 files changed, 22 insertions, 12 deletions
diff --git a/source/github-helpers/api.ts b/source/github-helpers/api.ts
index 13f56f58..4bc0e249 100644
--- a/source/github-helpers/api.ts
+++ b/source/github-helpers/api.ts
@@ -207,7 +207,17 @@ export const v4 = mem(async (
throw await getError(apiResponse as JsonObject);
}, {
- cacheKey: JSON.stringify,
+ cacheKey([query, options]) {
+ // `repository()` uses global state and must be handled explicitly
+ // https://github.com/refined-github/refined-github/issues/5821
+ // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1864
+ const key = [query, options];
+ if (query.includes('repository() {')) {
+ key.push(getRepo()?.nameWithOwner);
+ }
+
+ return JSON.stringify(key);
+ },
});
export async function getError(apiResponse: JsonObject): Promise<RefinedGitHubAPIError> {
diff --git a/source/github-helpers/get-default-branch.ts b/source/github-helpers/get-default-branch.ts
index 17ae3830..4855c465 100644
--- a/source/github-helpers/get-default-branch.ts
+++ b/source/github-helpers/get-default-branch.ts
@@ -13,15 +13,9 @@ import {getRepo, getCurrentBranchFromFeed} from '.';
// "This branch is 1 commit ahead, 27 commits behind master."
const branchInfoRegex = /([^ ]+)\.$/;
-const getDefaultBranch = cache.function(async function (repository?: pageDetect.RepositoryInfo): Promise<string> {
- if (arguments.length === 0) {
- repository = getRepo();
- }
-
- if (!repository) {
- throw new Error('getDefaultBranch was called on a non-repository page');
- }
-
+// DO NOT use optional arguments/defaults in "cached functions" because they can't be memoized effectively
+// https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1864
+const _getDefaultBranch = cache.function(async function (repository: pageDetect.RepositoryInfo): Promise<string> {
if (arguments.length === 0 || JSON.stringify(repository) === JSON.stringify(getRepo())) {
if (pageDetect.isRepoHome()) {
const branchSelector = await elementReady('[data-hotkey="w"]');
@@ -59,7 +53,13 @@ const getDefaultBranch = cache.function(async function (repository?: pageDetect.
}, {
maxAge: {hours: 1},
staleWhileRevalidate: {days: 20},
- cacheKey: ([repository = getRepo()]) => 'default-branch:' + repository!.nameWithOwner,
+ cacheKey: ([repository]) => 'default-branch:' + repository.nameWithOwner,
});
-export default getDefaultBranch;
+export default async function getDefaultBranch(repository: pageDetect.RepositoryInfo | undefined = getRepo()): Promise<string> {
+ if (!repository) {
+ throw new Error('getDefaultBranch was called on a non-repository page');
+ }
+
+ return _getDefaultBranch(repository);
+}