2022-04-30 13:19:27 +02:00
|
|
|
import { getModuleBuildInfo } from '../get-module-build-info'
|
2021-11-02 16:13:15 +01:00
|
|
|
import { stringifyRequest } from '../../stringify-request'
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2022-06-26 23:01:26 +02:00
|
|
|
export type EdgeSSRLoaderQuery = {
|
2022-04-27 11:50:29 +02:00
|
|
|
absolute500Path: string
|
|
|
|
absoluteAppPath: string
|
|
|
|
absoluteDocumentPath: string
|
|
|
|
absoluteErrorPath: string
|
|
|
|
absolutePagePath: string
|
|
|
|
buildId: string
|
|
|
|
dev: boolean
|
|
|
|
isServerComponent: boolean
|
|
|
|
page: string
|
|
|
|
stringifiedConfig: string
|
2022-08-24 21:49:47 +02:00
|
|
|
appDirLoader?: string
|
|
|
|
pagesType?: 'app' | 'pages' | 'root'
|
2022-04-27 11:50:29 +02:00
|
|
|
}
|
|
|
|
|
2022-06-26 23:01:26 +02:00
|
|
|
export default async function edgeSSRLoader(this: any) {
|
2021-10-26 18:50:56 +02:00
|
|
|
const {
|
2022-01-26 07:22:11 +01:00
|
|
|
dev,
|
|
|
|
page,
|
|
|
|
buildId,
|
2021-10-26 18:50:56 +02:00
|
|
|
absolutePagePath,
|
2021-11-04 20:10:07 +01:00
|
|
|
absoluteAppPath,
|
|
|
|
absoluteDocumentPath,
|
2021-11-16 21:43:57 +01:00
|
|
|
absolute500Path,
|
2021-11-15 18:49:50 +01:00
|
|
|
absoluteErrorPath,
|
2021-11-16 21:43:57 +01:00
|
|
|
isServerComponent,
|
2022-01-26 07:22:11 +01:00
|
|
|
stringifiedConfig,
|
2022-08-24 21:49:47 +02:00
|
|
|
appDirLoader: appDirLoaderBase64,
|
|
|
|
pagesType,
|
2022-04-30 13:19:27 +02:00
|
|
|
} = this.getOptions()
|
|
|
|
|
2022-08-24 21:49:47 +02:00
|
|
|
const appDirLoader = Buffer.from(
|
|
|
|
appDirLoaderBase64 || '',
|
|
|
|
'base64'
|
|
|
|
).toString()
|
|
|
|
const isAppDir = pagesType === 'app'
|
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
const buildInfo = getModuleBuildInfo(this._module)
|
|
|
|
buildInfo.nextEdgeSSR = {
|
|
|
|
isServerComponent: isServerComponent === 'true',
|
|
|
|
page: page,
|
2022-08-24 21:49:47 +02:00
|
|
|
isAppDir,
|
2022-04-30 13:19:27 +02:00
|
|
|
}
|
2022-05-13 19:48:53 +02:00
|
|
|
buildInfo.route = {
|
|
|
|
page,
|
|
|
|
absolutePagePath,
|
|
|
|
}
|
2021-11-02 16:13:15 +01:00
|
|
|
|
2022-01-27 17:20:16 +01:00
|
|
|
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
|
|
|
|
const stringifiedAppPath = stringifyRequest(this, absoluteAppPath)
|
|
|
|
const stringifiedErrorPath = stringifyRequest(this, absoluteErrorPath)
|
|
|
|
const stringifiedDocumentPath = stringifyRequest(this, absoluteDocumentPath)
|
|
|
|
const stringified500Path = absolute500Path
|
|
|
|
? stringifyRequest(this, absolute500Path)
|
2022-04-05 21:46:17 +02:00
|
|
|
: null
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2022-08-24 21:49:47 +02:00
|
|
|
const pageModPath = `${appDirLoader}${stringifiedPagePath.substring(
|
|
|
|
1,
|
|
|
|
stringifiedPagePath.length - 1
|
|
|
|
)}`
|
|
|
|
|
2021-10-26 18:50:56 +02:00
|
|
|
const transformed = `
|
2022-07-26 23:41:59 +02:00
|
|
|
import { adapter, enhanceGlobals } from 'next/dist/server/web/adapter'
|
2022-06-26 23:01:26 +02:00
|
|
|
import { getRender } from 'next/dist/build/webpack/loaders/next-edge-ssr-loader/render'
|
2021-11-16 21:43:57 +01:00
|
|
|
|
2022-01-27 17:20:16 +01:00
|
|
|
import Document from ${stringifiedDocumentPath}
|
|
|
|
|
2022-07-26 23:41:59 +02:00
|
|
|
enhanceGlobals()
|
|
|
|
|
2022-08-24 21:49:47 +02:00
|
|
|
${
|
|
|
|
isAppDir
|
|
|
|
? `
|
|
|
|
const appRenderToHTML = require('next/dist/server/app-render').renderToHTMLOrFlight
|
|
|
|
const pagesRenderToHTML = null
|
|
|
|
const pageMod = require(${JSON.stringify(pageModPath)})
|
|
|
|
`
|
|
|
|
: `
|
|
|
|
const appRenderToHTML = null
|
|
|
|
const pagesRenderToHTML = require('next/dist/server/render').renderToHTML
|
|
|
|
const pageMod = require(${stringifiedPagePath})
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2022-03-02 19:29:54 +01:00
|
|
|
const appMod = require(${stringifiedAppPath})
|
2022-01-27 17:20:16 +01:00
|
|
|
const errorMod = require(${stringifiedErrorPath})
|
2022-04-05 21:46:17 +02:00
|
|
|
const error500Mod = ${
|
|
|
|
stringified500Path ? `require(${stringified500Path})` : 'null'
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:43:57 +01:00
|
|
|
const buildManifest = self.__BUILD_MANIFEST
|
|
|
|
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
|
|
|
|
const rscManifest = self.__RSC_MANIFEST
|
2022-08-24 21:49:47 +02:00
|
|
|
const rscCssManifest = self.__RSC_CSS_MANIFEST
|
2021-11-16 21:43:57 +01:00
|
|
|
|
2022-01-26 07:22:11 +01:00
|
|
|
const render = getRender({
|
2022-02-01 23:36:47 +01:00
|
|
|
dev: ${dev},
|
|
|
|
page: ${JSON.stringify(page)},
|
2022-03-02 19:29:54 +01:00
|
|
|
appMod,
|
2022-02-01 23:36:47 +01:00
|
|
|
pageMod,
|
|
|
|
errorMod,
|
|
|
|
error500Mod,
|
2022-01-26 07:22:11 +01:00
|
|
|
Document,
|
2022-02-01 23:36:47 +01:00
|
|
|
buildManifest,
|
2022-08-24 21:49:47 +02:00
|
|
|
appRenderToHTML,
|
|
|
|
pagesRenderToHTML,
|
2022-02-01 23:36:47 +01:00
|
|
|
reactLoadableManifest,
|
|
|
|
serverComponentManifest: ${isServerComponent} ? rscManifest : null,
|
2022-08-24 21:49:47 +02:00
|
|
|
serverCSSManifest: ${isServerComponent} ? rscCssManifest : null,
|
2022-01-26 07:22:11 +01:00
|
|
|
config: ${stringifiedConfig},
|
2022-02-01 23:36:47 +01:00
|
|
|
buildId: ${JSON.stringify(buildId)},
|
2021-11-16 21:43:57 +01:00
|
|
|
})
|
|
|
|
|
2022-06-29 02:30:55 +02:00
|
|
|
export default function(opts) {
|
2021-11-16 21:43:57 +01:00
|
|
|
return adapter({
|
|
|
|
...opts,
|
|
|
|
handler: render
|
|
|
|
})
|
|
|
|
}`
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
return transformed
|
|
|
|
}
|