Skip to content

Commit

Permalink
Async Storage Cleanup (#46586)
Browse files Browse the repository at this point in the history
<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:
-->

Cleaned up duplicate code around async storage. Also fixed some types:

- Changed `supportsDynamicHTML: boolean`
- Changed `revalidate: false | number | undefined`
  • Loading branch information
wyattjoh committed Mar 1, 2023
1 parent 9fdddb3 commit 0c923c3
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getHandle({ page, mod }: any) {
extendedReq,
extendedRes,
// TODO: pass incrementalCache here
{},
{ supportsDynamicHTML: true },
request
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface StaticGenerationStore {
readonly isRevalidate?: boolean

forceDynamic?: boolean
revalidate?: boolean | number
revalidate?: false | number
forceStatic?: boolean
dynamicShouldError?: boolean
pendingRevalidates?: Promise<any>[]
Expand Down
10 changes: 5 additions & 5 deletions packages/next/src/server/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import type { StaticGenerationAsyncStorage } from '../client/components/static-g
import type { RequestAsyncStorage } from '../client/components/request-async-storage'
import { formatServerError } from '../lib/format-server-error'
import { MetadataTree } from '../lib/metadata/metadata'
import { runWithRequestAsyncStorage } from './run-with-request-async-storage'
import { runWithStaticGenerationAsyncStorage } from './run-with-static-generation-async-storage'
import { RequestAsyncStorageWrapper } from './async-storage/request-async-storage-wrapper'
import { StaticGenerationAsyncStorageWrapper } from './async-storage/static-generation-async-storage-wrapper'
import { collectMetadata } from '../lib/metadata/resolve-metadata'
import type { MetadataItems } from '../lib/metadata/resolve-metadata'
import { isClientReference } from '../build/is-client-reference'
Expand Down Expand Up @@ -184,7 +184,7 @@ export type RenderOptsPartial = {
dev?: boolean
serverComponentManifest?: FlightManifest
serverCSSManifest?: FlightCSSManifest
supportsDynamicHTML?: boolean
supportsDynamicHTML: boolean
runtime?: ServerRuntime
serverComponents?: boolean
assetPrefix?: string
Expand Down Expand Up @@ -2001,11 +2001,11 @@ export async function renderToHTMLOrFlight(
return renderResult
}

return runWithRequestAsyncStorage(
return RequestAsyncStorageWrapper.wrap(
requestAsyncStorage,
{ req, res, renderOpts },
() =>
runWithStaticGenerationAsyncStorage(
StaticGenerationAsyncStorageWrapper.wrap(
staticGenerationAsyncStorage,
{ pathname, renderOpts },
() => wrappedRender()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type RequestContext = {
pathname: string
renderOpts: {
incrementalCache?: IncrementalCache
supportsDynamicHTML?: boolean
supportsDynamicHTML: boolean
isRevalidate?: boolean
isBot?: boolean
}
Expand Down Expand Up @@ -45,11 +45,8 @@ export class StaticGenerationAsyncStorageWrapper
* These rules help ensure that other existing features like request caching,
* coalescing, and ISR continue working as intended.
*/
const supportsDynamicHTML =
typeof renderOpts.supportsDynamicHTML !== 'boolean' ||
renderOpts.supportsDynamicHTML

const isStaticGeneration = !supportsDynamicHTML && !renderOpts.isBot
const isStaticGeneration =
!renderOpts.supportsDynamicHTML && !renderOpts.isBot

const store: StaticGenerationStore = {
isStaticGeneration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type AppRouteModule = {

export type StaticGenerationContext = {
incrementalCache?: IncrementalCache
supportsDynamicHTML?: boolean
supportsDynamicHTML: boolean
}

/**
Expand Down Expand Up @@ -309,23 +309,25 @@ export class AppRouteRouteHandler implements RouteHandler<AppRouteRouteMatch> {
staticGenerationAsyncStorage,
{
pathname: definition.pathname,
renderOpts: context || {},
renderOpts: context ?? {
supportsDynamicHTML: false,
},
},
() => {
const _req = (request ? request : wrapRequest(req)) as NextRequest

// We can currently only statically optimize if only GET/HEAD
// are used as a Prerender can't be used conditionally based
// on the method currently
const nonStaticHandlers = [
const nonStaticHandlers: ReadonlyArray<HTTP_METHOD> = [
'OPTIONS',
'POST',
'PUT',
'DELETE',
'PATCH',
]
const usedNonStaticHandlers = nonStaticHandlers.filter(
(name) => !!(module.handlers as any)[name]
(name) => name in module.handlers
)

if (usedNonStaticHandlers.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export type RenderOptsPartial = {
defaultLocale?: string
domainLocales?: DomainLocale[]
disableOptimizedLoading?: boolean
supportsDynamicHTML?: boolean
supportsDynamicHTML: boolean
isBot?: boolean
runtime?: ServerRuntime
serverComponents?: boolean
Expand Down
77 changes: 0 additions & 77 deletions packages/next/src/server/run-with-request-async-storage.ts

This file was deleted.

This file was deleted.

0 comments on commit 0c923c3

Please sign in to comment.