Skip to content

Commit

Permalink
refactor and extract util
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jul 15, 2024
1 parent e296664 commit 06110c7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { internal_getCurrentFunctionWaitUntil } from '../../../../server/web/int
import type { PAGE_TYPES } from '../../../../lib/page-types'
import type { NextRequestHint } from '../../../../server/web/adapter'
import type { DeepReadonly } from '../../../../shared/lib/deep-readonly'
import { getEdgePreviewProps } from '../../../../server/web/get-edge-preview-props'

export function getRender({
dev,
Expand Down Expand Up @@ -91,16 +92,7 @@ export function getRender({
prerenderManifest: prerenderManifest
? {
...prerenderManifest,
preview: {
previewModeId:
process.env.NODE_ENV === 'production'
? process.env.__NEXT_PREVIEW_MODE_ID!
: 'development-id',
previewModeSigningKey:
process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY!,
previewModeEncryptionKey:
process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY!,
},
preview: getEdgePreviewProps(),
}
: undefined,
interceptionRouteRewrites,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class DraftModeProvider {
cookieValue &&
previewProps &&
(cookieValue === previewProps.previewModeId ||
// In dev mode, the cookie can be actual hash value preview id but the preview props can still be `development-id`.
(process.env.NODE_ENV !== 'production' &&
previewProps.previewModeId === 'development-id'))
)
Expand Down
12 changes: 2 additions & 10 deletions packages/next/src/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { UNDERSCORE_NOT_FOUND_ROUTE } from '../api/constants'
import type { DeepReadonly } from '../shared/lib/deep-readonly'
import { getEdgeInstrumentationModule } from './web/globals'
import type { ServerOnInstrumentationRequestError } from './app-render/types'
import { getEdgePreviewProps } from './web/get-edge-preview-props'

interface WebServerOptions extends Options {
webServerConfig: {
Expand Down Expand Up @@ -146,16 +147,7 @@ export default class NextWebServer extends BaseServer<
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: {
previewModeId:
process.env.NODE_ENV === 'production'
? process.env.__NEXT_PREVIEW_MODE_ID!
: 'development-id',
previewModeSigningKey:
process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY || '',
previewModeEncryptionKey:
process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY || '',
} as any, // `preview` is special case read in next-dev-server
preview: getEdgePreviewProps(),
}
}
return prerenderManifest
Expand Down
24 changes: 3 additions & 21 deletions packages/next/src/server/web/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getTracer } from '../lib/trace/tracer'
import type { TextMapGetter } from 'next/dist/compiled/@opentelemetry/api'
import { MiddlewareSpan } from '../lib/trace/constants'
import { CloseController } from './web-on-close'
import { getEdgePreviewProps } from './get-edge-preview-props'

export class NextRequestHint extends NextRequest {
sourcePage: string
Expand Down Expand Up @@ -191,16 +192,7 @@ export async function adapter(
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: {
previewModeId:
process.env.NODE_ENV === 'production'
? process.env.__NEXT_PREVIEW_MODE_ID!
: 'development-id',
previewModeSigningKey:
process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY || '',
previewModeEncryptionKey:
process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY || '',
} as any, // `preview` is special case read in next-dev-server
preview: getEdgePreviewProps(),
}
},
})
Expand Down Expand Up @@ -242,17 +234,7 @@ export async function adapter(
},
async () => {
try {
// For edge runtime, preview props are injected through the environment variables
const previewProps = {
previewModeId:
process.env.NODE_ENV === 'production'
? process.env.__NEXT_PREVIEW_MODE_ID!
: 'development-id',
previewModeSigningKey:
process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY || '',
previewModeEncryptionKey:
process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY || '',
}
const previewProps = getEdgePreviewProps()

return await withRequestStore(
requestAsyncStorage,
Expand Down
11 changes: 2 additions & 9 deletions packages/next/src/server/web/edge-route-module-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getUtils } from '../server-utils'
import { searchParamsToUrlQuery } from '../../shared/lib/router/utils/querystring'
import type { RequestLifecycleOpts } from '../base-server'
import { CloseController, trackStreamConsumed } from './web-on-close'
import { getEdgePreviewProps } from './get-edge-preview-props'

type WrapOptions = Partial<Pick<AdapterOptions, 'page'>>

Expand Down Expand Up @@ -93,15 +94,7 @@ export class EdgeRouteModuleWrapper {
closeController = new CloseController()
}

const previewProps = {
previewModeId:
process.env.NODE_ENV === 'production'
? process.env.__NEXT_PREVIEW_MODE_ID!
: 'development-id',
previewModeSigningKey: process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY || '',
previewModeEncryptionKey:
process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY || '',
}
const previewProps = getEdgePreviewProps()

// Create the context for the handler. This contains the params from the
// match (if any).
Expand Down
16 changes: 16 additions & 0 deletions packages/next/src/server/web/get-edge-preview-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* In edge runtime, these props directly accessed from environment variables.
* - local: env vars will be injected through edge-runtime as runtime env vars
* - deployment: env vars will be replaced by edge build pipeline
*/
export function getEdgePreviewProps() {
return {
previewModeId:
process.env.NODE_ENV === 'production'
? process.env.__NEXT_PREVIEW_MODE_ID!
: 'development-id',
previewModeSigningKey: process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY || '',
previewModeEncryptionKey:
process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY || '',
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'

describe('app-dir - draft-mode-middleware', () => {
const { next } = nextTestSetup({
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})

if (skipped) {
return
}

it('should be able to enable draft mode with middleware present', async () => {
const browser = await next.browser(
'/api/draft?secret=secret-token&slug=preview-page'
Expand Down

0 comments on commit 06110c7

Please sign in to comment.