-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): rework client modules lifecycles, officially make API public #6732
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d71d643
fix: do not fire onRouteUpdate on hash change; new onRouteDidUpdate l…
Josh-Cena a4e16b9
use a separate dispatcher layer
Josh-Cena f33fb16
address reviews
Josh-Cena d725324
move this up
Josh-Cena 98f3e1c
Merge branch 'main' into jc/fix-client-module
Josh-Cena b581e6d
minor refactor
Josh-Cena 403d5f0
add docs
Josh-Cena 297e149
move nprogress to theme-classic
Josh-Cena c2410ea
move cb
Josh-Cena 004d761
move it back...
Josh-Cena 773e580
minor doc change
Josh-Cena 4dda8a6
minor refactors
Josh-Cena f338261
migrate analytics to onRouteDidUpdate
Josh-Cena 409c29f
docs
Josh-Cena de9d03e
fix
Josh-Cena 85e8c61
Merge branch 'main' into jc/fix-client-module
Josh-Cena 8d31081
fire onRouteUpdate during init
Josh-Cena 285e830
do not fire on SSG
Josh-Cena 5746f95
minor refactor
Josh-Cena 9286297
fix...
Josh-Cena c244bb1
fix analytics
Josh-Cena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import nprogress from 'nprogress'; | ||
import './nprogress.css'; | ||
import type {ClientModule} from '@docusaurus/types'; | ||
|
||
nprogress.configure({showSpinner: false}); | ||
|
||
const delay = 200; | ||
|
||
const clientModule: ClientModule = { | ||
onRouteUpdate({location, previousLocation}) { | ||
if (previousLocation && location.pathname !== previousLocation.pathname) { | ||
const progressBarTimeout = window.setTimeout(() => { | ||
nprogress.start(); | ||
}, delay); | ||
return () => window.clearTimeout(progressBarTimeout); | ||
} | ||
return undefined; | ||
}, | ||
onRouteDidUpdate() { | ||
nprogress.done(); | ||
}, | ||
}; | ||
|
||
export default clientModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/docusaurus/src/client/ClientLifecyclesDispatcher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {useLayoutEffect, type ReactElement} from 'react'; | ||
import clientModules from '@generated/client-modules'; | ||
import type {ClientModule} from '@docusaurus/types'; | ||
import type {Location} from 'history'; | ||
|
||
export function dispatchLifecycleAction<K extends keyof ClientModule>( | ||
lifecycleAction: K, | ||
...args: Parameters<NonNullable<ClientModule[K]>> | ||
): () => void { | ||
const callbacks = clientModules.map((clientModule) => { | ||
const lifecycleFunction = (clientModule?.default?.[lifecycleAction] ?? | ||
clientModule[lifecycleAction]) as | ||
| (( | ||
...a: Parameters<NonNullable<ClientModule[K]>> | ||
) => (() => void) | void) | ||
| undefined; | ||
|
||
return lifecycleFunction?.(...args); | ||
}); | ||
return () => callbacks.forEach((cb) => cb?.()); | ||
} | ||
|
||
function ClientLifecyclesDispatcher({ | ||
children, | ||
location, | ||
previousLocation, | ||
}: { | ||
children: ReactElement; | ||
location: Location; | ||
previousLocation: Location | null; | ||
}): JSX.Element { | ||
useLayoutEffect(() => { | ||
if (previousLocation !== location) { | ||
const {hash} = location; | ||
if (!hash) { | ||
window.scrollTo(0, 0); | ||
} else { | ||
const id = decodeURIComponent(hash.substring(1)); | ||
const element = document.getElementById(id); | ||
element?.scrollIntoView(); | ||
} | ||
dispatchLifecycleAction('onRouteDidUpdate', {previousLocation, location}); | ||
} | ||
}, [previousLocation, location]); | ||
return children; | ||
} | ||
|
||
export default ClientLifecyclesDispatcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check actually isn't necessary before either...