From 666ed624d547f3ed10b90fd7b6994ffba4ff14fc Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 31 Oct 2019 06:35:39 -0500 Subject: [PATCH 1/6] Don't rely on page-config plugin to detect iSSG usage --- packages/next/build/babel/plugins/next-page-config.ts | 2 +- .../plugins/terser-webpack-plugin/src/index.js | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/next/build/babel/plugins/next-page-config.ts b/packages/next/build/babel/plugins/next-page-config.ts index 9cedf8ba88d42..812cc9923bd8f 100644 --- a/packages/next/build/babel/plugins/next-page-config.ts +++ b/packages/next/build/babel/plugins/next-page-config.ts @@ -4,12 +4,12 @@ import * as BabelTypes from '@babel/types' import { PageConfig } from '../../../types' export const dropBundleIdentifier = '__NEXT_DROP_CLIENT_FILE__' +export const prerenderId = '__NEXT_SPR' export const sprStatus = { used: false } const configKeys = new Set(['amp']) const pageComponentVar = '__NEXT_COMP' // this value can't be optimized by terser so the shorter the better -const prerenderId = '__NEXT_SPR' const EXPORT_NAME_GET_STATIC_PROPS = 'unstable_getStaticProps' const EXPORT_NAME_GET_STATIC_PARAMS = 'unstable_getStaticParams' diff --git a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js index 4302cb344e4d1..314d1bb17a573 100644 --- a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js +++ b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js @@ -6,7 +6,10 @@ import { SourceMapConsumer } from 'source-map' import { SourceMapSource, RawSource } from 'webpack-sources' import { RequestShortener } from 'webpack' import TaskRunner from './TaskRunner' -import { sprStatus } from '../../../../babel/plugins/next-page-config' +import { + prerenderId, + sprStatus, +} from '../../../../babel/plugins/next-page-config' const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/ @@ -177,6 +180,12 @@ export class TerserPlugin { inputSourceMap = null } + // if we are using babel cache we can't rely on the page-config + // plugin to update this value so check for it manually + if (input.indexOf(prerenderId) > -1) { + sprStatus.used = true + } + // force dead-code elimination for SPR related code if not used const { compress } = this.options.terserOptions if (compress) { From b0d868bb35431697aaeb2e3d97d56a489e82f1ca Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 31 Oct 2019 07:18:03 -0500 Subject: [PATCH 2/6] Make sure to only update sprStatus if page file --- .../build/webpack/plugins/terser-webpack-plugin/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js index 314d1bb17a573..f7d3238810917 100644 --- a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js +++ b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js @@ -182,7 +182,7 @@ export class TerserPlugin { // if we are using babel cache we can't rely on the page-config // plugin to update this value so check for it manually - if (input.indexOf(prerenderId) > -1) { + if (file.match(/pages(\\|\/)/) && input.indexOf(prerenderId) > -1) { sprStatus.used = true } From 03b31cc1d828beba58209eba5b65042804a56c72 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 31 Oct 2019 14:37:41 -0500 Subject: [PATCH 3/6] Remove dead-code elimination for iSSG code --- .../plugins/terser-webpack-plugin/src/index.js | 15 --------------- packages/next/next-server/lib/router/router.ts | 6 +----- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js index f7d3238810917..e6e2368ad4dc2 100644 --- a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js +++ b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js @@ -180,21 +180,6 @@ export class TerserPlugin { inputSourceMap = null } - // if we are using babel cache we can't rely on the page-config - // plugin to update this value so check for it manually - if (file.match(/pages(\\|\/)/) && input.indexOf(prerenderId) > -1) { - sprStatus.used = true - } - - // force dead-code elimination for SPR related code if not used - const { compress } = this.options.terserOptions - if (compress) { - if (!compress.global_defs) { - compress.global_defs = {} - } - compress.global_defs['self.__HAS_SPR'] = !!sprStatus.used - } - const task = { file, input, diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index 1186fa10f971a..d0012b7123477 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -625,11 +625,7 @@ export default class Router implements BaseRouter { const { Component: App } = this.components['/_app'] let props - if ( - // @ts-ignore workaround for dead-code elimination - (self.__HAS_SPR || process.env.NODE_ENV !== 'production') && - (Component as any).__NEXT_SPR - ) { + if ((Component as any).__NEXT_SPR) { let status: any // pathname should have leading slash let { pathname } = parse(ctx.asPath || ctx.pathname) From e892804f1394f335777b055978acfd027713258b Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Thu, 31 Oct 2019 15:42:54 -0400 Subject: [PATCH 4/6] Revert next-page-config.ts --- packages/next/build/babel/plugins/next-page-config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/build/babel/plugins/next-page-config.ts b/packages/next/build/babel/plugins/next-page-config.ts index 812cc9923bd8f..9cedf8ba88d42 100644 --- a/packages/next/build/babel/plugins/next-page-config.ts +++ b/packages/next/build/babel/plugins/next-page-config.ts @@ -4,12 +4,12 @@ import * as BabelTypes from '@babel/types' import { PageConfig } from '../../../types' export const dropBundleIdentifier = '__NEXT_DROP_CLIENT_FILE__' -export const prerenderId = '__NEXT_SPR' export const sprStatus = { used: false } const configKeys = new Set(['amp']) const pageComponentVar = '__NEXT_COMP' // this value can't be optimized by terser so the shorter the better +const prerenderId = '__NEXT_SPR' const EXPORT_NAME_GET_STATIC_PROPS = 'unstable_getStaticProps' const EXPORT_NAME_GET_STATIC_PARAMS = 'unstable_getStaticParams' From 5e27a30fe237a6ce26ab23269f58ea22eabf5388 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Thu, 31 Oct 2019 15:43:17 -0400 Subject: [PATCH 5/6] Update index.js --- .../build/webpack/plugins/terser-webpack-plugin/src/index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js index e6e2368ad4dc2..7e37f8dafe3a8 100644 --- a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js +++ b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js @@ -6,10 +6,6 @@ import { SourceMapConsumer } from 'source-map' import { SourceMapSource, RawSource } from 'webpack-sources' import { RequestShortener } from 'webpack' import TaskRunner from './TaskRunner' -import { - prerenderId, - sprStatus, -} from '../../../../babel/plugins/next-page-config' const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/ From 0ac212e170e606c466eaf9f24bca694750b7dc81 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Thu, 31 Oct 2019 15:55:07 -0400 Subject: [PATCH 6/6] Update index.test.js --- test/integration/size-limit/test/index.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/size-limit/test/index.test.js b/test/integration/size-limit/test/index.test.js index 248cadf3523f6..ae9a35257f980 100644 --- a/test/integration/size-limit/test/index.test.js +++ b/test/integration/size-limit/test/index.test.js @@ -81,7 +81,7 @@ describe('Production response size', () => { ) // These numbers are without gzip compression! - expect(responseSizeKilobytes).toBeLessThanOrEqual(221) // Kilobytes + expect(responseSizeKilobytes).toBeLessThanOrEqual(222) // Kilobytes }) it('should not increase the overall response size of modern build', async () => { @@ -99,6 +99,6 @@ describe('Production response size', () => { ) // These numbers are without gzip compression! - expect(responseSizeKilobytes).toBeLessThanOrEqual(199) // Kilobytes + expect(responseSizeKilobytes).toBeLessThanOrEqual(200) // Kilobytes }) })