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 @@
import './app-webpack'
import { appBootstrap } from './app-bootstrap'
import { initializeDevBuildIndicatorForAppRouter } from './dev/dev-build-indicator/initialize-for-app-router'
appBootstrap(() => {
const { hydrate } = require('./app-index')
hydrate()
initializeDevBuildIndicatorForAppRouter()
})
// TODO-APP: build indicator
......@@ -8,6 +8,10 @@ window.next.version += '-turbo'
appBootstrap(() => {
const { hydrate } = require('./app-index')
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'
import { useUntrackedPathname } from '../../navigation-untracked'
import { getReactStitchedError } from '../internal/helpers/stitched-error'
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 {
onBuildOk(): void
......@@ -676,6 +677,7 @@ export default function HotReload({
const handler = (event: MessageEvent<any>) => {
try {
const obj = JSON.parse(event.data)
handleDevBuildIndicatorHmrEvents(obj)
processMessage(
obj,
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 { 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. */
export const initializeDevBuildIndicatorForPageRouter = () => {
......@@ -12,22 +12,5 @@ export const initializeDevBuildIndicatorForPageRouter = () => {
// Add message listener specifically for Pages Router to handle lifecycle events
// related to dev builds (building, built, sync)
addMessageListener((obj) => {
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 {}
})
addMessageListener(handleDevBuildIndicatorHmrEvents)
}
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', () => {
}
})
describe('Enabled', () => {
describe.each(['pages', 'app'])('Enabled - (%s)', (pagesOrApp) => {
beforeAll(async () => {
await fs.remove(nextConfig)
appPort = await findPort()
......@@ -68,14 +68,20 @@ describe('Build Activity Indicator', () => {
afterAll(() => killApp(app))
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')
expect(html).toMatch(/__next-build-indicator/)
await browser.close()
})
;(process.env.TURBOPACK ? describe.skip : describe)('webpack only', () => {
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 browser.elementByCss('#to-a').click()
await waitFor(500)
......@@ -86,9 +92,15 @@ describe('Build Activity Indicator', () => {
})
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)
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 newContent = origContent.replace('b', 'c')
......@@ -102,26 +114,33 @@ describe('Build Activity Indicator', () => {
})
})
describe('Disabled with next.config.js', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
'module.exports = { devIndicators: { buildActivity: false } }',
'utf8'
)
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
describe.each(['pages', 'app'])(
'Disabled with next.config.js - (%s)',
(pagesOrApp) => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
'module.exports = { devIndicators: { buildActivity: false } }',
'utf8'
)
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
it('Does not add the build indicator container', async () => {
const browser = await webdriver(appPort, '/')
const html = await browser.eval('document.body.innerHTML')
expect(html).not.toMatch(/__next-build-indicator/)
await browser.close()
})
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
it('Does not add the build indicator container', async () => {
const browser = await webdriver(
appPort,
pagesOrApp === 'pages' ? '/' : '/app'
)
const html = await browser.eval('document.body.innerHTML')
expect(html).not.toMatch(/__next-build-indicator/)
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