.
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
import type webpack from 'webpack';
|
||||
import type { SizeLimit } from '../../../../types';
|
||||
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;
|
||||
appDirLoader?: string;
|
||||
pagesType: PAGE_TYPES;
|
||||
sriEnabled: boolean;
|
||||
cacheHandler?: string;
|
||||
cacheHandlers?: string;
|
||||
preferredRegion: string | string[] | undefined;
|
||||
middlewareConfig: string;
|
||||
serverActions?: {
|
||||
bodySizeLimit?: SizeLimit;
|
||||
allowedOrigins?: string[];
|
||||
};
|
||||
};
|
||||
declare const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery>;
|
||||
export default edgeSSRLoader;
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "default", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return _default;
|
||||
}
|
||||
});
|
||||
const _getmodulebuildinfo = require("../get-module-build-info");
|
||||
const _constants = require("../../../../lib/constants");
|
||||
const _routekind = require("../../../../server/route-kind");
|
||||
const _normalizepagepath = require("../../../../shared/lib/page-path/normalize-page-path");
|
||||
const _loadentrypoint = require("../../../load-entrypoint");
|
||||
/*
|
||||
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) {
|
||||
return path.replace('next/dist/pages', 'next/dist/esm/pages');
|
||||
}
|
||||
function getRouteModuleOptions(page) {
|
||||
const options = {
|
||||
definition: {
|
||||
kind: _routekind.RouteKind.PAGES,
|
||||
page: (0, _normalizepagepath.normalizePagePath)(page),
|
||||
pathname: page,
|
||||
// The following aren't used in production.
|
||||
bundlePath: '',
|
||||
filename: ''
|
||||
}
|
||||
};
|
||||
return options;
|
||||
}
|
||||
function getCacheHandlersSetup(cacheHandlersStringified, contextifyImportPath) {
|
||||
const cacheHandlers = JSON.parse(cacheHandlersStringified || '{}');
|
||||
const definedCacheHandlers = Object.entries(cacheHandlers).filter((entry)=>Boolean(entry[1]));
|
||||
const cacheHandlerImports = [];
|
||||
const cacheHandlerRegistration = [];
|
||||
for (const [index, [kind, handlerPath]] of definedCacheHandlers.entries()){
|
||||
const cacheHandlerVarName = `edgeCacheHandler_${index}`;
|
||||
const cacheHandlerImportPath = contextifyImportPath(handlerPath);
|
||||
cacheHandlerImports.push(`import ${cacheHandlerVarName} from ${JSON.stringify(cacheHandlerImportPath)}`);
|
||||
cacheHandlerRegistration.push(` cacheHandlers.setCacheHandler(${JSON.stringify(kind)}, ${cacheHandlerVarName})`);
|
||||
}
|
||||
return {
|
||||
cacheHandlerImports: cacheHandlerImports.join('\n') || '\n',
|
||||
cacheHandlerRegistration: cacheHandlerRegistration.join('\n') || '\n'
|
||||
};
|
||||
}
|
||||
const edgeSSRLoader = async function edgeSSRLoader() {
|
||||
const { page, absolutePagePath, absoluteAppPath, absoluteDocumentPath, absolute500Path, absoluteErrorPath, isServerComponent, appDirLoader: appDirLoaderBase64, pagesType, cacheHandler, cacheHandlers: cacheHandlersStringified, preferredRegion, middlewareConfig: middlewareConfigBase64 } = this.getOptions();
|
||||
const cacheHandlersSetup = getCacheHandlersSetup(cacheHandlersStringified, (handlerPath)=>this.utils.contextify(this.context || this.rootContext, handlerPath));
|
||||
const incrementalCacheHandler = cacheHandler ? this.utils.contextify(this.context || this.rootContext, cacheHandler) : null;
|
||||
const middlewareConfig = JSON.parse(Buffer.from(middlewareConfigBase64, 'base64').toString());
|
||||
const appDirLoader = Buffer.from(appDirLoaderBase64 || '', 'base64').toString();
|
||||
const isAppDir = pagesType === 'app';
|
||||
const buildInfo = (0, _getmodulebuildinfo.getModuleBuildInfo)(this._module);
|
||||
buildInfo.nextEdgeSSR = {
|
||||
isServerComponent,
|
||||
page: page,
|
||||
isAppDir
|
||||
};
|
||||
buildInfo.route = {
|
||||
page,
|
||||
absolutePagePath,
|
||||
preferredRegion,
|
||||
middlewareConfig
|
||||
};
|
||||
const pagePath = this.utils.contextify(this.context || this.rootContext, absolutePagePath);
|
||||
const appPath = absoluteAppPath ? this.utils.contextify(this.context || this.rootContext, swapDistFolderWithEsmDistFolder(absoluteAppPath)) : '';
|
||||
const errorPath = absoluteErrorPath ? this.utils.contextify(this.context || this.rootContext, swapDistFolderWithEsmDistFolder(absoluteErrorPath)) : '';
|
||||
const documentPath = absoluteDocumentPath ? 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 ? `?${_constants.WEBPACK_RESOURCE_QUERIES.edgeSSREntry}` : ''}`;
|
||||
if (isAppDir) {
|
||||
return await (0, _loadentrypoint.loadEntrypoint)('edge-ssr-app', {
|
||||
VAR_USERLAND: pageModPath,
|
||||
VAR_PAGE: page
|
||||
}, cacheHandlersSetup, {
|
||||
incrementalCacheHandler
|
||||
});
|
||||
} else {
|
||||
return await (0, _loadentrypoint.loadEntrypoint)('edge-ssr', {
|
||||
VAR_USERLAND: pageModPath,
|
||||
VAR_DEFINITION_PATHNAME: page,
|
||||
VAR_MODULE_DOCUMENT: documentPath,
|
||||
VAR_MODULE_APP: appPath,
|
||||
VAR_MODULE_GLOBAL_ERROR: errorPath
|
||||
}, {
|
||||
pageRouteModuleOptions: JSON.stringify(getRouteModuleOptions(page)),
|
||||
errorRouteModuleOptions: JSON.stringify(getRouteModuleOptions('/_error')),
|
||||
user500RouteModuleOptions: JSON.stringify(getRouteModuleOptions('/500')),
|
||||
...cacheHandlersSetup ?? {}
|
||||
}, {
|
||||
userland500Page: userland500Path,
|
||||
incrementalCacheHandler
|
||||
});
|
||||
}
|
||||
};
|
||||
const _default = edgeSSRLoader;
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user