Unverified Commit 47ceda3c authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[cache components] persist cache bypass UI until it's disabled (#85190)

Previously we were only showing this during navigations when caches are
bypassed. We want to make it clearer that you are in this state and dev
might not work as intended while caches are disabled.

This will show the cache bypass status on initial load and will reset
the next time a router action occurs that doesn't bypass cache.


https://github.com/user-attachments/assets/f82a98c6-b5be-44d0-ba86-e4f7ca7010e4
parent b66c1d61
...@@ -45,17 +45,14 @@ export function NextLogo({ ...@@ -45,17 +45,14 @@ export function NextLogo({
const isCacheFilling = state.cacheIndicator === 'filling' const isCacheFilling = state.cacheIndicator === 'filling'
const isCacheBypassing = state.cacheIndicator === 'bypass' const isCacheBypassing = state.cacheIndicator === 'bypass'
// Determine if we should show any status // Determine if we should show any status (excluding cache bypass, which renders like error badge)
const shouldShowStatus = const shouldShowStatus =
state.buildingIndicator || state.buildingIndicator || state.renderingIndicator || isCacheFilling
state.renderingIndicator ||
isCacheFilling ||
(isCacheBypassing && state.renderingIndicator)
// Delay showing for 400ms to catch fast operations, // Delay showing for 400ms to catch fast operations,
// and keep visible for minimum time (longer for warnings) // and keep visible for minimum time (longer for warnings)
const { rendered: showStatusIndicator } = useDelayedRender(shouldShowStatus, { const { rendered: showStatusIndicator } = useDelayedRender(shouldShowStatus, {
enterDelay: isCacheBypassing && state.renderingIndicator ? 0 : 400, // Bypass warning shows immediately, others delayed enterDelay: 400,
exitDelay: 500, exitDelay: 500,
}) })
...@@ -72,7 +69,10 @@ export function NextLogo({ ...@@ -72,7 +69,10 @@ export function NextLogo({
const displayStatus = showStatusIndicator ? currentStatus : Status.None const displayStatus = showStatusIndicator ? currentStatus : Status.None
const isExpanded = const isExpanded =
isErrorExpanded || showStatusIndicator || state.disableDevIndicator isErrorExpanded ||
isCacheBypassing ||
showStatusIndicator ||
state.disableDevIndicator
const width = measuredWidth === 0 ? 'auto' : measuredWidth const width = measuredWidth === 0 ? 'auto' : measuredWidth
return ( return (
...@@ -175,12 +175,12 @@ export function NextLogo({ ...@@ -175,12 +175,12 @@ export function NextLogo({
} }
} }
&[data-status='cache-bypassing'] { &[data-cache-bypassing='true']:not([data-error='true']) {
background: rgba(251, 211, 141, 0.95); /* Warm amber background */ background: rgba(217, 119, 6, 0.95);
--color-inner-border: rgba(245, 158, 11, 0.8); --color-inner-border: rgba(245, 158, 11, 0.9);
[data-indicator-status] { [data-issues-open] {
color: rgba(92, 45, 10, 1); /* Dark brown text */ color: white;
} }
} }
...@@ -378,7 +378,8 @@ export function NextLogo({ ...@@ -378,7 +378,8 @@ export function NextLogo({
data-next-badge data-next-badge
data-error={hasError} data-error={hasError}
data-error-expanded={isExpanded} data-error-expanded={isExpanded}
data-status={hasError ? Status.None : currentStatus} data-status={hasError || isCacheBypassing ? Status.None : currentStatus}
data-cache-bypassing={isCacheBypassing}
data-animate={newErrorDetected} data-animate={newErrorDetected}
style={{ width }} style={{ width }}
> >
...@@ -397,7 +398,10 @@ export function NextLogo({ ...@@ -397,7 +398,10 @@ export function NextLogo({
aria-label={`${isMenuOpen ? 'Close' : 'Open'} Next.js Dev Tools`} aria-label={`${isMenuOpen ? 'Close' : 'Open'} Next.js Dev Tools`}
data-nextjs-dev-tools-button data-nextjs-dev-tools-button
style={{ style={{
display: showStatusIndicator && !hasError ? 'none' : 'flex', display:
showStatusIndicator && !hasError && !isCacheBypassing
? 'none'
: 'flex',
}} }}
{...buttonProps} {...buttonProps}
> >
...@@ -472,11 +476,22 @@ export function NextLogo({ ...@@ -472,11 +476,22 @@ export function NextLogo({
)} )}
</div> </div>
)} )}
{/* Status indicator shown when no errors */} {/* Cache bypass badge shown when cache is being bypassed */}
{isCacheBypassing && !hasError && !state.disableDevIndicator && (
<CacheBypassBadge
onTriggerClick={onTriggerClick}
triggerRef={triggerRef}
/>
)}
{/* Status indicator shown when no errors and no cache bypass */}
{showStatusIndicator && {showStatusIndicator &&
!hasError && !hasError &&
!isCacheBypassing &&
!state.disableDevIndicator && ( !state.disableDevIndicator && (
<StatusIndicator status={displayStatus} /> <StatusIndicator
status={displayStatus}
onClick={onTriggerClick}
/>
)} )}
</> </>
)} )}
...@@ -507,6 +522,43 @@ function AnimateCount({ ...@@ -507,6 +522,43 @@ function AnimateCount({
) )
} }
function CacheBypassBadge({
onTriggerClick,
triggerRef,
}: {
onTriggerClick: () => void
triggerRef: React.RefObject<HTMLButtonElement | null>
}) {
const [dismissed, setDismissed] = useState(false)
if (dismissed) {
return null
}
return (
<div data-issues data-cache-bypass-badge>
<button
data-issues-open
aria-label="Open Next.js Dev Tools"
onClick={onTriggerClick}
>
Cache disabled
</button>
<button
data-issues-collapse
aria-label="Collapse cache bypass badge"
onClick={() => {
setDismissed(true)
// Move focus to the trigger to prevent having it stuck on this element
triggerRef.current?.focus()
}}
>
<Cross data-cross />
</button>
</div>
)
}
function NextMark() { function NextMark() {
return ( return (
<svg width="40" height="40" viewBox="0 0 40 40" fill="none"> <svg width="40" height="40" viewBox="0 0 40 40" fill="none">
......
...@@ -15,18 +15,15 @@ export function getCurrentStatus( ...@@ -15,18 +15,15 @@ export function getCurrentStatus(
cacheIndicator: CacheIndicatorState cacheIndicator: CacheIndicatorState
): Status { ): Status {
const isCacheFilling = cacheIndicator === 'filling' const isCacheFilling = cacheIndicator === 'filling'
const isCacheBypassing = cacheIndicator === 'bypass'
// Priority order: cache bypassing > prerendering > compiling > rendering // Priority order: compiling > prerendering > rendering
if (isCacheBypassing && renderingIndicator) { // Note: cache bypassing is now handled as a badge, not a status indicator
return Status.CacheBypassing if (buildingIndicator) {
return Status.Compiling
} }
if (isCacheFilling) { if (isCacheFilling) {
return Status.Prerendering return Status.Prerendering
} }
if (buildingIndicator) {
return Status.Compiling
}
if (renderingIndicator) { if (renderingIndicator) {
return Status.Rendering return Status.Rendering
} }
...@@ -35,9 +32,10 @@ export function getCurrentStatus( ...@@ -35,9 +32,10 @@ export function getCurrentStatus(
interface StatusIndicatorProps { interface StatusIndicatorProps {
status: Status status: Status
onClick?: () => void
} }
export function StatusIndicator({ status }: StatusIndicatorProps) { export function StatusIndicator({ status, onClick }: StatusIndicatorProps) {
const statusText: Record<Status, string> = { const statusText: Record<Status, string> = {
[Status.None]: '', [Status.None]: '',
[Status.CacheBypassing]: 'Cache disabled', [Status.CacheBypassing]: 'Cache disabled',
...@@ -78,6 +76,15 @@ export function StatusIndicator({ status }: StatusIndicatorProps) { ...@@ -78,6 +76,15 @@ export function StatusIndicator({ status }: StatusIndicatorProps) {
font-size: var(--size-13); font-size: var(--size-13);
font-weight: 500; font-weight: 500;
white-space: nowrap; white-space: nowrap;
border: none;
background: transparent;
cursor: pointer;
outline: none;
}
[data-indicator-status]:focus-visible {
outline: 2px solid var(--color-blue-800, #3b82f6);
outline-offset: 3px;
} }
[data-status-dot] { [data-status-dot] {
...@@ -149,7 +156,11 @@ export function StatusIndicator({ status }: StatusIndicatorProps) { ...@@ -149,7 +156,11 @@ export function StatusIndicator({ status }: StatusIndicatorProps) {
} }
`} `}
</style> </style>
<div data-indicator-status> <button
data-indicator-status
onClick={onClick}
aria-label="Open Next.js Dev Tools"
>
{statusDotColor[status] && ( {statusDotColor[status] && (
<div <div
data-status-dot data-status-dot
...@@ -161,29 +172,34 @@ export function StatusIndicator({ status }: StatusIndicatorProps) { ...@@ -161,29 +172,34 @@ export function StatusIndicator({ status }: StatusIndicatorProps) {
<AnimateStatusText <AnimateStatusText
key={status} // Key here triggers re-mount and animation key={status} // Key here triggers re-mount and animation
statusKey={status} statusKey={status}
showEllipsis={status !== Status.CacheBypassing}
> >
{statusText[status]} {statusText[status]}
</AnimateStatusText> </AnimateStatusText>
</div> </button>
</> </>
) )
} }
function AnimateStatusText({ function AnimateStatusText({
children: text, children: text,
showEllipsis = true,
}: { }: {
children: string children: string
statusKey?: string // Keep for type compatibility but unused statusKey?: string // Keep for type compatibility but unused
showEllipsis?: boolean
}) { }) {
return ( return (
<div data-status-text-animation> <div data-status-text-animation>
<div data-status-text-enter> <div data-status-text-enter>
{text} {text}
<span data-status-ellipsis> {showEllipsis && (
<span>.</span> <span data-status-ellipsis>
<span>.</span> <span>.</span>
<span>.</span> <span>.</span>
</span> <span>.</span>
</span>
)}
</div> </div>
</div> </div>
) )
......
...@@ -2477,6 +2477,10 @@ async function renderToStream( ...@@ -2477,6 +2477,10 @@ async function renderToStream(
if (isBypassingCachesInDev(renderOpts, requestStore)) { if (isBypassingCachesInDev(renderOpts, requestStore)) {
// Mark the RSC payload to indicate that caches were bypassed in dev. // Mark the RSC payload to indicate that caches were bypassed in dev.
// This lets the client know not to cache anything based on this render. // This lets the client know not to cache anything based on this render.
if (renderOpts.setCacheStatus) {
// we know this is available when cacheComponents is enabled, but typeguard to be safe
renderOpts.setCacheStatus('bypass', htmlRequestId, requestId)
}
payload._bypassCachesInDev = createElement(WarnForBypassCachesInDev, { payload._bypassCachesInDev = createElement(WarnForBypassCachesInDev, {
route: workStore.route, route: workStore.route,
}) })
......
...@@ -39,5 +39,128 @@ describe('cache-indicator', () => { ...@@ -39,5 +39,128 @@ describe('cache-indicator', () => {
const status = await badge.getAttribute('data-status') const status = await badge.getAttribute('data-status')
expect(status).toBe('none') expect(status).toBe('none')
}) })
it('shows cache-bypassing badge when cache is disabled', async () => {
const browser = await next.browser('/', {
extraHTTPHeaders: { 'cache-control': 'no-cache' },
})
// Wait for the badge to appear and show cache-bypassing status
await retry(async () => {
const badge = await browser.elementByCss('[data-next-badge]')
const cacheBypassingAttr = await badge.getAttribute(
'data-cache-bypassing'
)
expect(cacheBypassingAttr).toBe('true')
})
// Verify the cache bypass badge is visible
await retry(async () => {
const hasCacheBypassBadge = await browser.hasElementByCss(
'[data-cache-bypass-badge]'
)
expect(hasCacheBypassBadge).toBe(true)
})
// Verify the badge shows "Cache disabled" text
const badgeButton = await browser.elementByCss(
'[data-cache-bypass-badge] [data-issues-open]'
)
const badgeText = await badgeButton.text()
expect(badgeText).toBe('Cache disabled')
})
it('persists cache-bypassing badge after navigation when cache is disabled', async () => {
const browser = await next.browser('/', {
extraHTTPHeaders: { 'cache-control': 'no-cache' },
})
// Wait for initial cache-bypassing badge
await retry(async () => {
const hasCacheBypassBadge = await browser.hasElementByCss(
'[data-cache-bypass-badge]'
)
expect(hasCacheBypassBadge).toBe(true)
})
// Navigate to another page
const link = await browser.waitForElementByCss('a[href="/navigation"]')
await link.click()
// Wait for navigation to complete
await retry(async () => {
const text = await browser.elementByCss('#navigation-page').text()
expect(text).toContain('Hello navigation page!')
})
// Verify cache-bypassing badge persists after navigation
await retry(async () => {
const hasCacheBypassBadge = await browser.hasElementByCss(
'[data-cache-bypass-badge]'
)
expect(hasCacheBypassBadge).toBe(true)
})
// Verify the badge still shows "Cache disabled" text
const badgeButton = await browser.elementByCss(
'[data-cache-bypass-badge] [data-issues-open]'
)
const badgeText = await badgeButton.text()
expect(badgeText).toBe('Cache disabled')
})
it('opens devtools menu when clicking cache-bypassing badge', async () => {
const browser = await next.browser('/', {
extraHTTPHeaders: { 'cache-control': 'no-cache' },
})
// Wait for the cache-bypassing badge to appear
await retry(async () => {
const hasCacheBypassBadge = await browser.hasElementByCss(
'[data-cache-bypass-badge]'
)
expect(hasCacheBypassBadge).toBe(true)
})
// Click the cache bypass badge
const badgeButton = await browser.elementByCss(
'[data-cache-bypass-badge] [data-issues-open]'
)
await badgeButton.click()
// Verify devtools menu opens
await retry(async () => {
const hasMenu = await browser.hasElementByCss('#nextjs-dev-tools-menu')
expect(hasMenu).toBe(true)
})
})
it('can dismiss cache-bypassing badge', async () => {
const browser = await next.browser('/', {
extraHTTPHeaders: { 'cache-control': 'no-cache' },
})
// Wait for the cache-bypassing badge to appear
await retry(async () => {
const hasCacheBypassBadge = await browser.hasElementByCss(
'[data-cache-bypass-badge]'
)
expect(hasCacheBypassBadge).toBe(true)
})
// Click the collapse button
const collapseButton = await browser.elementByCss(
'[data-cache-bypass-badge] [data-issues-collapse]'
)
await collapseButton.click()
// Verify badge is dismissed
await retry(async () => {
const hasCacheBypassBadge = await browser.hasElementByCss(
'[data-cache-bypass-badge]'
)
expect(hasCacheBypassBadge).toBe(false)
})
})
} }
}) })
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