-
-
Notifications
You must be signed in to change notification settings - Fork 363
🔧 router scrollBehavior #11572
base: main
Are you sure you want to change the base?
🔧 router scrollBehavior #11572
Changes from all commits
daddb46
052b519
db24a93
0c321ee
eeedbc3
d3539e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { RouterConfig } from '@nuxt/schema' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const findEl = async (hash, x = 0) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.querySelector(hash) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| || new Promise((resolve) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (x > 50) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return resolve(document.querySelector('#app')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve(findEl(hash, ++x || 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, 100) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Simplify the retry logic and improve error handling. The function has several areas for improvement:
-const findEl = async (hash, x = 0) => {
+const findEl = async (hash: string, attempts = 0): Promise<Element | null> => {
return (
document.querySelector(hash)
|| new Promise((resolve) => {
- if (x > 50) {
- return resolve(document.querySelector('#app'))
+ if (attempts >= 50) {
+ return resolve(document.querySelector('#app') || null)
}
setTimeout(() => {
- resolve(findEl(hash, ++x || 1))
+ resolve(findEl(hash, attempts + 1))
}, 100)
})
)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const defaultScrollBehavior = async (to): Promise<false | any | { left: number, top: number }> => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const defaultScrollBehavior = async (to): Promise<false | any | { left: number, top: number }> => { | |
| const defaultScrollBehavior = async (to: RouteLocationNormalized): Promise<false | any | { left: number, top: number }> => { |
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.
🛠️ Refactor suggestion
Improve type safety and clarify return behavior.
The return type uses any which reduces type safety. Also, when a route is in the disableScrollToTop list, the function returns undefined, which may not be the intended behavior for maintaining current scroll position.
-const defaultScrollBehavior = async (to): Promise<false | any | { left: number, top: number }> => {
+const defaultScrollBehavior = async (to: any): Promise<false | { left: number, top: number }> => {Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In app/router.options.ts at line 17, the function defaultScrollBehavior uses
'any' in its return type, reducing type safety, and returns undefined for routes
in disableScrollToTop, which may cause unintended scroll behavior. Update the
return type to explicitly include 'false' or the scroll position object,
removing 'any', and ensure the function returns 'false' instead of undefined
when the route is in disableScrollToTop to maintain current scroll position.
This file was deleted.
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.
Consider adding explicit type annotations for the 'hash' parameter and for the function's return type to enhance clarity and maintainability.