Skip to content

Commit

Permalink
fix: simplify dynamic server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Apr 26, 2024
1 parent 845bdeb commit 375d2ec
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 47 deletions.
6 changes: 2 additions & 4 deletions packages/next/src/build/templates/app-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,18 @@ const routeModule = new AppRouteRouteModule({
// Pull out the exports that we need to expose from the module. This should
// be eliminated when we've moved the other routes to the new format. These
// are used to hook into the route.
const { requestAsyncStorage, staticGenerationAsyncStorage, serverHooks } =
routeModule
const { requestAsyncStorage, staticGenerationAsyncStorage } = routeModule

const originalPathname = 'VAR_ORIGINAL_PATHNAME'

function patchFetch() {
return _patchFetch({ serverHooks, staticGenerationAsyncStorage })
return _patchFetch({ staticGenerationAsyncStorage })
}

export {
routeModule,
requestAsyncStorage,
staticGenerationAsyncStorage,
serverHooks,
originalPathname,
patchFetch,
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getTracer } from '../lib/trace/tracer'
import { NextNodeServerSpan } from '../lib/trace/constants'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import type { LoadingModuleData } from '../../shared/lib/app-router-context.shared-runtime'
import { DynamicServerError } from '../../client/components/hooks-server-context'

type ComponentTree = {
seedData: CacheNodeSeedData
Expand Down Expand Up @@ -88,7 +89,6 @@ async function createComponentTreeInternal({
ClientPageRoot,
createUntrackedSearchParams,
createDynamicallyTrackedSearchParams,
serverHooks: { DynamicServerError },
Postpone,
},
pagePath,
Expand Down
17 changes: 15 additions & 2 deletions packages/next/src/server/app-render/dynamic-rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,22 @@ export function trackDynamicFetch(
) {
// If we aren't in a prerender, or we're in an unstable cache callback, we
// don't need to postpone.
if (!store.prerenderState || store.isUnstableCacheCallback) return
if (store.isUnstableCacheCallback) return

postponeWithTracking(store.prerenderState, expression, store.urlPathname)
// If we're in a prerender, let's postpone.
if (store.prerenderState) {
postponeWithTracking(store.prerenderState, expression, store.urlPathname)
}

// Otherwise, we need to throw a dynamic usage error, and mark that the
// current store's revalidation time is `0` to indicate that this route is
// dynamic.
store.revalidate = 0

const err = new DynamicServerError(expression)
store.dynamicUsageErr = err
store.dynamicUsageDescription = expression
throw err
}

function postponeWithTracking(
Expand Down
4 changes: 1 addition & 3 deletions packages/next/src/server/app-render/entry-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
createUntrackedSearchParams,
createDynamicallyTrackedSearchParams,
} from '../../client/components/search-params'
import * as serverHooks from '../../client/components/hooks-server-context'
import { NotFoundBoundary } from '../../client/components/not-found-boundary'
import { patchFetch as _patchFetch } from '../lib/patch-fetch'
// not being used but needs to be included in the client manifest for /_not-found
Expand All @@ -34,7 +33,7 @@ import { taintObjectReference } from '../../server/app-render/rsc/taint'
// patchFetch makes use of APIs such as `React.unstable_postpone` which are only available
// in the experimental channel of React, so export it from here so that it comes from the bundled runtime
function patchFetch() {
return _patchFetch({ serverHooks, staticGenerationAsyncStorage })
return _patchFetch({ staticGenerationAsyncStorage })
}

export {
Expand All @@ -46,7 +45,6 @@ export {
actionAsyncStorage,
createUntrackedSearchParams,
createDynamicallyTrackedSearchParams,
serverHooks,
preloadStyle,
preloadFont,
preconnect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { HeadersAdapter } from '../../../web/spec-extension/adapters/headers'
import { RequestCookiesAdapter } from '../../../web/spec-extension/adapters/request-cookies'
import { parsedUrlQueryToParams } from './helpers/parsed-url-query-to-params'

import * as serverHooks from '../../../../client/components/hooks-server-context'
import { DynamicServerError } from '../../../../client/components/hooks-server-context'

import { requestAsyncStorage } from '../../../../client/components/request-async-storage.external'
Expand Down Expand Up @@ -136,12 +135,6 @@ export class AppRouteRouteModule extends RouteModule<
*/
public readonly staticGenerationAsyncStorage = staticGenerationAsyncStorage

/**
* An interface to call server hooks which interact with the underlying
* storage.
*/
public readonly serverHooks = serverHooks

public static readonly sharedModules = sharedModules

/**
Expand Down Expand Up @@ -378,7 +371,6 @@ export class AppRouteRouteModule extends RouteModule<
async () => {
// Patch the global fetch.
patchFetch({
serverHooks: this.serverHooks,
staticGenerationAsyncStorage:
this.staticGenerationAsyncStorage,
})
Expand Down
30 changes: 1 addition & 29 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
StaticGenerationAsyncStorage,
StaticGenerationStore,
} from '../../client/components/static-generation-async-storage.external'
import type * as ServerHooks from '../../client/components/hooks-server-context'

import { AppRenderSpan, NextNodeServerSpan } from './trace/constants'
import { getTracer, SpanKind } from './trace/tracer'
Expand Down Expand Up @@ -209,16 +208,12 @@ function trackFetchMetric(
}

interface PatchableModule {
serverHooks: typeof ServerHooks
staticGenerationAsyncStorage: StaticGenerationAsyncStorage
}

function createPatchedFetcher(
originFetch: Fetcher,
{
serverHooks: { DynamicServerError },
staticGenerationAsyncStorage,
}: PatchableModule
{ staticGenerationAsyncStorage }: PatchableModule
): PatchedFetcher {
// Create the patched fetch function. We don't set the type here, as it's
// verified as the return value of this function.
Expand Down Expand Up @@ -693,19 +688,6 @@ function createPatchedFetcher(

// If enabled, we should bail out of static generation.
trackDynamicFetch(staticGenerationStore, dynamicUsageReason)

// If partial prerendering is not enabled, then we should throw an
// error to indicate that this fetch is dynamic.
if (!staticGenerationStore.prerenderState) {
// PPR is not enabled, or React postpone is not available, we
// should set the revalidate to 0.
staticGenerationStore.revalidate = 0

const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
}
}

const hasNextConfig = 'next' in init
Expand All @@ -729,16 +711,6 @@ function createPatchedFetcher(

// If enabled, we should bail out of static generation.
trackDynamicFetch(staticGenerationStore, dynamicUsageReason)

// If partial prerendering is not enabled, then we should throw an
// error to indicate that this fetch is dynamic.
if (!staticGenerationStore.prerenderState) {
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription =
dynamicUsageReason
throw err
}
}

if (!staticGenerationStore.forceStatic || next.revalidate !== 0) {
Expand Down

0 comments on commit 375d2ec

Please sign in to comment.