feat(core): rework client modules lifecycles, officially make API public (#6732)

This commit is contained in:
Joshua Chen 2022-04-29 21:11:20 +08:00 committed by GitHub
parent 2429bfbd59
commit ae788c536f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 259 additions and 126 deletions

View file

@ -5,13 +5,48 @@
* LICENSE file in the root directory of this source tree.
*/
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import siteConfig from '@generated/docusaurus.config';
import type {Location} from 'history';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function onRouteUpdate({location}: {location: Location}): void {
// console.log('onRouteUpdate', {location});
function logPage(
event: string,
location: Location,
previousLocation: Location | null,
): void {
console.log(`${event}
Previous location: ${previousLocation?.pathname}
Current location: ${location.pathname}
Current heading: ${document.getElementsByTagName('h1')[0]?.innerText}`);
}
if (ExecutionEnvironment.canUseDOM) {
// console.log('client module example log');
export function onRouteUpdate({
location,
previousLocation,
}: {
location: Location;
previousLocation: Location | null;
}): (() => void) | void {
if (
process.env.NODE_ENV === 'development' ||
siteConfig.customFields!.isDeployPreview
) {
logPage('onRouteUpdate', location, previousLocation);
return () => logPage('onRouteUpdate cleanup', location, previousLocation);
}
return undefined;
}
export function onRouteDidUpdate({
location,
previousLocation,
}: {
location: Location;
previousLocation: Location | null;
}): void {
if (
process.env.NODE_ENV === 'development' ||
siteConfig.customFields!.isDeployPreview
) {
logPage('onRouteDidUpdate', location, previousLocation);
}
}