|
| 1 | +import type { ParsedUrlQuery } from 'node:querystring' |
| 2 | +import React, { useMemo, useRef } from 'react' |
| 3 | +import type { AppRouterInstance } from '../app-router-context' |
| 4 | +import { PathnameContext } from '../hooks-client-context' |
| 5 | +import type { NextRouter } from './router' |
| 6 | +import { isDynamicRoute } from './utils' |
| 7 | + |
| 8 | +/** |
| 9 | + * adaptForAppRouterInstance implements the AppRouterInstance with a NextRouter. |
| 10 | + * |
| 11 | + * @param router the NextRouter to adapt |
| 12 | + * @returns an AppRouterInstance |
| 13 | + */ |
| 14 | +export function adaptForAppRouterInstance( |
| 15 | + router: NextRouter |
| 16 | +): AppRouterInstance { |
| 17 | + return { |
| 18 | + back(): void { |
| 19 | + router.back() |
| 20 | + }, |
| 21 | + forward(): void { |
| 22 | + router.forward() |
| 23 | + }, |
| 24 | + refresh(): void { |
| 25 | + router.reload() |
| 26 | + }, |
| 27 | + push(href: string): void { |
| 28 | + void router.push(href) |
| 29 | + }, |
| 30 | + replace(href: string): void { |
| 31 | + void router.replace(href) |
| 32 | + }, |
| 33 | + prefetch(href: string): void { |
| 34 | + void router.prefetch(href) |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * transforms the ParsedUrlQuery into a URLSearchParams. |
| 41 | + * |
| 42 | + * @param query the query to transform |
| 43 | + * @returns URLSearchParams |
| 44 | + */ |
| 45 | +function transformQuery(query: ParsedUrlQuery): URLSearchParams { |
| 46 | + const params = new URLSearchParams() |
| 47 | + |
| 48 | + for (const [name, value] of Object.entries(query)) { |
| 49 | + if (Array.isArray(value)) { |
| 50 | + for (const val of value) { |
| 51 | + params.append(name, val) |
| 52 | + } |
| 53 | + } else if (typeof value !== 'undefined') { |
| 54 | + params.append(name, value) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return params |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * adaptForSearchParams transforms the ParsedURLQuery into URLSearchParams. |
| 63 | + * |
| 64 | + * @param router the router that contains the query. |
| 65 | + * @returns the search params in the URLSearchParams format |
| 66 | + */ |
| 67 | +export function adaptForSearchParams( |
| 68 | + router: Pick<NextRouter, 'isReady' | 'query'> |
| 69 | +): URLSearchParams { |
| 70 | + if (!router.isReady || !router.query) { |
| 71 | + return new URLSearchParams() |
| 72 | + } |
| 73 | + |
| 74 | + return transformQuery(router.query) |
| 75 | +} |
| 76 | + |
| 77 | +export function PathnameContextProviderAdapter({ |
| 78 | + children, |
| 79 | + router, |
| 80 | + ...props |
| 81 | +}: React.PropsWithChildren<{ |
| 82 | + router: Pick<NextRouter, 'pathname' | 'asPath' | 'isReady' | 'isFallback'> |
| 83 | + isAutoExport: boolean |
| 84 | +}>) { |
| 85 | + const ref = useRef(props.isAutoExport) |
| 86 | + const value = useMemo(() => { |
| 87 | + // isAutoExport is only ever `true` on the first render from the server, |
| 88 | + // so reset it to `false` after we read it for the first time as `true`. If |
| 89 | + // we don't use the value, then we don't need it. |
| 90 | + const isAutoExport = ref.current |
| 91 | + if (isAutoExport) { |
| 92 | + ref.current = false |
| 93 | + } |
| 94 | + |
| 95 | + // When the route is a dynamic route, we need to do more processing to |
| 96 | + // determine if we need to stop showing the pathname. |
| 97 | + if (isDynamicRoute(router.pathname)) { |
| 98 | + // When the router is rendering the fallback page, it can't possibly know |
| 99 | + // the path, so return `null` here. Read more about fallback pages over |
| 100 | + // at: |
| 101 | + // https://nextjs.org/docs/api-reference/data-fetching/get-static-paths#fallback-pages |
| 102 | + if (router.isFallback) { |
| 103 | + return null |
| 104 | + } |
| 105 | + |
| 106 | + // When `isAutoExport` is true, meaning this is a page page has been |
| 107 | + // automatically statically optimized, and the router is not ready, then |
| 108 | + // we can't know the pathname yet. Read more about automatic static |
| 109 | + // optimization at: |
| 110 | + // https://nextjs.org/docs/advanced-features/automatic-static-optimization |
| 111 | + if (isAutoExport && !router.isReady) { |
| 112 | + return null |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // The `router.asPath` contains the pathname seen by the browser (including |
| 117 | + // any query strings), so it should have that stripped. Read more about the |
| 118 | + // `asPath` option over at: |
| 119 | + // https://nextjs.org/docs/api-reference/next/router#router-object |
| 120 | + const url = new URL(router.asPath, 'http://f') |
| 121 | + return url.pathname |
| 122 | + }, [router.asPath, router.isFallback, router.isReady, router.pathname]) |
| 123 | + |
| 124 | + return ( |
| 125 | + <PathnameContext.Provider value={value}> |
| 126 | + {children} |
| 127 | + </PathnameContext.Provider> |
| 128 | + ) |
| 129 | +} |
0 commit comments