Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bloat in main bundle from amp #37383

Merged
merged 2 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import '../server/node-polyfill-fetch'
import { requireFontManifest } from '../server/require'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { trace } from '../trace'
import { isInAmpMode } from '../shared/lib/amp'
import { isInAmpMode } from '../shared/lib/amp-mode'
import { setHttpAgentOptions } from '../server/config'
import RenderResult from '../server/render-result'
import isError from '../lib/is-error'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
STATIC_STATUS_PAGES,
} from '../shared/lib/constants'
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 { defaultHead } from '../shared/lib/head'
import { HeadManagerContext } from '../shared/lib/head-manager-context'
Expand Down
7 changes: 7 additions & 0 deletions packages/next/shared/lib/amp-mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function isInAmpMode({
ampFirst = false,
hybrid = false,
hasQuery = false,
} = {}): boolean {
return ampFirst || (hybrid && hasQuery)
}
9 changes: 1 addition & 8 deletions packages/next/shared/lib/amp.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import React from 'react'
import { AmpStateContext } from './amp-context'

export function isInAmpMode({
ampFirst = false,
hybrid = false,
hasQuery = false,
} = {}): boolean {
return ampFirst || (hybrid && hasQuery)
}
import { isInAmpMode } from './amp-mode'

export function useAmp(): boolean {
// Don't assign the context value to a variable to save bytes
Expand Down
2 changes: 1 addition & 1 deletion packages/next/shared/lib/head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useContext } from 'react'
import Effect from './side-effect'
import { AmpStateContext } from './amp-context'
import { HeadManagerContext } from './head-manager-context'
import { isInAmpMode } from './amp'
import { isInAmpMode } from './amp-mode'
import { warnOnce } from './utils'

type WithInAmpMode = {
Expand Down
17 changes: 17 additions & 0 deletions test/integration/production/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,23 @@ describe('Production Usage', () => {
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', () => {
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
Expand Down