Skip to content

Commit 3112014

Browse files
committed
Better handling of the abortcontroller cancelling
1 parent feeba29 commit 3112014

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

react-static-web-apps-auth/src/ClientPrincipalContext.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,28 @@ const ClientPrincipalContextProvider = ({
4040
const [swaCookie, setSwaCookie] = useState<string | null>(null);
4141

4242
useEffect(() => {
43-
const abortController = new AbortController();
43+
// const abortController = new AbortController();
4444
const run = async () => {
4545
try {
4646
const res = await fetch("/.auth/me", {
47-
signal: abortController.signal,
47+
// signal: abortController.signal,
4848
});
4949
const json: {
5050
clientPrincipal: ClientPrincipal | null;
5151
} = await res.json();
5252
if (json.clientPrincipal) {
5353
setClientPrincipal(json.clientPrincipal);
5454
}
55-
} catch (e) {
55+
} catch (e: any) {
56+
if (e.name === "AbortError") {
57+
// This was caused by the abort controller being aborted so we can ignore it
58+
return;
59+
}
60+
5661
if (window.location.hostname === "localhost") {
5762
console.warn(
58-
"Can't access the auth endpoint. For local development, please use the Static Web Apps CLI to emulate authentication: https://github.com/azure/static-web-apps-cli"
63+
"Can't access the auth endpoint. For local development, please use the Static Web Apps CLI to emulate authentication: https://github.com/azure/static-web-apps-cli",
64+
e
5965
);
6066
} else {
6167
console.error(`Failed to unpack JSON.`, e);
@@ -70,7 +76,7 @@ const ClientPrincipalContextProvider = ({
7076

7177
run();
7278

73-
return () => abortController.abort();
79+
// return () => abortController.abort();
7480
}, []);
7581

7682
return (

0 commit comments

Comments
 (0)