mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-11 07:12:29 +02:00
feat(core): rework client modules lifecycles, officially make API public (#6732)
This commit is contained in:
parent
2429bfbd59
commit
ae788c536f
16 changed files with 259 additions and 126 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue