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

Remove __NEXT_DATA__ from production builds when runtime JS is disabled #12406

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
4 changes: 2 additions & 2 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ export class NextScript extends Component<OriginProps> {

return (
<>
{staticMarkup ? null : (
{staticMarkup || disableRuntimeJS ? null : (
<script
id="__NEXT_DATA__"
type="application/json"
Expand Down Expand Up @@ -803,7 +803,7 @@ export class NextScript extends Component<OriginProps> {
)
)
: null}
{staticMarkup ? null : (
{staticMarkup || disableRuntimeJS ? null : (
<script
id="__NEXT_DATA__"
type="application/json"
Expand Down
101 changes: 101 additions & 0 deletions test/integration/disable-js/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import cheerio from 'cheerio'
import {
nextServer,
nextBuild,
startApp,
stopApp,
renderViaHTTP,
findPort,
launchApp,
killApp,
} from 'next-test-utils'

const appDir = join(__dirname, '../')
let appPort
let server
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5

const context = {}

describe('disabled runtime JS', () => {
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
app = nextServer({
dir: join(__dirname, '../'),
dev: false,
quiet: true,
})

server = await startApp(app)
context.appPort = appPort = server.address().port
})
afterAll(() => stopApp(server))

it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})

it('should not have __NEXT_DATA__ script', async () => {
const html = await renderViaHTTP(appPort, '/')

const $ = cheerio.load(html)
expect($('script#__NEXT_DATA__').length).toBe(0)
})

it('should not have scripts', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect($('script[src]').length).toBe(0)
})

it('should not have preload links', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect($('link[rel=preload]').length).toBe(0)
})
})

describe('dev mode', () => {
let appPort
let app

beforeAll(async () => {
appPort = await findPort()
app = await launchApp(join(__dirname, '../'), appPort)
})

afterAll(() => killApp(app))

it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})

it('should not have __NEXT_DATA__ script', async () => {
const html = await renderViaHTTP(appPort, '/')

const $ = cheerio.load(html)
expect($('script#__NEXT_DATA__').length).toBe(1)
})
it('should have preload links', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect($('link[rel=preload]').length).toBeGreaterThan(0)
})
it('should have a script for each preload link', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
const preloadLinks = $('link[rel=preload]')
preloadLinks.each((idx, element) => {
const url = $(element).attr('href')
expect($(`script[src="${url}"]`).length).toBe(1)
})
})
})
})