diff --git a/packages/next/client/components/app-router.tsx b/packages/next/client/components/app-router.tsx index 981b3045b3a386d..432abe3a14e71b3 100644 --- a/packages/next/client/components/app-router.tsx +++ b/packages/next/client/components/app-router.tsx @@ -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) @@ -98,7 +97,6 @@ const prefetched = new Set() type AppRouterProps = { initialTree: FlightRouterState initialCanonicalUrl?: string - isStaticGeneration: boolean children: ReactNode assetPrefix: string } @@ -108,10 +106,9 @@ type AppRouterProps = { */ function Router({ initialTree, + initialCanonicalUrl, children, assetPrefix, - initialCanonicalUrl, - isStaticGeneration, }: AppRouterProps) { const initialState = useMemo(() => { return { @@ -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, @@ -345,39 +342,37 @@ function Router({ }, [onPopState]) return ( - - - - - - - {HotReloader ? ( - - {cache.subTreeData} - - ) : ( - cache.subTreeData - )} - - - - - - + + + + + + {HotReloader ? ( + + {cache.subTreeData} + + ) : ( + cache.subTreeData + )} + + + + + ) } diff --git a/packages/next/client/components/bailout-to-client-rendering.ts b/packages/next/client/components/bailout-to-client-rendering.ts deleted file mode 100644 index 534e393def14fa1..000000000000000 --- a/packages/next/client/components/bailout-to-client-rendering.ts +++ /dev/null @@ -1,25 +0,0 @@ -import React from 'react' -import { StaticGenerationContext } from './static-generation-context' - -export const BAILOUT_TO_CLIENT_RENDERING_ERROR_CODE = - 'BAILOUT_TO_CLIENT_RENDERING' - -export class BailoutToClientRenderingError extends Error { - digest: typeof BAILOUT_TO_CLIENT_RENDERING_ERROR_CODE = - BAILOUT_TO_CLIENT_RENDERING_ERROR_CODE - - constructor(reason: string) { - super(`Reason: ${reason}`) - } -} - -export function useBailoutToClientRendering(reason: string) { - const isStaticGeneration = React.useContext( - StaticGenerationContext - )?.isStaticGeneration - - // Should only throw during SSG on the server - if (isStaticGeneration && typeof window === 'undefined') { - throw new BailoutToClientRenderingError(reason) - } -} diff --git a/packages/next/client/components/headers.ts b/packages/next/client/components/headers.ts index 63a906e03f5e549..ce2924d66323820 100644 --- a/packages/next/client/components/headers.ts +++ b/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') diff --git a/packages/next/client/components/navigation.ts b/packages/next/client/components/navigation.ts index b0425a087bda45a..038c2f4021c2612 100644 --- a/packages/next/client/components/navigation.ts +++ b/packages/next/client/components/navigation.ts @@ -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, @@ -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) @@ -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) } diff --git a/packages/next/client/components/static-generation-bailout.ts b/packages/next/client/components/static-generation-bailout.ts new file mode 100644 index 000000000000000..d28fc10a42e6fa9 --- /dev/null +++ b/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) + } +} diff --git a/packages/next/client/components/static-generation-context.ts b/packages/next/client/components/static-generation-context.ts deleted file mode 100644 index 6112381b395ca16..000000000000000 --- a/packages/next/client/components/static-generation-context.ts +++ /dev/null @@ -1,9 +0,0 @@ -import React from 'react' - -export const StaticGenerationContext: React.Context<{ - isStaticGeneration: boolean -}> = React.createContext(null as any) - -if (process.env.NODE_ENV !== 'production') { - StaticGenerationContext.displayName = 'StaticGenerationContext' -} diff --git a/packages/next/export/worker.ts b/packages/next/export/worker.ts index 7214819a1aed9ee..93c265b698eb088 100644 --- a/packages/next/export/worker.ts +++ b/packages/next/export/worker.ts @@ -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') @@ -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 diff --git a/packages/next/server/app-render.tsx b/packages/next/server/app-render.tsx index dc9ec15ba3b2d74..47f3fa506ad47a5 100644 --- a/packages/next/server/app-render.tsx +++ b/packages/next/server/app-render.tsx @@ -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') @@ -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 @@ -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 } @@ -1372,7 +1372,6 @@ export async function renderToHTMLOrFlight( isStaticGeneration ? undefined : initialCanonicalUrl } initialTree={initialTree} - isStaticGeneration={isStaticGeneration} >