Unverified Commit cbfab2a3 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Strip internal pages for pagesDir in app edge ssr (#40776)

For app edge SSR, we don't need any internal pages for it since it's
handled by app-renderer. This results around reducing 20KB from app edge
SSR

# Changes
* Strip those internal pages modules when it's in app edge SSR
* Minimize edge SSR chunk to see the tree-shake result
* Add bundle analyzer with switcher in stats-app for testing. Using
`TEST_ANAYLYSE=1` to build stats-app for testing
parent 69595adc
...@@ -253,9 +253,13 @@ async function linkPkgs(pkgDir = '', pkgPaths) { ...@@ -253,9 +253,13 @@ async function linkPkgs(pkgDir = '', pkgPaths) {
await fs.writeFile(pkgJsonPath, JSON.stringify(pkgData, null, 2), 'utf8') await fs.writeFile(pkgJsonPath, JSON.stringify(pkgData, null, 2), 'utf8')
await fs.remove(yarnEnvValues.YARN_CACHE_FOLDER) await fs.remove(yarnEnvValues.YARN_CACHE_FOLDER)
await exec(`cd ${pkgDir} && pnpm install`, false, { await exec(
`cd ${pkgDir} && pnpm install --strict-peer-dependencies=false`,
false,
{
env: yarnEnvValues, env: yarnEnvValues,
}) }
)
} }
module.exports = runConfigs module.exports = runConfigs
...@@ -1401,7 +1401,7 @@ export default async function getBaseWebpackConfig( ...@@ -1401,7 +1401,7 @@ export default async function getBaseWebpackConfig(
runtimeChunk: isClient runtimeChunk: isClient
? { name: CLIENT_STATIC_FILES_RUNTIME_WEBPACK } ? { name: CLIENT_STATIC_FILES_RUNTIME_WEBPACK }
: undefined, : undefined,
minimize: !dev && isClient, minimize: !dev && (isClient || isEdgeServer),
minimizer: [ minimizer: [
// Minify JavaScript // Minify JavaScript
(compiler: webpack.Compiler) => { (compiler: webpack.Compiler) => {
......
...@@ -68,29 +68,33 @@ export default async function edgeSSRLoader(this: any) { ...@@ -68,29 +68,33 @@ export default async function edgeSSRLoader(this: any) {
import { adapter, enhanceGlobals } from 'next/dist/server/web/adapter' import { adapter, enhanceGlobals } from 'next/dist/server/web/adapter'
import { getRender } from 'next/dist/build/webpack/loaders/next-edge-ssr-loader/render' import { getRender } from 'next/dist/build/webpack/loaders/next-edge-ssr-loader/render'
import Document from ${stringifiedDocumentPath}
enhanceGlobals() enhanceGlobals()
const pageType = ${JSON.stringify(pagesType)}
${ ${
isAppDir isAppDir
? ` ? `
const Document = null
const appRenderToHTML = require('next/dist/server/app-render').renderToHTMLOrFlight const appRenderToHTML = require('next/dist/server/app-render').renderToHTMLOrFlight
const pagesRenderToHTML = null const pagesRenderToHTML = null
const pageMod = require(${JSON.stringify(pageModPath)}) const pageMod = require(${JSON.stringify(pageModPath)})
const appMod = null
const errorMod = null
const error500Mod = null
` `
: ` : `
const Document = require(${stringifiedDocumentPath}).default
const appRenderToHTML = null const appRenderToHTML = null
const pagesRenderToHTML = require('next/dist/server/render').renderToHTML const pagesRenderToHTML = require('next/dist/server/render').renderToHTML
const pageMod = require(${stringifiedPagePath}) const pageMod = require(${stringifiedPagePath})
`
}
const appMod = require(${stringifiedAppPath}) const appMod = require(${stringifiedAppPath})
const errorMod = require(${stringifiedErrorPath}) const errorMod = require(${stringifiedErrorPath})
const error500Mod = ${ const error500Mod = ${
stringified500Path ? `require(${stringified500Path})` : 'null' stringified500Path ? `require(${stringified500Path})` : 'null'
} }
`
}
const buildManifest = self.__BUILD_MANIFEST const buildManifest = self.__BUILD_MANIFEST
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
...@@ -101,6 +105,7 @@ export default async function edgeSSRLoader(this: any) { ...@@ -101,6 +105,7 @@ export default async function edgeSSRLoader(this: any) {
} }
const render = getRender({ const render = getRender({
pageType,
dev: ${dev}, dev: ${dev},
page: ${JSON.stringify(page)}, page: ${JSON.stringify(page)},
appMod, appMod,
......
...@@ -17,6 +17,7 @@ export function getRender({ ...@@ -17,6 +17,7 @@ export function getRender({
pageMod, pageMod,
errorMod, errorMod,
error500Mod, error500Mod,
pagesType,
Document, Document,
buildManifest, buildManifest,
reactLoadableManifest, reactLoadableManifest,
...@@ -28,6 +29,7 @@ export function getRender({ ...@@ -28,6 +29,7 @@ export function getRender({
config, config,
buildId, buildId,
}: { }: {
pagesType?: 'app' | 'pages' | 'root'
dev: boolean dev: boolean
page: string page: string
appMod: any appMod: any
...@@ -46,13 +48,14 @@ export function getRender({ ...@@ -46,13 +48,14 @@ export function getRender({
config: NextConfig config: NextConfig
buildId: string buildId: string
}) { }) {
const isAppPath = pagesType === 'app'
const baseLoadComponentResult = { const baseLoadComponentResult = {
dev, dev,
buildManifest, buildManifest,
reactLoadableManifest, reactLoadableManifest,
subresourceIntegrityManifest, subresourceIntegrityManifest,
Document, Document,
App: appMod.default as AppType, App: appMod?.default as AppType,
} }
const server = new WebServer({ const server = new WebServer({
...@@ -72,6 +75,7 @@ export function getRender({ ...@@ -72,6 +75,7 @@ export function getRender({
appRenderToHTML, appRenderToHTML,
pagesRenderToHTML, pagesRenderToHTML,
loadComponent: async (pathname) => { loadComponent: async (pathname) => {
if (isAppPath) return null
if (pathname === page) { if (pathname === page) {
return { return {
...baseLoadComponentResult, ...baseLoadComponentResult,
......
export default function page() {
return 'edge-ssr'
}
export const config = {
runtime: 'experimental-edge',
}
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: !!process.env.TEST_ANALYZE,
})
module.exports = withBundleAnalyzer({
experimental: {
appDir: true,
},
})
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "18.2.0", "react": "experimental",
"react-dom": "18.2.0" "react-dom": "experimental"
} }
} }
...@@ -2,12 +2,6 @@ export default function page() { ...@@ -2,12 +2,6 @@ export default function page() {
return 'edge-ssr' return 'edge-ssr'
} }
export async function getServerSideProps() {
return {
props: {},
}
}
export const config = { export const config = {
runtime: 'experimental-edge', runtime: 'experimental-edge',
} }
...@@ -32,8 +32,11 @@ const clientGlobs = [ ...@@ -32,8 +32,11 @@ const clientGlobs = [
globs: ['fetched-pages/**/*.html'], globs: ['fetched-pages/**/*.html'],
}, },
{ {
name: 'Edge SSR Page bundle Size', name: 'Edge SSR bundle Size',
globs: ['.next/server/pages/edge-ssr.js'], globs: [
'.next/server/pages/edge-ssr.js',
'.next/server/app/app-edge-ssr/page.js',
],
}, },
{ {
name: 'Middleware size', name: 'Middleware size',
...@@ -89,6 +92,9 @@ module.exports = { ...@@ -89,6 +92,9 @@ module.exports = {
path: 'next.config.js', path: 'next.config.js',
content: ` content: `
module.exports = { module.exports = {
experimental: {
appDir: true,
},
generateBuildId: () => 'BUILD_ID', generateBuildId: () => 'BUILD_ID',
webpack(config) { webpack(config) {
config.optimization.minimize = false config.optimization.minimize = false
...@@ -110,6 +116,9 @@ module.exports = { ...@@ -110,6 +116,9 @@ module.exports = {
path: 'next.config.js', path: 'next.config.js',
content: ` content: `
module.exports = { module.exports = {
experimental: {
appDir: true,
},
generateBuildId: () => 'BUILD_ID' generateBuildId: () => 'BUILD_ID'
} }
`, `,
...@@ -144,6 +153,9 @@ module.exports = { ...@@ -144,6 +153,9 @@ module.exports = {
path: 'next.config.js', path: 'next.config.js',
content: ` content: `
module.exports = { module.exports = {
experimental: {
appDir: true,
},
generateBuildId: () => 'BUILD_ID', generateBuildId: () => 'BUILD_ID',
swcMinify: true, swcMinify: true,
webpack(config) { webpack(config) {
...@@ -166,6 +178,9 @@ module.exports = { ...@@ -166,6 +178,9 @@ module.exports = {
path: 'next.config.js', path: 'next.config.js',
content: ` content: `
module.exports = { module.exports = {
experimental: {
appDir: true,
},
swcMinify: true, swcMinify: true,
generateBuildId: () => 'BUILD_ID' generateBuildId: () => 'BUILD_ID'
} }
......
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