Unverified Commit 5740ef3e authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

refactor(ts): type `fastRefresh` (#62848)

parent 5f3b1358
......@@ -391,7 +391,6 @@ function Router({
})
})
},
// @ts-ignore we don't want to expose this method at all
fastRefresh: () => {
if (process.env.NODE_ENV !== 'development') {
throw new Error(
......
......@@ -410,7 +410,6 @@ function processMessage(
return window.location.reload()
}
startTransition(() => {
// @ts-ignore it exists, it's just hidden
router.fastRefresh()
dispatcher.onRefresh()
})
......@@ -435,17 +434,10 @@ function processMessage(
reloading = true
return window.location.reload()
}
case HMR_ACTIONS_SENT_TO_BROWSER.ADDED_PAGE:
case HMR_ACTIONS_SENT_TO_BROWSER.REMOVED_PAGE: {
// TODO-APP: potentially only refresh if the currently viewed page was removed.
// @ts-ignore it exists, it's just hidden
router.fastRefresh()
return
}
case HMR_ACTIONS_SENT_TO_BROWSER.ADDED_PAGE: {
// TODO-APP: potentially only refresh if the currently viewed page was added.
// @ts-ignore it exists, it's just hidden
router.fastRefresh()
return
// TODO-APP: potentially only refresh if the currently viewed page was added/removed.
return router.fastRefresh()
}
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR: {
const { errorJSON } = obj
......@@ -476,7 +468,7 @@ export default function HotReload({
errorOverlayReducer,
INITIAL_OVERLAY_STATE
)
const dispatcher = useMemo((): Dispatcher => {
const dispatcher = useMemo<Dispatcher>(() => {
return {
onBuildOk() {
dispatch({ type: ACTION_BUILD_OK })
......@@ -494,7 +486,7 @@ export default function HotReload({
dispatch({ type: ACTION_VERSION_INFO, versionInfo })
},
}
}, [dispatch])
}, [])
const handleOnUnhandledError = useCallback((error: Error): void => {
const errorDetails = (error as any).details as
......@@ -533,6 +525,9 @@ export default function HotReload({
const router = useRouter()
useEffect(() => {
const websocket = webSocketRef.current
if (!websocket) return
const handler = (event: MessageEvent<any>) => {
try {
const obj = JSON.parse(event.data)
......@@ -550,12 +545,8 @@ export default function HotReload({
}
}
const websocket = webSocketRef.current
if (websocket) {
websocket.addEventListener('message', handler)
}
return () => websocket && websocket.removeEventListener('message', handler)
websocket.addEventListener('message', handler)
return () => websocket.removeEventListener('message', handler)
}, [sendMessage, router, webSocketRef, dispatcher, processTurbopackMessage])
return (
......
......@@ -125,6 +125,11 @@ export interface AppRouterInstance {
* Refresh the current page.
*/
refresh(): void
/**
* Refresh the current page. Use in development only.
* @internal
*/
fastRefresh(): void
/**
* Navigate to the provided href.
* Pushes a new history entry.
......
import type {
AppRouterInstance,
NavigateOptions,
} from '../app-router-context.shared-runtime'
import type { AppRouterInstance } from '../app-router-context.shared-runtime'
import type { Params } from './utils/route-matcher'
import type { NextRouter } from './router'
......@@ -11,33 +8,29 @@ import { isDynamicRoute } from './utils'
import { asPathToSearchParams } from './utils/as-path-to-search-params'
import { getRouteRegex } from './utils/route-regex'
/**
* adaptForAppRouterInstance implements the AppRouterInstance with a NextRouter.
*
* @param router the NextRouter to adapt
* @returns an AppRouterInstance
*/
/** It adapts a Pages Router (`NextRouter`) to the App Router Instance. */
export function adaptForAppRouterInstance(
router: NextRouter
pagesRouter: NextRouter
): AppRouterInstance {
return {
back(): void {
router.back()
back() {
pagesRouter.back()
},
forward(): void {
router.forward()
forward() {
pagesRouter.forward()
},
refresh(): void {
router.reload()
refresh() {
pagesRouter.reload()
},
push(href: string, { scroll }: NavigateOptions = {}): void {
void router.push(href, undefined, { scroll })
fastRefresh() {},
push(href, { scroll } = {}) {
void pagesRouter.push(href, undefined, { scroll })
},
replace(href: string, { scroll }: NavigateOptions = {}): void {
void router.replace(href, undefined, { scroll })
replace(href, { scroll } = {}) {
void pagesRouter.replace(href, undefined, { scroll })
},
prefetch(href: string): void {
void router.prefetch(href)
prefetch(href) {
void pagesRouter.prefetch(href)
},
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment