Unverified Commit ccd3df2f authored by Steven's avatar Steven Committed by GitHub

Fix bloat in main bundle from amp (#37383)

- Related to #35900 
- Related to #36702 
- Related to #35907 
parent 129052dc
...@@ -19,7 +19,7 @@ import '../server/node-polyfill-fetch' ...@@ -19,7 +19,7 @@ import '../server/node-polyfill-fetch'
import { requireFontManifest } from '../server/require' import { requireFontManifest } from '../server/require'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { trace } from '../trace' import { trace } from '../trace'
import { isInAmpMode } from '../shared/lib/amp' import { isInAmpMode } from '../shared/lib/amp-mode'
import { setHttpAgentOptions } from '../server/config' import { setHttpAgentOptions } from '../server/config'
import RenderResult from '../server/render-result' import RenderResult from '../server/render-result'
import isError from '../lib/is-error' import isError from '../lib/is-error'
......
...@@ -43,7 +43,7 @@ import { ...@@ -43,7 +43,7 @@ import {
STATIC_STATUS_PAGES, STATIC_STATUS_PAGES,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { isSerializableProps } from '../lib/is-serializable-props' import { isSerializableProps } from '../lib/is-serializable-props'
import { isInAmpMode } from '../shared/lib/amp' import { isInAmpMode } from '../shared/lib/amp-mode'
import { AmpStateContext } from '../shared/lib/amp-context' import { AmpStateContext } from '../shared/lib/amp-context'
import { defaultHead } from '../shared/lib/head' import { defaultHead } from '../shared/lib/head'
import { HeadManagerContext } from '../shared/lib/head-manager-context' import { HeadManagerContext } from '../shared/lib/head-manager-context'
......
export function isInAmpMode({
ampFirst = false,
hybrid = false,
hasQuery = false,
} = {}): boolean {
return ampFirst || (hybrid && hasQuery)
}
import React from 'react' import React from 'react'
import { AmpStateContext } from './amp-context' import { AmpStateContext } from './amp-context'
import { isInAmpMode } from './amp-mode'
export function isInAmpMode({
ampFirst = false,
hybrid = false,
hasQuery = false,
} = {}): boolean {
return ampFirst || (hybrid && hasQuery)
}
export function useAmp(): boolean { export function useAmp(): boolean {
// Don't assign the context value to a variable to save bytes // Don't assign the context value to a variable to save bytes
......
...@@ -2,7 +2,7 @@ import React, { useContext } from 'react' ...@@ -2,7 +2,7 @@ import React, { useContext } from 'react'
import Effect from './side-effect' import Effect from './side-effect'
import { AmpStateContext } from './amp-context' import { AmpStateContext } from './amp-context'
import { HeadManagerContext } from './head-manager-context' import { HeadManagerContext } from './head-manager-context'
import { isInAmpMode } from './amp' import { isInAmpMode } from './amp-mode'
import { warnOnce } from './utils' import { warnOnce } from './utils'
type WithInAmpMode = { type WithInAmpMode = {
......
...@@ -313,6 +313,23 @@ describe('Production Usage', () => { ...@@ -313,6 +313,23 @@ describe('Production Usage', () => {
expect(content).not.toContain('.currentScript') expect(content).not.toContain('.currentScript')
}) })
it('should not contain useAmp in main chunk', async () => {
const globResult = await glob('main-*.js', {
cwd: join(appDir, '.next/static/chunks'),
})
if (!globResult || globResult.length !== 1) {
throw new Error('could not find main js chunk')
}
const content = await fs.readFile(
join(appDir, '.next/static/chunks', globResult[0]),
'utf8'
)
expect(content).not.toContain('useAmp')
})
describe('With basic usage', () => { describe('With basic usage', () => {
it('should render the page', async () => { it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
......
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