{"record":{"id":"e3470805cbf3a612","repo":"vercel/next.js","slug":"draft-mode-cannot-be-enabled-during-build-time-ins","errorCode":null,"errorMessage":"Draft mode cannot be enabled during build-time instant validation.","messagePattern":"Draft mode cannot be enabled during build-time instant validation\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/next/src/server/app-render/instant-validation/instant-samples.ts","lineNumber":239,"sourceCode":"      return Reflect.get(target, prop, receiver)\n    },\n  })\n}\n\n/**\n * Creates a DraftModeProvider that always returns isEnabled: false.\n */\nexport function createDraftModeForValidation(): DraftModeProvider {\n  // Create a minimal DraftModeProvider-compatible object\n  // that always reports draft mode as disabled.\n  //\n  // private properties that can't be set from outside the class.\n  return {\n    get isEnabled() {\n      return false\n    },\n    enable() {\n      throw new Error(\n        'Draft mode cannot be enabled during build-time instant validation.'\n      )\n    },\n    disable() {\n      throw new Error(\n        'Draft mode cannot be disabled during build-time instant validation.'\n      )\n    },\n  } as Partial<DraftModeProvider> as DraftModeProvider\n}\n\n/**\n * Creates params wrapped with an exhaustive proxy.\n * Accessing a param not declared in the sample will throw an error.\n */\nexport function createExhaustiveParamsProxy<TParams extends Params>(\n  underlyingParams: TParams,\n  declaredParamNames: Set<string>,","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/vercel/next.js/blob/0ae8c72462952df163f1b1e0726641bc5b40dc93/packages/next/src/server/app-render/instant-validation/instant-samples.ts#L221-L257","documentation":"Thrown by the build-time instant-validation DraftModeProvider.enable() stub. During instant validation, draft mode is intentionally a no-op that always reports disabled; calling enable() is invalid because there is no live request to establish a draft-mode cookie. This prevents components from mutating draft state during static validation.","triggerScenarios":"During instant validation render, application code calls draftMode().enable() (e.g. in a preview/preview-API-like flow). The validation DraftModeProvider.enable() throws immediately.","commonSituations":"A route handler or component that enables draft mode (e.g. an API route for preview) is exercised during instant validation; shared code unconditionally enables draft mode; enabling preview in build-time validation where no request exists.","solutions":["Guard draftMode().enable() so it is not invoked during build/validation -- detect the validation context and skip it.","Move draft-mode enablement into a runtime-only route handler that is excluded from instant validation.","Conditionally enable draft mode only when a real request/preview token is present.","Ensure preview-enabling code lives in an API route excluded from static validation samples."],"exampleFix":"// before\n// export default function Handler() { draftMode().enable() }\n\n// after: skip in build/validation context\n// if (process.env.NEXT_RUNTIME) draftMode().enable() // runtime only","handlingStrategy":"validation","validationCode":"// Skip draft-mode enablement during build/validation.\nfunction enablePreview() {\n  if (process.env.NEXT_PHASE === 'phase-production-build') return\n  draftMode().enable()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep draftMode().enable() in runtime-only route handlers.","Exclude preview routes from instant validation samples.","Guard draft-mode calls with a runtime/phase check.","Don't call draft APIs from statically validated components."],"tags":["instant-validation","draft-mode","build","validation"],"analyzedSha":"0ae8c72462952df163f1b1e0726641bc5b40dc93","analyzedAt":"2026-08-06T19:44:29.143Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}