File size: 2,363 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { z } from 'next/dist/compiled/zod'
import { formatZodError } from '../../../shared/lib/zod'

/**
 * The schema for the page segment config.
 */
const PagesSegmentConfigSchema = z.object({
  /**
   * The runtime to use for the page.
   */
  runtime: z.enum(['edge', 'experimental-edge', 'nodejs']).optional(),

  /**
   * The maximum duration for the page render.
   */
  maxDuration: z.number().optional(),

  /**
   * The exported config object for the page.
   */
  config: z
    .object({
      /**
       * Enables AMP for the page.
       */
      amp: z.union([z.boolean(), z.literal('hybrid')]).optional(),

      /**
       * The runtime to use for the page.
       */
      runtime: z.enum(['edge', 'experimental-edge', 'nodejs']).optional(),

      /**
       * The maximum duration for the page render.
       */
      maxDuration: z.number().optional(),
    })
    .optional(),
})

/**
 * Parse the page segment config.
 * @param data - The data to parse.
 * @param route - The route of the page.
 * @returns The parsed page segment config.
 */
export function parsePagesSegmentConfig(
  data: unknown,
  route: string
): PagesSegmentConfig {
  const parsed = PagesSegmentConfigSchema.safeParse(data, {})
  if (!parsed.success) {
    throw formatZodError(
      `Invalid segment configuration options detected for "${route}". Read more at https://nextjs.org/docs/messages/invalid-page-config`,
      parsed.error
    )
  }

  return parsed.data
}

/**
 * The keys of the configuration for a page.
 *
 * @internal - required to exclude zod types from the build
 */
export const PagesSegmentConfigSchemaKeys =
  PagesSegmentConfigSchema.keyof().options

export type PagesSegmentConfigConfig = {
  /**
   * Enables AMP for the page.
   */
  amp?: boolean | 'hybrid'

  /**
   * The maximum duration for the page render.
   */
  maxDuration?: number

  /**
   * The runtime to use for the page.
   */
  runtime?: 'edge' | 'experimental-edge' | 'nodejs'

  /**
   * The preferred region for the page.
   */
  regions?: string[]
}

export type PagesSegmentConfig = {
  /**
   * The runtime to use for the page.
   */
  runtime?: 'edge' | 'experimental-edge' | 'nodejs'

  /**
   * The maximum duration for the page render.
   */
  maxDuration?: number

  /**
   * The exported config object for the page.
   */
  config?: PagesSegmentConfigConfig
}