Unverified Commit b7f35380 authored by Jude Gao's avatar Jude Gao Committed by GitHub

feat: dev build indicator for App Router (#74618)

Add the implementation of
https://nextjs.org/docs/app/api-reference/config/next-config-js/devIndicators
to app router.
Co-authored-by: 's avatarvercel-release-bot <infra+release@vercel.com>
parent 34c991ce
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
import './app-webpack' import './app-webpack'
import { appBootstrap } from './app-bootstrap' import { appBootstrap } from './app-bootstrap'
import { initializeDevBuildIndicatorForAppRouter } from './dev/dev-build-indicator/initialize-for-app-router'
appBootstrap(() => { appBootstrap(() => {
const { hydrate } = require('./app-index') const { hydrate } = require('./app-index')
hydrate() hydrate()
initializeDevBuildIndicatorForAppRouter()
}) })
// TODO-APP: build indicator
...@@ -8,6 +8,10 @@ window.next.version += '-turbo' ...@@ -8,6 +8,10 @@ window.next.version += '-turbo'
appBootstrap(() => { appBootstrap(() => {
const { hydrate } = require('./app-index') const { hydrate } = require('./app-index')
hydrate() hydrate()
})
// TODO-APP: build indicator if (process.env.NODE_ENV !== 'production') {
const { initializeDevBuildIndicatorForAppRouter } =
require('./dev/dev-build-indicator/initialize-for-app-router') as typeof import('./dev/dev-build-indicator/initialize-for-app-router')
initializeDevBuildIndicatorForAppRouter()
}
})
...@@ -46,6 +46,7 @@ import type { DebugInfo } from '../types' ...@@ -46,6 +46,7 @@ import type { DebugInfo } from '../types'
import { useUntrackedPathname } from '../../navigation-untracked' import { useUntrackedPathname } from '../../navigation-untracked'
import { getReactStitchedError } from '../internal/helpers/stitched-error' import { getReactStitchedError } from '../internal/helpers/stitched-error'
import { shouldRenderRootLevelErrorOverlay } from '../../../lib/is-error-thrown-while-rendering-rsc' import { shouldRenderRootLevelErrorOverlay } from '../../../lib/is-error-thrown-while-rendering-rsc'
import { handleDevBuildIndicatorHmrEvents } from '../../../dev/dev-build-indicator/internal/handle-dev-build-indicator-hmr-events'
export interface Dispatcher { export interface Dispatcher {
onBuildOk(): void onBuildOk(): void
...@@ -676,6 +677,7 @@ export default function HotReload({ ...@@ -676,6 +677,7 @@ export default function HotReload({
const handler = (event: MessageEvent<any>) => { const handler = (event: MessageEvent<any>) => {
try { try {
const obj = JSON.parse(event.data) const obj = JSON.parse(event.data)
handleDevBuildIndicatorHmrEvents(obj)
processMessage( processMessage(
obj, obj,
sendMessage, sendMessage,
......
import { devBuildIndicator } from './internal/dev-build-indicator'
/** Integrates the generic dev build indicator with the App Router. */
export const initializeDevBuildIndicatorForAppRouter = () => {
if (!process.env.__NEXT_BUILD_INDICATOR) {
return
}
devBuildIndicator.initialize(process.env.__NEXT_BUILD_INDICATOR_POSITION)
}
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../../../server/dev/hot-reloader-types'
import { addMessageListener } from '../../components/react-dev-overlay/pages/websocket' import { addMessageListener } from '../../components/react-dev-overlay/pages/websocket'
import { devBuildIndicator } from './internal/dev-build-indicator' import { devBuildIndicator } from './internal/dev-build-indicator'
import { handleDevBuildIndicatorHmrEvents } from './internal/handle-dev-build-indicator-hmr-events'
/** Integrates the generic dev build indicator with the Pages Router. */ /** Integrates the generic dev build indicator with the Pages Router. */
export const initializeDevBuildIndicatorForPageRouter = () => { export const initializeDevBuildIndicatorForPageRouter = () => {
...@@ -12,22 +12,5 @@ export const initializeDevBuildIndicatorForPageRouter = () => { ...@@ -12,22 +12,5 @@ export const initializeDevBuildIndicatorForPageRouter = () => {
// Add message listener specifically for Pages Router to handle lifecycle events // Add message listener specifically for Pages Router to handle lifecycle events
// related to dev builds (building, built, sync) // related to dev builds (building, built, sync)
addMessageListener((obj) => { addMessageListener(handleDevBuildIndicatorHmrEvents)
try {
if (!('action' in obj)) {
return
}
// eslint-disable-next-line default-case
switch (obj.action) {
case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING:
devBuildIndicator.show()
break
case HMR_ACTIONS_SENT_TO_BROWSER.BUILT:
case HMR_ACTIONS_SENT_TO_BROWSER.SYNC:
devBuildIndicator.hide()
break
}
} catch {}
})
} }
import {
HMR_ACTIONS_SENT_TO_BROWSER,
type HMR_ACTION_TYPES,
} from '../../../../server/dev/hot-reloader-types'
import { devBuildIndicator } from './dev-build-indicator'
/**
* Handles HMR events to control the dev build indicator visibility.
* Shows indicator when building and hides it when build completes or syncs.
*/
export const handleDevBuildIndicatorHmrEvents = (obj: HMR_ACTION_TYPES) => {
try {
if (!('action' in obj)) {
return
}
// eslint-disable-next-line default-case
switch (obj.action) {
case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING:
devBuildIndicator.show()
break
case HMR_ACTIONS_SENT_TO_BROWSER.BUILT:
case HMR_ACTIONS_SENT_TO_BROWSER.SYNC:
devBuildIndicator.hide()
break
}
} catch {}
}
export default function Page() {
return <div>a</div>
}
'use client'
export default function Page() {
return <div>b</div>
}
import Link from 'next/link'
export default function Page() {
return (
<>
<Link href="/app/a" id="to-a">
Go to a
</Link>
<Link href="/app/b" id="to-b">
Go to b
</Link>
</>
)
}
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
...@@ -59,7 +59,7 @@ describe('Build Activity Indicator', () => { ...@@ -59,7 +59,7 @@ describe('Build Activity Indicator', () => {
} }
}) })
describe('Enabled', () => { describe.each(['pages', 'app'])('Enabled - (%s)', (pagesOrApp) => {
beforeAll(async () => { beforeAll(async () => {
await fs.remove(nextConfig) await fs.remove(nextConfig)
appPort = await findPort() appPort = await findPort()
...@@ -68,14 +68,20 @@ describe('Build Activity Indicator', () => { ...@@ -68,14 +68,20 @@ describe('Build Activity Indicator', () => {
afterAll(() => killApp(app)) afterAll(() => killApp(app))
it('Adds the build indicator container', async () => { it('Adds the build indicator container', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(
appPort,
pagesOrApp === 'pages' ? '/' : '/app'
)
const html = await browser.eval('document.body.innerHTML') const html = await browser.eval('document.body.innerHTML')
expect(html).toMatch(/__next-build-indicator/) expect(html).toMatch(/__next-build-indicator/)
await browser.close() await browser.close()
}) })
;(process.env.TURBOPACK ? describe.skip : describe)('webpack only', () => { ;(process.env.TURBOPACK ? describe.skip : describe)('webpack only', () => {
it('Shows the build indicator when a page is built during navigation', async () => { it('Shows the build indicator when a page is built during navigation', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(
appPort,
pagesOrApp === 'pages' ? '/' : '/app'
)
await installCheckVisible(browser) await installCheckVisible(browser)
await browser.elementByCss('#to-a').click() await browser.elementByCss('#to-a').click()
await waitFor(500) await waitFor(500)
...@@ -86,9 +92,15 @@ describe('Build Activity Indicator', () => { ...@@ -86,9 +92,15 @@ describe('Build Activity Indicator', () => {
}) })
it('Shows build indicator when page is built from modifying', async () => { it('Shows build indicator when page is built from modifying', async () => {
const browser = await webdriver(appPort, '/b') const browser = await webdriver(
appPort,
pagesOrApp === 'pages' ? '/b' : '/app/b'
)
await installCheckVisible(browser) await installCheckVisible(browser)
const pagePath = join(appDir, 'pages/b.js') const pagePath = join(
appDir,
pagesOrApp === 'pages' ? 'pages/b.js' : 'app/app/b/page.js'
)
const origContent = await fs.readFile(pagePath, 'utf8') const origContent = await fs.readFile(pagePath, 'utf8')
const newContent = origContent.replace('b', 'c') const newContent = origContent.replace('b', 'c')
...@@ -102,7 +114,9 @@ describe('Build Activity Indicator', () => { ...@@ -102,7 +114,9 @@ describe('Build Activity Indicator', () => {
}) })
}) })
describe('Disabled with next.config.js', () => { describe.each(['pages', 'app'])(
'Disabled with next.config.js - (%s)',
(pagesOrApp) => {
beforeAll(async () => { beforeAll(async () => {
await fs.writeFile( await fs.writeFile(
nextConfig, nextConfig,
...@@ -112,16 +126,21 @@ describe('Build Activity Indicator', () => { ...@@ -112,16 +126,21 @@ describe('Build Activity Indicator', () => {
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
}) })
afterAll(async () => { afterAll(async () => {
await killApp(app) await killApp(app)
await fs.remove(nextConfig) await fs.remove(nextConfig)
}) })
it('Does not add the build indicator container', async () => { it('Does not add the build indicator container', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(
appPort,
pagesOrApp === 'pages' ? '/' : '/app'
)
const html = await browser.eval('document.body.innerHTML') const html = await browser.eval('document.body.innerHTML')
expect(html).not.toMatch(/__next-build-indicator/) expect(html).not.toMatch(/__next-build-indicator/)
await browser.close() await browser.close()
}) })
}) }
)
}) })
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