2021-11-02 16:13:15 +01:00
|
|
|
import { stringifyRequest } from '../../stringify-request'
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2022-04-27 11:50:29 +02:00
|
|
|
export type MiddlewareSSRLoaderQuery = {
|
|
|
|
absolute500Path: string
|
|
|
|
absoluteAppPath: string
|
|
|
|
absoluteAppServerPath: string
|
|
|
|
absoluteDocumentPath: string
|
|
|
|
absoluteErrorPath: string
|
|
|
|
absolutePagePath: string
|
|
|
|
buildId: string
|
|
|
|
dev: boolean
|
|
|
|
isServerComponent: boolean
|
|
|
|
page: string
|
|
|
|
stringifiedConfig: string
|
|
|
|
}
|
|
|
|
|
2021-11-26 16:53:38 +01:00
|
|
|
export default async function middlewareSSRLoader(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,
|
2022-04-05 21:46:17 +02:00
|
|
|
absoluteAppServerPath,
|
2021-11-04 20:10:07 +01:00
|
|
|
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-04-27 11:50:29 +02:00
|
|
|
}: MiddlewareSSRLoaderQuery = this.getOptions()
|
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)
|
2022-04-05 21:46:17 +02:00
|
|
|
const stringifiedAppServerPath = absoluteAppServerPath
|
|
|
|
? stringifyRequest(this, absoluteAppServerPath)
|
|
|
|
: null
|
|
|
|
|
2022-01-27 17:20:16 +01:00
|
|
|
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
|
|
|
|
|
|
|
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 { getRender } from 'next/dist/build/webpack/loaders/next-middleware-ssr-loader/render'
|
|
|
|
|
2022-01-27 17:20:16 +01:00
|
|
|
import Document from ${stringifiedDocumentPath}
|
|
|
|
|
2022-03-02 19:29:54 +01:00
|
|
|
const appMod = require(${stringifiedAppPath})
|
2022-04-05 21:46:17 +02:00
|
|
|
const appServerMod = ${
|
|
|
|
stringifiedAppServerPath ? `require(${stringifiedAppServerPath})` : 'null'
|
|
|
|
}
|
2022-01-27 17:20:16 +01:00
|
|
|
const pageMod = require(${stringifiedPagePath})
|
|
|
|
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-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,
|
|
|
|
reactLoadableManifest,
|
|
|
|
serverComponentManifest: ${isServerComponent} ? rscManifest : null,
|
2022-04-05 21:46:17 +02:00
|
|
|
appServerMod,
|
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
|
|
|
})
|
|
|
|
|
|
|
|
export default function rscMiddleware(opts) {
|
|
|
|
return adapter({
|
|
|
|
...opts,
|
|
|
|
handler: render
|
|
|
|
})
|
|
|
|
}`
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
return transformed
|
|
|
|
}
|