2021-11-02 16:13:15 +01:00
|
|
|
import { stringifyRequest } from '../../stringify-request'
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2021-11-26 16:53:38 +01:00
|
|
|
export default async function middlewareSSRLoader(this: any) {
|
2021-10-26 18:50:56 +02:00
|
|
|
const {
|
|
|
|
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,
|
|
|
|
...restRenderOpts
|
2021-11-02 16:13:15 +01:00
|
|
|
} = this.getOptions()
|
|
|
|
|
|
|
|
const stringifiedAbsolutePagePath = stringifyRequest(this, absolutePagePath)
|
2021-11-04 20:10:07 +01:00
|
|
|
const stringifiedAbsoluteAppPath = stringifyRequest(this, absoluteAppPath)
|
2021-11-16 21:43:57 +01:00
|
|
|
const stringifiedAbsolute500PagePath = stringifyRequest(
|
|
|
|
this,
|
|
|
|
absolute500Path || absoluteErrorPath
|
|
|
|
)
|
2021-11-04 20:10:07 +01:00
|
|
|
const stringifiedAbsoluteDocumentPath = stringifyRequest(
|
2021-10-26 18:50:56 +02:00
|
|
|
this,
|
2021-11-04 20:10:07 +01:00
|
|
|
absoluteDocumentPath
|
2021-10-26 18:50:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const transformed = `
|
2021-11-16 21:43:57 +01:00
|
|
|
import { adapter } from 'next/dist/server/web/adapter'
|
|
|
|
import { RouterContext } from 'next/dist/shared/lib/router-context'
|
|
|
|
|
|
|
|
import App from ${stringifiedAbsoluteAppPath}
|
|
|
|
import Document from ${stringifiedAbsoluteDocumentPath}
|
|
|
|
|
|
|
|
import { getRender } from 'next/dist/build/webpack/loaders/next-middleware-ssr-loader/render'
|
|
|
|
|
|
|
|
const pageMod = require(${stringifiedAbsolutePagePath})
|
|
|
|
const errorMod = require(${stringifiedAbsolute500PagePath})
|
|
|
|
|
|
|
|
const buildManifest = self.__BUILD_MANIFEST
|
|
|
|
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
|
|
|
|
const rscManifest = self.__RSC_MANIFEST
|
|
|
|
|
|
|
|
if (typeof pageMod.default !== 'function') {
|
|
|
|
throw new Error('Your page must export a \`default\` component')
|
|
|
|
}
|
|
|
|
|
|
|
|
const render = getRender({
|
|
|
|
App,
|
|
|
|
Document,
|
|
|
|
pageMod,
|
|
|
|
errorMod,
|
|
|
|
buildManifest,
|
|
|
|
reactLoadableManifest,
|
|
|
|
rscManifest,
|
|
|
|
isServerComponent: ${JSON.stringify(isServerComponent)},
|
|
|
|
restRenderOpts: ${JSON.stringify(restRenderOpts)}
|
|
|
|
})
|
|
|
|
|
|
|
|
export default function rscMiddleware(opts) {
|
|
|
|
return adapter({
|
|
|
|
...opts,
|
|
|
|
handler: render
|
|
|
|
})
|
|
|
|
}`
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
return transformed
|
|
|
|
}
|