play和live豁免进度条参数显示
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
import { usePathname, useRouter } from 'next/navigation';
|
import { usePathname, useSearchParams, useRouter } from 'next/navigation';
|
||||||
import NProgress from 'nprogress';
|
import NProgress from 'nprogress';
|
||||||
|
|
||||||
// 创建全局钩子来拦截 router
|
// 创建全局钩子来拦截 router
|
||||||
@@ -9,6 +9,7 @@ let globalRouterRef: any = null;
|
|||||||
|
|
||||||
export default function TopProgressBar() {
|
export default function TopProgressBar() {
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const isNavigatingRef = useRef(false);
|
const isNavigatingRef = useRef(false);
|
||||||
const previousPathnameRef = useRef(pathname);
|
const previousPathnameRef = useRef(pathname);
|
||||||
@@ -34,7 +35,11 @@ export default function TopProgressBar() {
|
|||||||
router.push = function (...args: Parameters<typeof originalPush>) {
|
router.push = function (...args: Parameters<typeof originalPush>) {
|
||||||
const targetUrl = args[0] as string;
|
const targetUrl = args[0] as string;
|
||||||
const targetPathname = new URL(targetUrl, window.location.href).pathname;
|
const targetPathname = new URL(targetUrl, window.location.href).pathname;
|
||||||
if (targetPathname !== previousPathnameRef.current) {
|
const currentPathname = window.location.pathname;
|
||||||
|
|
||||||
|
// /play 和 /live 页面:参数变化也显示进度条
|
||||||
|
// 其他页面:仅路径变化时显示进度条
|
||||||
|
if (currentPathname === '/play' || currentPathname === '/live' || targetPathname !== previousPathnameRef.current) {
|
||||||
isNavigatingRef.current = true;
|
isNavigatingRef.current = true;
|
||||||
NProgress.start();
|
NProgress.start();
|
||||||
}
|
}
|
||||||
@@ -45,7 +50,11 @@ export default function TopProgressBar() {
|
|||||||
router.replace = function (...args: Parameters<typeof originalReplace>) {
|
router.replace = function (...args: Parameters<typeof originalReplace>) {
|
||||||
const targetUrl = args[0] as string;
|
const targetUrl = args[0] as string;
|
||||||
const targetPathname = new URL(targetUrl, window.location.href).pathname;
|
const targetPathname = new URL(targetUrl, window.location.href).pathname;
|
||||||
if (targetPathname !== previousPathnameRef.current) {
|
const currentPathname = window.location.pathname;
|
||||||
|
|
||||||
|
// /play 和 /live 页面:参数变化也显示进度条
|
||||||
|
// 其他页面:仅路径变化时显示进度条
|
||||||
|
if (currentPathname === '/play' || currentPathname === '/live' || targetPathname !== previousPathnameRef.current) {
|
||||||
isNavigatingRef.current = true;
|
isNavigatingRef.current = true;
|
||||||
NProgress.start();
|
NProgress.start();
|
||||||
}
|
}
|
||||||
@@ -121,7 +130,7 @@ export default function TopProgressBar() {
|
|||||||
isNavigatingRef.current = false;
|
isNavigatingRef.current = false;
|
||||||
}
|
}
|
||||||
previousPathnameRef.current = pathname;
|
previousPathnameRef.current = pathname;
|
||||||
}, [pathname]);
|
}, [pathname, searchParams]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user