mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-10 23:02:56 +02:00
refactor(pwa): simplify registerSW code, fix ESLint errors (#7579)
This commit is contained in:
parent
bada5c11cc
commit
7869e74fd7
14 changed files with 204 additions and 247 deletions
|
@ -5,7 +5,13 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import React, {useContext, useEffect, useState, type ReactNode} from 'react';
|
||||
import React, {
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
useRef,
|
||||
type ReactNode,
|
||||
} from 'react';
|
||||
import {useDocsPreferredVersion} from '@docusaurus/theme-common';
|
||||
import {useVersions} from '@docusaurus/plugin-content-docs/client';
|
||||
import Translate from '@docusaurus/Translate';
|
||||
|
@ -24,11 +30,21 @@ export function VersionsProvider({
|
|||
children: ReactNode;
|
||||
}): JSX.Element {
|
||||
const [canaryVersion, setCanaryVersion] = useState<ContextValue | null>(null);
|
||||
const mounted = useRef(true);
|
||||
useEffect(() => {
|
||||
mounted.current = true;
|
||||
return () => {
|
||||
mounted.current = false;
|
||||
};
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
fetch('https://registry.npmjs.org/@docusaurus/core')
|
||||
.then((res) => res.json())
|
||||
.then(
|
||||
(data: {versions: string[]; time: {[versionName: string]: string}}) => {
|
||||
if (!mounted.current) {
|
||||
return;
|
||||
}
|
||||
const name = Object.keys(data.versions).at(-1)!;
|
||||
const time = data.time[name];
|
||||
setCanaryVersion({name, time});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue