Unverified Commit aa0664cc authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add experimental pre-compiled next-server (#43198)

This implements an experimental config to leverage a pre-compiled
version of `next-server` which avoids the need for build-time tracing of
`next-server` dependencies and also optimizes the files included to only
have necessary dependencies in environments with full Next.js support.

Leveraging this config reduces `output: 'standalone'` size by `5 MB`
uncompressed and around `1MB` compressed.

<details>

<summary>Before</summary>

![CleanShot 2022-11-21 at 16 22
50@2x](https://user-images.githubusercontent.com/22380829/203184786-cbe79c45-9c4b-4d9d-9ac4-3af797f8ba87.png)


</details>

<details>

<summary>After</summary>


![CleanShot 2022-11-21 at 16 23
14@2x](https://user-images.githubusercontent.com/22380829/203184806-7abcfff3-a917-4bbd-904c-570b4096451f.png)


</details>

x-ref: [slack
thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1669062037261249?thread_ts=1668993202.171709&cid=C03KAR5DCKC)
x-ref: [slack
thread](https://vercel.slack.com/archives/C041SKJRH7W/p1665850356157749)
parent 35264c36
......@@ -879,6 +879,11 @@ export default async function build(
config: {
...config,
configFile: undefined,
...(ciEnvironment.hasNextSupport
? {
compress: false,
}
: {}),
experimental: {
...config.experimental,
trustHostHeader: ciEnvironment.hasNextSupport,
......@@ -1694,7 +1699,84 @@ export default async function build(
)
}
if (config.outputFileTracing) {
const nextServerTraceOutput = path.join(
distDir,
'next-server.js.nft.json'
)
if (config.experimental.preCompiledNextServer) {
if (!ciEnvironment.hasNextSupport) {
Log.warn(
`"experimental.preCompiledNextServer" is currently optimized for environments with Next.js support, some features may not be supported`
)
}
const nextServerPath = require.resolve('next/dist/server/next-server')
await promises.rename(nextServerPath, `${nextServerPath}.bak`)
await promises.writeFile(
nextServerPath,
`module.exports = require('next/dist/compiled/next-server/next-server.js')`
)
const glob =
require('next/dist/compiled/glob') as typeof import('next/dist/compiled/glob')
const compiledFiles: string[] = []
const compiledNextServerFolder = path.dirname(
require.resolve('next/dist/compiled/next-server/next-server.js')
)
for (const compiledFolder of [
compiledNextServerFolder,
path.join(compiledNextServerFolder, '../react'),
path.join(compiledNextServerFolder, '../react-dom'),
path.join(compiledNextServerFolder, '../scheduler'),
]) {
const globResult = glob.sync('**/*', {
cwd: compiledFolder,
dot: true,
})
await Promise.all(
globResult.map(async (file) => {
const absolutePath = path.join(compiledFolder, file)
const statResult = await promises.stat(absolutePath)
if (statResult.isFile()) {
compiledFiles.push(absolutePath)
}
})
)
}
const externalLibFiles = [
'next/dist/shared/lib/server-inserted-html.js',
'next/dist/shared/lib/router-context.js',
'next/dist/shared/lib/loadable-context.js',
'next/dist/shared/lib/image-config-context.js',
'next/dist/shared/lib/image-config.js',
'next/dist/shared/lib/head-manager-context.js',
'next/dist/shared/lib/app-router-context.js',
'next/dist/shared/lib/amp-context.js',
'next/dist/shared/lib/hooks-client-context.js',
'next/dist/shared/lib/html-context.js',
]
for (const file of externalLibFiles) {
compiledFiles.push(require.resolve(file))
}
compiledFiles.push(nextServerPath)
await promises.writeFile(
nextServerTraceOutput,
JSON.stringify({
version: 1,
files: [...new Set(compiledFiles)].map((file) =>
path.relative(distDir, file)
),
} as {
version: number
files: string[]
})
)
} else if (config.outputFileTracing) {
let nodeFileTrace: any
if (config.experimental.turbotrace) {
let binding = (await loadBindings()) as any
......@@ -1799,10 +1881,6 @@ export default async function build(
)
).filter(Boolean) as any // TypeScript doesn't like this filter
const nextServerTraceOutput = path.join(
distDir,
'next-server.js.nft.json'
)
const cachedTracePath = path.join(
distDir,
'cache/next-server.js.nft.json'
......
This diff is collapsed.
(()=>{var e={597:()=>{},762:(e,t,r)=>{var n;r(597);var i=r(522);function _interopDefaultLegacy(e){return e&&typeof e==="object"&&"default"in e?e:{default:e}}var s=_interopDefaultLegacy(i);function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||false;n.configurable=true;if("value"in n)n.writable=true;Object.defineProperty(e,n.key,n)}}function _createClass(e,t,r){if(t)_defineProperties(e.prototype,t);if(r)_defineProperties(e,r);return e}var o=typeof process!=="undefined"&&process.env&&process.env.NODE_ENV==="production";var isString=function(e){return Object.prototype.toString.call(e)==="[object String]"};var u=function(){function StyleSheet(e){var t=e===void 0?{}:e,r=t.name,n=r===void 0?"stylesheet":r,i=t.optimizeForSpeed,s=i===void 0?o:i;invariant$1(isString(n),"`name` must be a string");this._name=n;this._deletedRulePlaceholder="#"+n+"-deleted-rule____{}";invariant$1(typeof s==="boolean","`optimizeForSpeed` must be a boolean");this._optimizeForSpeed=s;this._serverSheet=undefined;this._tags=[];this._injected=false;this._rulesCount=0;var u=typeof window!=="undefined"&&document.querySelector('meta[property="csp-nonce"]');this._nonce=u?u.getAttribute("content"):null}var e=StyleSheet.prototype;e.setOptimizeForSpeed=function setOptimizeForSpeed(e){invariant$1(typeof e==="boolean","`setOptimizeForSpeed` accepts a boolean");invariant$1(this._rulesCount===0,"optimizeForSpeed cannot be when rules have already been inserted");this.flush();this._optimizeForSpeed=e;this.inject()};e.isOptimizeForSpeed=function isOptimizeForSpeed(){return this._optimizeForSpeed};e.inject=function inject(){var e=this;invariant$1(!this._injected,"sheet already injected");this._injected=true;if(typeof window!=="undefined"&&this._optimizeForSpeed){this._tags[0]=this.makeStyleTag(this._name);this._optimizeForSpeed="insertRule"in this.getSheet();if(!this._optimizeForSpeed){if(!o){console.warn("StyleSheet: optimizeForSpeed mode not supported falling back to standard mode.")}this.flush();this._injected=true}return}this._serverSheet={cssRules:[],insertRule:function(t,r){if(typeof r==="number"){e._serverSheet.cssRules[r]={cssText:t}}else{e._serverSheet.cssRules.push({cssText:t})}return r},deleteRule:function(t){e._serverSheet.cssRules[t]=null}}};e.getSheetForTag=function getSheetForTag(e){if(e.sheet){return e.sheet}for(var t=0;t<document.styleSheets.length;t++){if(document.styleSheets[t].ownerNode===e){return document.styleSheets[t]}}};e.getSheet=function getSheet(){return this.getSheetForTag(this._tags[this._tags.length-1])};e.insertRule=function insertRule(e,t){invariant$1(isString(e),"`insertRule` accepts only strings");if(typeof window==="undefined"){if(typeof t!=="number"){t=this._serverSheet.cssRules.length}this._serverSheet.insertRule(e,t);return this._rulesCount++}if(this._optimizeForSpeed){var r=this.getSheet();if(typeof t!=="number"){t=r.cssRules.length}try{r.insertRule(e,t)}catch(t){if(!o){console.warn("StyleSheet: illegal rule: \n\n"+e+"\n\nSee https://stackoverflow.com/q/20007992 for more info")}return-1}}else{var n=this._tags[t];this._tags.push(this.makeStyleTag(this._name,e,n))}return this._rulesCount++};e.replaceRule=function replaceRule(e,t){if(this._optimizeForSpeed||typeof window==="undefined"){var r=typeof window!=="undefined"?this.getSheet():this._serverSheet;if(!t.trim()){t=this._deletedRulePlaceholder}if(!r.cssRules[e]){return e}r.deleteRule(e);try{r.insertRule(t,e)}catch(n){if(!o){console.warn("StyleSheet: illegal rule: \n\n"+t+"\n\nSee https://stackoverflow.com/q/20007992 for more info")}r.insertRule(this._deletedRulePlaceholder,e)}}else{var n=this._tags[e];invariant$1(n,"old rule at index `"+e+"` not found");n.textContent=t}return e};e.deleteRule=function deleteRule(e){if(typeof window==="undefined"){this._serverSheet.deleteRule(e);return}if(this._optimizeForSpeed){this.replaceRule(e,"")}else{var t=this._tags[e];invariant$1(t,"rule at index `"+e+"` not found");t.parentNode.removeChild(t);this._tags[e]=null}};e.flush=function flush(){this._injected=false;this._rulesCount=0;if(typeof window!=="undefined"){this._tags.forEach((function(e){return e&&e.parentNode.removeChild(e)}));this._tags=[]}else{this._serverSheet.cssRules=[]}};e.cssRules=function cssRules(){var e=this;if(typeof window==="undefined"){return this._serverSheet.cssRules}return this._tags.reduce((function(t,r){if(r){t=t.concat(Array.prototype.map.call(e.getSheetForTag(r).cssRules,(function(t){return t.cssText===e._deletedRulePlaceholder?null:t})))}else{t.push(null)}return t}),[])};e.makeStyleTag=function makeStyleTag(e,t,r){if(t){invariant$1(isString(t),"makeStyleTag accepts only strings as second parameter")}var n=document.createElement("style");if(this._nonce)n.setAttribute("nonce",this._nonce);n.type="text/css";n.setAttribute("data-"+e,"");if(t){n.appendChild(document.createTextNode(t))}var i=document.head||document.getElementsByTagName("head")[0];if(r){i.insertBefore(n,r)}else{i.appendChild(n)}return n};_createClass(StyleSheet,[{key:"length",get:function get(){return this._rulesCount}}]);return StyleSheet}();function invariant$1(e,t){if(!e){throw new Error("StyleSheet: "+t+".")}}function hash(e){var t=5381,r=e.length;while(r){t=t*33^e.charCodeAt(--r)}return t>>>0}var a=hash;var sanitize=function(e){return e.replace(/\/style/gi,"\\/style")};var l={};function computeId(e,t){if(!t){return"jsx-"+e}var r=String(t);var n=e+r;if(!l[n]){l[n]="jsx-"+a(e+"-"+r)}return l[n]}function computeSelector(e,t){var r=/__jsx-style-dynamic-selector/g;if(typeof window==="undefined"){t=sanitize(t)}var n=e+t;if(!l[n]){l[n]=t.replace(r,e)}return l[n]}function mapRulesToStyle(e,t){if(t===void 0)t={};return e.map((function(e){var r=e[0];var n=e[1];return s["default"].createElement("style",{id:"__"+r,key:"__"+r,nonce:t.nonce?t.nonce:undefined,dangerouslySetInnerHTML:{__html:n}})}))}var c=function(){function StyleSheetRegistry(e){var t=e===void 0?{}:e,r=t.styleSheet,n=r===void 0?null:r,i=t.optimizeForSpeed,s=i===void 0?false:i;this._sheet=n||new u({name:"styled-jsx",optimizeForSpeed:s});this._sheet.inject();if(n&&typeof s==="boolean"){this._sheet.setOptimizeForSpeed(s);this._optimizeForSpeed=this._sheet.isOptimizeForSpeed()}this._fromServer=undefined;this._indices={};this._instancesCounts={}}var e=StyleSheetRegistry.prototype;e.add=function add(e){var t=this;if(undefined===this._optimizeForSpeed){this._optimizeForSpeed=Array.isArray(e.children);this._sheet.setOptimizeForSpeed(this._optimizeForSpeed);this._optimizeForSpeed=this._sheet.isOptimizeForSpeed()}if(typeof window!=="undefined"&&!this._fromServer){this._fromServer=this.selectFromServer();this._instancesCounts=Object.keys(this._fromServer).reduce((function(e,t){e[t]=0;return e}),{})}var r=this.getIdAndRules(e),n=r.styleId,i=r.rules;if(n in this._instancesCounts){this._instancesCounts[n]+=1;return}var s=i.map((function(e){return t._sheet.insertRule(e)})).filter((function(e){return e!==-1}));this._indices[n]=s;this._instancesCounts[n]=1};e.remove=function remove(e){var t=this;var r=this.getIdAndRules(e).styleId;invariant(r in this._instancesCounts,"styleId: `"+r+"` not found");this._instancesCounts[r]-=1;if(this._instancesCounts[r]<1){var n=this._fromServer&&this._fromServer[r];if(n){n.parentNode.removeChild(n);delete this._fromServer[r]}else{this._indices[r].forEach((function(e){return t._sheet.deleteRule(e)}));delete this._indices[r]}delete this._instancesCounts[r]}};e.update=function update(e,t){this.add(t);this.remove(e)};e.flush=function flush(){this._sheet.flush();this._sheet.inject();this._fromServer=undefined;this._indices={};this._instancesCounts={}};e.cssRules=function cssRules(){var e=this;var t=this._fromServer?Object.keys(this._fromServer).map((function(t){return[t,e._fromServer[t]]})):[];var cssRules=this._sheet.cssRules();return t.concat(Object.keys(this._indices).map((function(t){return[t,e._indices[t].map((function(e){return cssRules[e].cssText})).join(e._optimizeForSpeed?"":"\n")]})).filter((function(e){return Boolean(e[1])})))};e.styles=function styles(e){return mapRulesToStyle(this.cssRules(),e)};e.getIdAndRules=function getIdAndRules(e){var t=e.children,r=e.dynamic,n=e.id;if(r){var i=computeId(n,r);return{styleId:i,rules:Array.isArray(t)?t.map((function(e){return computeSelector(i,e)})):[computeSelector(i,t)]}}return{styleId:computeId(n),rules:Array.isArray(t)?t:[t]}};e.selectFromServer=function selectFromServer(){var e=Array.prototype.slice.call(document.querySelectorAll('[id^="__jsx-"]'));return e.reduce((function(e,t){var r=t.id.slice(2);e[r]=t;return e}),{})};return StyleSheetRegistry}();function invariant(e,t){if(!e){throw new Error("StyleSheetRegistry: "+t+".")}}var d=i.createContext(null);d.displayName="StyleSheetContext";function createStyleRegistry(){return new c}function StyleRegistry(e){var t=e.registry,r=e.children;var n=i.useContext(d);var o=i.useState((function(){return n||t||createStyleRegistry()})),u=o[0];return s["default"].createElement(d.Provider,{value:u},r)}function useStyleRegistry(){return i.useContext(d)}var h=s["default"].useInsertionEffect||s["default"].useLayoutEffect;var f=typeof window!=="undefined"?createStyleRegistry():undefined;function JSXStyle(e){var t=f?f:useStyleRegistry();if(!t){return null}if(typeof window==="undefined"){t.add(e);return null}h((function(){t.add(e);return function(){t.remove(e)}}),[e.id,String(e.dynamic)]);return null}JSXStyle.dynamic=function(e){return e.map((function(e){var t=e[0];var r=e[1];return computeId(t,r)})).join(" ")};n=StyleRegistry;n=createStyleRegistry;t.style=JSXStyle;n=useStyleRegistry},987:(e,t,r)=>{e.exports=r(762).style},522:e=>{"use strict";e.exports=require("react")}};var t={};function __nccwpck_require__(r){var n=t[r];if(n!==undefined){return n.exports}var i=t[r]={exports:{}};var s=true;try{e[r](i,i.exports,__nccwpck_require__);s=false}finally{if(s)delete t[r]}return i.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var r=__nccwpck_require__(987);module.exports=r})();
\ No newline at end of file
......@@ -339,6 +339,9 @@ const configSchema = {
pageEnv: {
type: 'boolean',
},
preCompiledNextServer: {
type: 'boolean',
},
proxyTimeout: {
minimum: 0,
type: 'number',
......
......@@ -82,6 +82,7 @@ export interface ExperimentalConfig {
fetchCache?: boolean
optimisticClientCache?: boolean
middlewarePrefetch?: 'strict' | 'flexible'
preCompiledNextServer?: boolean
legacyBrowsers?: boolean
manualClientBasePath?: boolean
newNextLinkBehavior?: boolean
......@@ -568,6 +569,7 @@ export const defaultConfig: NextConfig = {
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined,
experimental: {
preCompiledNextServer: false,
fetchCache: false,
middlewarePrefetch: 'flexible',
optimisticClientCache: true,
......
import { init as initWebpack } from 'next/dist/compiled/webpack/webpack'
let installed: boolean = false
export function loadWebpackHook() {
const { init: initWebpack } = require('next/dist/compiled/webpack/webpack')
if (installed) {
return
}
......
......@@ -833,7 +833,10 @@ export default async function loadConfig(
rawConfig?: boolean
): Promise<NextConfigComplete> {
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log)
loadWebpackHook()
if (!customConfig) {
loadWebpackHook()
}
let configFileName = 'next.config.js'
......
......@@ -51,7 +51,6 @@ import {
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
import { findDir } from '../lib/find-pages-dir'
import { format as formatUrl, UrlWithParsedQuery } from 'url'
import compression from 'next/dist/compiled/compression'
import { getPathMatch } from '../shared/lib/router/utils/path-match'
import { createHeaderRoute, createRedirectRoute } from './server-route-utils'
import getRouteFromAssetPath from '../shared/lib/router/utils/get-route-from-asset-path'
......@@ -206,6 +205,7 @@ function getEdgeMatcher(
export default class NextNodeServer extends BaseServer {
private imageResponseCache?: ResponseCache
private compression?: ExpressMiddleware
constructor(options: Options) {
// Initialize super class
......@@ -228,6 +228,10 @@ export default class NextNodeServer extends BaseServer {
process.env.__NEXT_SCRIPT_WORKERS = JSON.stringify(true)
}
if (this.nextConfig.compress) {
this.compression = require('next/dist/compiled/compression')()
}
if (!this.minimalMode) {
this.imageResponseCache = new ResponseCache(this.minimalMode)
}
......@@ -257,10 +261,6 @@ export default class NextNodeServer extends BaseServer {
setHttpClientAndAgentOptions(this.nextConfig)
}
private compression = this.nextConfig.compress
? (compression() as ExpressMiddleware)
: undefined
protected loadEnvConfig({
dev,
forceReload,
......
......@@ -88,6 +88,76 @@ export async function ncc_node_html_parser(task, opts) {
.target('src/compiled/node-html-parser')
}
export async function ncc_next_server(task, opts) {
await task
.source(
opts.src ||
relative(__dirname, require.resolve('next/dist/server/next-server'))
)
.ncc({
bundleName: 'next-server',
// minify: false,
externals: {
...externals,
sharp: 'sharp',
react: 'react',
'react-dom': 'react-dom',
'next/dist/compiled/compression': 'next/dist/compiled/compression',
critters: 'critters',
'next/dist/compiled/jest-worker': 'next/dist/compiled/jest-worker',
'next/dist/compiled/react': 'next/dist/compiled/react',
'/next/dist/compiled/react(/.+)/': 'next/dist/compiled/react$1',
'next/dist/compiled/react-dom': 'next/dist/compiled/react-dom',
'/next/dist/compiled/react-dom(/.+)/': 'next/dist/compiled/react-dom$1',
// react contexts must be external
'/(.*)server-inserted-html/':
'next/dist/shared/lib/server-inserted-html.js',
'/(.+/)router-context/': 'next/dist/shared/lib/router-context.js',
'/(.*)loadable-context/': 'next/dist/shared/lib/loadable-context.js',
'/(.*)image-config-context/':
'next/dist/shared/lib/image-config-context.js',
'/(.*)head-manager-context/':
'next/dist/shared/lib/head-manager-context.js',
'/(.*)app-router-context/':
'next/dist/shared/lib/app-router-context.js',
'/(.*)amp-context/': 'next/dist/shared/lib/amp-context.js',
'/(.*)hooks-client-context/':
'next/dist/shared/lib/hooks-client-context.js',
'/(.*)html-context/': 'next/dist/shared/lib/html-context.js',
// 'next/dist/compiled/undici': 'next/dist/compiled/undici',
// 'next/dist/compiled/node-fetch': 'next/dist/compiled/node-fetch',
// '/(.*)google-font-metrics.json/': '$1google-font-metrics.json',
'/(.*)next-config-validate.js/': '$1/next-config-validate.js',
'/(.*)server/web(.*)/': '$1server/web$2',
'./web/sandbox': './web/sandbox',
'next/dist/compiled/edge-runtime': 'next/dist/compiled/edge-runtime',
'(.*)@edge-runtime/primitives': '$1@edge-runtime/primitives',
'/(.*)compiled/webpack(/.*)/': '$1webpack$2',
'./image-optimizer': './image-optimizer',
'/(.*)@ampproject/toolbox-optimizer/':
'$1@ampproject/toolbox-optimizer',
},
})
.target('src/compiled/next-server')
}
// eslint-disable-next-line camelcase
externals['@babel/runtime'] = 'next/dist/compiled/@babel/runtime'
export async function copy_babel_runtime(task, opts) {
......@@ -1897,6 +1967,7 @@ export async function ncc(task, opts) {
.clear('compiled')
.parallel(
[
'ncc_next_server',
'ncc_node_html_parser',
'ncc_watchpack',
'ncc_chalk',
......
......@@ -293,10 +293,10 @@ describe('should set-up next', () => {
)
})
it('`compress` should be `true` by default', async () => {
it('`compress` should be `false` in nextEnv', async () => {
expect(
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
).toContain('"compress":true')
).toContain('"compress":false')
})
it('should output middleware correctly', async () => {
......@@ -1287,5 +1287,10 @@ describe('should set-up next', () => {
const res = await fetchViaHTTP(appPort, '/')
expect(res.status).toBe(200)
expect(await res.text()).toContain('index page')
// when not in next env should be compress: true
expect(
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
).toContain('"compress":true')
})
})
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