Skip to content

Commit

Permalink
Remove context
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannes Bornö committed Oct 18, 2022
1 parent de4ec38 commit 372a814
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 105 deletions.
69 changes: 32 additions & 37 deletions packages/next/client/components/app-router.tsx
Expand Up @@ -29,7 +29,6 @@ import {
} from './hooks-client-context'
import { useReducerWithReduxDevtools } from './use-reducer-with-devtools'
import { ErrorBoundary, GlobalErrorComponent } from './error-boundary'
import { StaticGenerationContext } from './static-generation-context'

function urlToUrlWithoutFlightMarker(url: string): URL {
const urlWithoutFlightParameters = new URL(url, location.origin)
Expand Down Expand Up @@ -98,7 +97,6 @@ const prefetched = new Set<string>()
type AppRouterProps = {
initialTree: FlightRouterState
initialCanonicalUrl?: string
isStaticGeneration: boolean
children: ReactNode
assetPrefix: string
}
Expand All @@ -111,7 +109,6 @@ function Router({
children,
assetPrefix,
initialCanonicalUrl,
isStaticGeneration,
}: AppRouterProps) {
const initialState = useMemo(() => {
return {
Expand All @@ -135,7 +132,7 @@ function Router({
// This is safe to do as canonicalUrl can't be rendered, it's only used to control the history updates the useEffect further down.
(typeof window !== 'undefined' ? window.location.hash : ''),
}
}, [children, initialTree, initialCanonicalUrl])
}, [children, initialCanonicalUrl, initialTree])
const [
{ tree, cache, prefetchCache, pushRef, focusAndScrollRef, canonicalUrl },
dispatch,
Expand Down Expand Up @@ -345,39 +342,37 @@ function Router({
}, [onPopState])

return (
<StaticGenerationContext.Provider value={{ isStaticGeneration }}>
<PathnameContext.Provider value={pathname}>
<SearchParamsContext.Provider value={searchParams}>
<GlobalLayoutRouterContext.Provider
value={{
changeByServerResponse,
tree,
focusAndScrollRef,
}}
>
<AppRouterContext.Provider value={appRouter}>
<LayoutRouterContext.Provider
value={{
childNodes: cache.parallelRoutes,
tree: tree,
// Root node always has `url`
// Provided in AppTreeContext to ensure it can be overwritten in layout-router
url: canonicalUrl,
}}
>
{HotReloader ? (
<HotReloader assetPrefix={assetPrefix}>
{cache.subTreeData}
</HotReloader>
) : (
cache.subTreeData
)}
</LayoutRouterContext.Provider>
</AppRouterContext.Provider>
</GlobalLayoutRouterContext.Provider>
</SearchParamsContext.Provider>
</PathnameContext.Provider>
</StaticGenerationContext.Provider>
<PathnameContext.Provider value={pathname}>
<SearchParamsContext.Provider value={searchParams}>
<GlobalLayoutRouterContext.Provider
value={{
changeByServerResponse,
tree,
focusAndScrollRef,
}}
>
<AppRouterContext.Provider value={appRouter}>
<LayoutRouterContext.Provider
value={{
childNodes: cache.parallelRoutes,
tree: tree,
// Root node always has `url`
// Provided in AppTreeContext to ensure it can be overwritten in layout-router
url: canonicalUrl,
}}
>
{HotReloader ? (
<HotReloader assetPrefix={assetPrefix}>
{cache.subTreeData}
</HotReloader>
) : (
cache.subTreeData
)}
</LayoutRouterContext.Provider>
</AppRouterContext.Provider>
</GlobalLayoutRouterContext.Provider>
</SearchParamsContext.Provider>
</PathnameContext.Provider>
)
}

Expand Down
25 changes: 0 additions & 25 deletions packages/next/client/components/bailout-to-client-rendering.ts

This file was deleted.

18 changes: 1 addition & 17 deletions packages/next/client/components/headers.ts
@@ -1,21 +1,5 @@
import { DynamicServerError } from './hooks-server-context'
import { requestAsyncStorage } from './request-async-storage'
import { staticGenerationAsyncStorage } from './static-generation-async-storage'

function staticGenerationBailout(reason: string) {
const staticGenerationStore =
staticGenerationAsyncStorage && 'getStore' in staticGenerationAsyncStorage
? staticGenerationAsyncStorage?.getStore()
: staticGenerationAsyncStorage

if (staticGenerationStore?.isStaticGeneration) {
// TODO: honor the dynamic: 'force-static'
if (staticGenerationStore) {
staticGenerationStore.revalidate = 0
}
throw new DynamicServerError(reason)
}
}
import { staticGenerationBailout } from './static-generation-bailout'

export function headers(): Headers {
staticGenerationBailout('headers')
Expand Down
6 changes: 3 additions & 3 deletions packages/next/client/components/navigation.ts
Expand Up @@ -11,7 +11,7 @@ import {
AppRouterContext,
LayoutRouterContext,
} from '../../shared/lib/app-router-context'
import { useBailoutToClientRendering } from './bailout-to-client-rendering'
import { staticGenerationBailout } from './static-generation-bailout'

export {
ServerInsertedHTMLContext,
Expand Down Expand Up @@ -74,7 +74,7 @@ class ReadonlyURLSearchParams {
* Learn more about URLSearchParams here: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
*/
export function useSearchParams() {
useBailoutToClientRendering('useSearchParams')
staticGenerationBailout('useSearchParams')
const searchParams = useContext(SearchParamsContext)
const readonlySearchParams = useMemo(() => {
return new ReadonlyURLSearchParams(searchParams)
Expand All @@ -99,7 +99,7 @@ export function useRouter(): import('../../shared/lib/app-router-context').AppRo
* Get the current pathname. For example usePathname() on /dashboard?foo=bar would return "/dashboard"
*/
export function usePathname(): string {
useBailoutToClientRendering('usePathname')
staticGenerationBailout('usePathname')
return useContext(PathnameContext)
}

Expand Down
18 changes: 18 additions & 0 deletions packages/next/client/components/static-generation-bailout.ts
@@ -0,0 +1,18 @@
import { DynamicServerError } from './hooks-server-context'
import { staticGenerationAsyncStorage } from './static-generation-async-storage'

export function staticGenerationBailout(reason: string) {
const staticGenerationStore =
staticGenerationAsyncStorage && 'getStore' in staticGenerationAsyncStorage
? staticGenerationAsyncStorage?.getStore()
: staticGenerationAsyncStorage
console.log({ staticGenerationAsyncStorage, staticGenerationStore })

if (staticGenerationStore?.isStaticGeneration) {
// TODO: honor the dynamic: 'force-static'
if (staticGenerationStore) {
staticGenerationStore.revalidate = 0
}
throw new DynamicServerError(reason)
}
}
9 changes: 0 additions & 9 deletions packages/next/client/components/static-generation-context.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/next/export/worker.ts
Expand Up @@ -31,7 +31,6 @@ import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { REDIRECT_ERROR_CODE } from '../client/components/redirect'
import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context'
import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found'
import { BAILOUT_TO_CLIENT_RENDERING_ERROR_CODE } from '../client/components/bailout-to-client-rendering'

loadRequireHook()
const envConfig = require('../shared/lib/runtime-config')
Expand Down Expand Up @@ -427,7 +426,6 @@ export default async function exportPage({
if (
err.digest !== DYNAMIC_ERROR_CODE &&
err.digest !== NOT_FOUND_ERROR_CODE &&
err.digest !== BAILOUT_TO_CLIENT_RENDERING_ERROR_CODE &&
!err.digest?.startsWith(REDIRECT_ERROR_CODE)
) {
throw err
Expand Down
23 changes: 11 additions & 12 deletions packages/next/server/app-render.tsx
Expand Up @@ -35,7 +35,6 @@ import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context'
import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found'
import { HeadManagerContext } from '../shared/lib/head-manager-context'
import { Writable } from 'stream'
import { BAILOUT_TO_CLIENT_RENDERING_ERROR_CODE } from '../client/components/bailout-to-client-rendering'

const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly')

Expand Down Expand Up @@ -181,7 +180,6 @@ function createErrorHandler(
// TODO-APP: Handle redirect throw
err.digest !== DYNAMIC_ERROR_CODE &&
err.digest !== NOT_FOUND_ERROR_CODE &&
err.digest !== BAILOUT_TO_CLIENT_RENDERING_ERROR_CODE &&
!err.digest?.startsWith(REDIRECT_ERROR_CODE)
) {
// Used for debugging error source
Expand Down Expand Up @@ -860,15 +858,17 @@ export async function renderToHTMLOrFlight(
{},
]

segmentTree[1] = Object.keys(parallelRoutes).reduce(
(existingValue, currentValue) => {
existingValue[currentValue] = createFlightRouterStateFromLoaderTree(
parallelRoutes[currentValue]
)
return existingValue
},
{} as FlightRouterState[1]
)
if (parallelRoutes) {
segmentTree[1] = Object.keys(parallelRoutes).reduce(
(existingValue, currentValue) => {
existingValue[currentValue] = createFlightRouterStateFromLoaderTree(
parallelRoutes[currentValue]
)
return existingValue
},
{} as FlightRouterState[1]
)
}

return segmentTree
}
Expand Down Expand Up @@ -1372,7 +1372,6 @@ export async function renderToHTMLOrFlight(
isStaticGeneration ? undefined : initialCanonicalUrl
}
initialTree={initialTree}
isStaticGeneration={isStaticGeneration}
>
<ComponentTree />
</AppRouter>
Expand Down

0 comments on commit 372a814

Please sign in to comment.