Unverified Commit 77b0c7b7 authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

turbopack: defer dev code evaluation until after page render IPC connects (#47888)

When the devs have a parse/module-eval error in their Page/App module, Turbopack's rendering process will fail to connect the IPC. This is because the entrypoint statically imports the dev module. If either fails, then we'll never reach the code to set up the IPC. This leads to a "timeout while receiving message from process" timeout error after 30s, instead of receiving an exact message/stack detailing the dev's error.

By wrapping the import code in a deferred dynamic-import closure, we're able to connect the IPC first, then evaluate the dev code. This wrapping style is a little awkward, but it's necessary with the way to inject virtual modules into our graph.

Fixes WEB-850
parent a79d1efd
// IPC need to be the first import to allow it to catch errors happening during
// the other imports
import startHandler from "@vercel/turbopack-next/internal/page-server-handler";
import startHandler from '@vercel/turbopack-next/internal/page-server-handler'
// eslint-disable-next-line
import Document from "next/document";
import App from "next/app";
import * as otherExports from "INNER";
import Document from 'next/document'
import App from 'next/app'
startHandler({
isDataReq: true,
App,
Document,
Component: () => {},
otherExports,
});
mod: () => {
return import('INNER').then((namespace) => ({
Component: () => null,
namespace,
}))
},
})
// IPC need to be the first import to allow it to catch errors happening during
// the other imports
import startHandler from "@vercel/turbopack-next/internal/page-server-handler";
import startHandler from '@vercel/turbopack-next/internal/page-server-handler'
import App from "@vercel/turbopack-next/pages/_app";
import Document from "@vercel/turbopack-next/pages/_document";
import App from '@vercel/turbopack-next/pages/_app'
import Document from '@vercel/turbopack-next/pages/_document'
import Component, * as otherExports from "INNER";
import chunkGroup from "INNER_CLIENT_CHUNK_GROUP";
import chunkGroup from 'INNER_CLIENT_CHUNK_GROUP'
startHandler({
isDataReq: false,
App,
Document,
Component,
otherExports,
mod: () => {
return import('INNER').then((namespace) => ({
Component: namespace.default as any,
namespace,
}))
},
chunkGroup,
});
})
......@@ -19,6 +19,7 @@ import { headersFromEntries } from "@vercel/turbopack-next/internal/headers";
import type { Ipc } from "@vercel/turbopack-next/ipc/index";
import type { RenderData } from "types/turbopack";
import type { ChunkGroup } from "types/next";
import type { NextComponentType } from "next/types";
import { parse } from "node:querystring";
const ipc = IPC as Ipc<IpcIncomingMessage, IpcOutgoingMessage>;
......@@ -40,19 +41,22 @@ type IpcOutgoingMessage =
const MIME_APPLICATION_JAVASCRIPT = "application/javascript";
const MIME_TEXT_HTML_UTF8 = "text/html; charset=utf-8";
type ModuleImport = Promise<{
Component: NextComponentType,
namespace: Record<string, any>;
}>
export default function startHandler({
isDataReq,
App,
Document,
Component,
otherExports,
mod,
chunkGroup,
}: {
isDataReq: boolean;
App?: any;
Document?: any;
Component?: any;
otherExports: any;
mod: () => ModuleImport,
chunkGroup?: ChunkGroup;
}) {
(async () => {
......@@ -71,7 +75,9 @@ export default function startHandler({
}
}
const res = await runOperation(renderData);
const { Component, namespace } = await mod();
const res = await runOperation(renderData, Component, namespace);
ipc.send(res);
}
......@@ -80,16 +86,18 @@ export default function startHandler({
});
async function runOperation(
renderData: RenderData
renderData: RenderData,
Component: NextComponentType,
namespace: Record<string, any>,
): Promise<IpcOutgoingMessage> {
if ("getStaticPaths" in otherExports) {
if ("getStaticPaths" in namespace) {
const {
paths: prerenderRoutes,
fallback: prerenderFallback,
encodedPaths: _encodedPrerenderRoutes,
} = await buildStaticPaths({
page: renderData.path,
getStaticPaths: otherExports.getStaticPaths,
getStaticPaths: namespace.getStaticPaths,
// TODO(alexkirsz) Provide the correct next.config.js path.
configFileName: "next.config.js",
});
......@@ -140,7 +148,7 @@ export default function startHandler({
const comp =
typeof Component === "undefined" ||
(typeof Component === "object" && Object.keys(Component).length === 0)
? () => {}
? () => null
: Component;
const renderOpts: RenderOpts = {
......@@ -153,7 +161,7 @@ export default function startHandler({
reactLoadableManifest: createReactLoadableManifestProxy(),
ComponentMod: {
default: comp,
...otherExports,
...namespace,
},
pathname: renderData.path,
buildId: "development",
......@@ -195,14 +203,14 @@ export default function startHandler({
},
};
if ("getStaticPaths" in otherExports) {
renderOpts.getStaticPaths = otherExports.getStaticPaths;
if ("getStaticPaths" in namespace) {
renderOpts.getStaticPaths = namespace.getStaticPaths;
}
if ("getStaticProps" in otherExports) {
renderOpts.getStaticProps = otherExports.getStaticProps;
if ("getStaticProps" in namespace) {
renderOpts.getStaticProps = namespace.getStaticProps;
}
if ("getServerSideProps" in otherExports) {
renderOpts.getServerSideProps = otherExports.getServerSideProps;
if ("getServerSideProps" in namespace) {
renderOpts.getServerSideProps = namespace.getServerSideProps;
}
const req: IncomingMessage = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment