File size: 5,822 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type webpack from 'webpack'
import type { SizeLimit } from '../../../../types'
import type { PagesRouteModuleOptions } from '../../../../server/route-modules/pages/module'
import type { MiddlewareConfig } from '../../../analysis/get-page-static-info'

import { getModuleBuildInfo } from '../get-module-build-info'
import { WEBPACK_RESOURCE_QUERIES } from '../../../../lib/constants'
import { RouteKind } from '../../../../server/route-kind'
import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-page-path'
import { loadEntrypoint } from '../../../load-entrypoint'
import type { PAGE_TYPES } from '../../../../lib/page-types'

export type EdgeSSRLoaderQuery = {
  absolute500Path: string
  absoluteAppPath: string
  absoluteDocumentPath: string
  absoluteErrorPath: string
  absolutePagePath: string
  dev: boolean
  isServerComponent: boolean
  page: string
  stringifiedConfig: string
  appDirLoader?: string
  pagesType: PAGE_TYPES
  sriEnabled: boolean
  cacheHandler?: string
  cacheHandlers?: string
  preferredRegion: string | string[] | undefined
  middlewareConfig: string
  serverActions?: {
    bodySizeLimit?: SizeLimit
    allowedOrigins?: string[]
  }
}

/*
For pages SSR'd at the edge, we bundle them with the ESM version of Next in order to
benefit from the better tree-shaking and thus, smaller bundle sizes.

The absolute paths for _app, _error and _document, used in this loader, link to the regular CJS modules.
They are generated in `createPagesMapping` where we don't have access to `isEdgeRuntime`,
so we have to do it here. It's not that bad because it keeps all references to ESM modules magic in this place.
*/
function swapDistFolderWithEsmDistFolder(path: string) {
  return path.replace('next/dist/pages', 'next/dist/esm/pages')
}

function getRouteModuleOptions(page: string) {
  const options: Omit<PagesRouteModuleOptions, 'userland' | 'components'> = {
    definition: {
      kind: RouteKind.PAGES,
      page: normalizePagePath(page),
      pathname: page,
      // The following aren't used in production.
      bundlePath: '',
      filename: '',
    },
    // edge runtime doesn't read from distDir or projectDir
    distDir: '',
    relativeProjectDir: '',
  }

  return options
}

const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
  async function edgeSSRLoader(this) {
    const {
      page,
      absolutePagePath,
      absoluteAppPath,
      absoluteDocumentPath,
      absolute500Path,
      absoluteErrorPath,
      isServerComponent,
      stringifiedConfig: stringifiedConfigBase64,
      appDirLoader: appDirLoaderBase64,
      pagesType,
      cacheHandler,
      cacheHandlers: cacheHandlersStringified,
      preferredRegion,
      middlewareConfig: middlewareConfigBase64,
    } = this.getOptions()

    const cacheHandlers = JSON.parse(cacheHandlersStringified || '{}')

    if (!cacheHandlers.default) {
      cacheHandlers.default = require.resolve(
        '../../../../server/lib/cache-handlers/default.external'
      )
    }

    const middlewareConfig: MiddlewareConfig = JSON.parse(
      Buffer.from(middlewareConfigBase64, 'base64').toString()
    )

    const stringifiedConfig = Buffer.from(
      stringifiedConfigBase64 || '',
      'base64'
    ).toString()
    const appDirLoader = Buffer.from(
      appDirLoaderBase64 || '',
      'base64'
    ).toString()
    const isAppDir = pagesType === 'app'

    const buildInfo = getModuleBuildInfo(this._module as any)
    buildInfo.nextEdgeSSR = {
      isServerComponent,
      page: page,
      isAppDir,
    }
    buildInfo.route = {
      page,
      absolutePagePath,
      preferredRegion,
      middlewareConfig,
    }

    const pagePath = this.utils.contextify(
      this.context || this.rootContext,
      absolutePagePath
    )
    const appPath = this.utils.contextify(
      this.context || this.rootContext,
      swapDistFolderWithEsmDistFolder(absoluteAppPath)
    )
    const errorPath = this.utils.contextify(
      this.context || this.rootContext,
      swapDistFolderWithEsmDistFolder(absoluteErrorPath)
    )
    const documentPath = this.utils.contextify(
      this.context || this.rootContext,
      swapDistFolderWithEsmDistFolder(absoluteDocumentPath)
    )
    const userland500Path = absolute500Path
      ? this.utils.contextify(
          this.context || this.rootContext,
          swapDistFolderWithEsmDistFolder(absolute500Path)
        )
      : null

    const stringifiedPagePath = JSON.stringify(pagePath)

    const pageModPath = `${appDirLoader}${stringifiedPagePath.substring(
      1,
      stringifiedPagePath.length - 1
    )}${isAppDir ? `?${WEBPACK_RESOURCE_QUERIES.edgeSSREntry}` : ''}`

    if (isAppDir) {
      return await loadEntrypoint(
        'edge-ssr-app',
        {
          VAR_USERLAND: pageModPath,
          VAR_PAGE: page,
        },
        {
          nextConfig: stringifiedConfig,
        },
        {
          incrementalCacheHandler: cacheHandler ?? null,
        }
      )
    } else {
      return await loadEntrypoint(
        'edge-ssr',
        {
          VAR_USERLAND: pageModPath,
          VAR_PAGE: page,
          VAR_MODULE_DOCUMENT: documentPath,
          VAR_MODULE_APP: appPath,
          VAR_MODULE_GLOBAL_ERROR: errorPath,
        },
        {
          nextConfig: stringifiedConfig,
          pageRouteModuleOptions: JSON.stringify(getRouteModuleOptions(page)),
          errorRouteModuleOptions: JSON.stringify(
            getRouteModuleOptions('/_error')
          ),
          user500RouteModuleOptions: JSON.stringify(
            getRouteModuleOptions('/500')
          ),
        },
        {
          userland500Page: userland500Path,
          incrementalCacheHandler: cacheHandler ?? null,
        }
      )
    }
  }
export default edgeSSRLoader