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