Skip to content

Commit

Permalink
remove legacy transform code (vercel#40966)
Browse files Browse the repository at this point in the history
* Remove legacy `__next_rsc__` transform
* Fix `staticGenerationAsyncStorage` is null during rendering
  • Loading branch information
huozhi authored and BowlingX committed Oct 5, 2022
1 parent 23fe351 commit 78a335b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 55 deletions.
Expand Up @@ -20,25 +20,15 @@ export default async function transformSource(this: any): Promise<string> {
}

const requests = modules as string[]
const code =
requests
// Filter out css files on the server
.filter((request) => (isServer ? !request.endsWith('.css') : true))
.map((request) =>
request.endsWith('.css')
? `(() => import(/* webpackMode: "lazy" */ ${JSON.stringify(
request
)}))`
: `import(/* webpackMode: "eager" */ ${JSON.stringify(request)})`
)
.join(';\n') +
`
export const __next_rsc__ = {
server: false,
__webpack_require__
};
export default function RSC() {};
`
const code = requests
// Filter out css files on the server
.filter((request) => (isServer ? !request.endsWith('.css') : true))
.map((request) =>
request.endsWith('.css')
? `(() => import(/* webpackMode: "lazy" */ ${JSON.stringify(request)}))`
: `import(/* webpackMode: "eager" */ ${JSON.stringify(request)})`
)
.join(';\n')

const buildInfo = getModuleBuildInfo(this._module)
const resolve = this.getResolve()
Expand Down
19 changes: 1 addition & 18 deletions packages/next/build/webpack/loaders/next-flight-loader/index.ts
@@ -1,16 +1,7 @@
import { RSC_MODULE_TYPES } from '../../../../shared/lib/constants'
import { getRSCModuleType } from '../../../analysis/get-page-static-info'
import { parse } from '../../../swc'
import { getModuleBuildInfo } from '../get-module-build-info'

function transformServer(source: string, isESModule: boolean) {
return (
source +
(isESModule ? `export const __next_rsc__` : `exports.__next_rsc__`) +
` = { __webpack_require__, server: true }\n`
)
}

export default async function transformSource(
this: any,
source: string,
Expand All @@ -21,14 +12,8 @@ export default async function transformSource(
throw new Error('Expected source to have been transformed to a string.')
}

const { resourcePath } = this
const callback = this.async()
const buildInfo = getModuleBuildInfo(this._module)
const swcAST = await parse(source, {
filename: resourcePath,
isModule: 'unknown',
})

const rscType = getRSCModuleType(source)

// Assign the RSC meta information to buildInfo.
Expand All @@ -39,7 +24,5 @@ export default async function transformSource(
return callback(null, source, sourceMap)
}

const isModule = swcAST.type === 'Module'
const code = transformServer(source, isModule)
return callback(null, code, sourceMap)
return callback(null, source, sourceMap)
}
10 changes: 3 additions & 7 deletions packages/next/server/app-render.tsx
Expand Up @@ -317,9 +317,6 @@ function createServerComponentRenderer(
ComponentMod: {
renderToReadableStream: any
__next_app_webpack_require__?: any
__next_rsc__?: {
__webpack_require__?: any
}
},
{
transformStream,
Expand All @@ -339,11 +336,9 @@ function createServerComponentRenderer(
): () => JSX.Element {
// We need to expose the `__webpack_require__` API globally for
// react-server-dom-webpack. This is a hack until we find a better way.
if (ComponentMod.__next_app_webpack_require__ || ComponentMod.__next_rsc__) {
if (ComponentMod.__next_app_webpack_require__) {
// @ts-ignore
globalThis.__next_require__ =
ComponentMod.__next_app_webpack_require__ ||
ComponentMod.__next_rsc__?.__webpack_require__
globalThis.__next_require__ = ComponentMod.__next_app_webpack_require__

// @ts-ignore
globalThis.__next_chunk_load__ = () => Promise.resolve()
Expand Down Expand Up @@ -639,6 +634,7 @@ export async function renderToHTMLOrFlight(
const requestAsyncStorage = ComponentMod.requestAsyncStorage

if (
staticGenerationAsyncStorage &&
!('getStore' in staticGenerationAsyncStorage) &&
staticGenerationAsyncStorage.inUse
) {
Expand Down
13 changes: 2 additions & 11 deletions packages/next/server/base-server.ts
Expand Up @@ -963,15 +963,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
let hasStaticPaths = !!components.getStaticPaths

const hasGetInitialProps = !!components.Component?.getInitialProps
const isServerComponent = !!components.ComponentMod?.__next_rsc__
let isSSG =
!!components.getStaticProps ||
// For static server component pages, we currently always consider them
// as SSG since we also need to handle the next data (flight JSON).
(isServerComponent &&
!hasServerProps &&
!hasGetInitialProps &&
process.env.NEXT_RUNTIME !== 'edge')
let isSSG = !!components.getStaticProps

// Toggle whether or not this is a Data request
const isDataReq =
Expand All @@ -980,7 +972,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
(req.headers['x-nextjs-data'] &&
(this.serverOptions as any).webServerConfig)
) &&
(isSSG || hasServerProps || isServerComponent)
(isSSG || hasServerProps)

delete query.__nextDataReq

Expand Down Expand Up @@ -1027,7 +1019,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}

if (
!isServerComponent &&
!!req.headers['x-nextjs-data'] &&
(!res.statusCode || res.statusCode === 200)
) {
Expand Down

0 comments on commit 78a335b

Please sign in to comment.