Unverified Commit 197d46dd authored by Carmelo Scandaliato's avatar Carmelo Scandaliato Committed by GitHub

Remove the static optimization indicator (#18629)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent fb58d95d
......@@ -34,8 +34,6 @@ And if you add `getServerSideProps` to the page, it will then be JavaScript, lik
.next/server/static/${BUILD_ID}/about.js
```
In development you'll know if `pages/about.js` is optimized or not thanks to the included [static optimization indicator](/docs/api-reference/next.config.js/static-optimization-indicator.md).
## Caveats
- If you have a [custom `App`](/docs/advanced-features/custom-app.md) with `getInitialProps` then this optimization will be turned off in pages without [Static Generation](/docs/basic-features/data-fetching.md#getstaticprops-static-generation).
......
---
description: Optimized pages include an indicator to let you know if it's being statically optimized. You can opt-out of it here.
---
# Static Optimization Indicator
When a page qualifies for [Automatic Static Optimization](/docs/advanced-features/automatic-static-optimization.md) we show an indicator to let you know.
This is helpful since automatic static optimization can be very beneficial and knowing immediately in development if the page qualifies can be useful.
In some cases this indicator might not be useful, like when working on electron applications. To remove it open `next.config.js` and disable the `autoPrerender` config in `devIndicators`:
```js
module.exports = {
devIndicators: {
autoPrerender: false,
},
}
```
## Related
<div class="card">
<a href="/docs/api-reference/next.config.js/introduction.md">
<b>Introduction to next.config.js:</b>
<small>Learn more about the configuration file used by Next.js.</small>
</a>
</div>
<div class="card">
<a href="/docs/advanced-features/automatic-static-optimization.md">
<b>Automatic Static Optimization:</b>
<small>Next.js automatically optimizes your app to be static HTML whenever possible. Learn how it works here.</small>
</a>
</div>
......@@ -292,10 +292,6 @@
"title": "Compression",
"path": "/docs/api-reference/next.config.js/compression.md"
},
{
"title": "Static Optimization Indicator",
"path": "/docs/api-reference/next.config.js/static-optimization-indicator.md"
},
{
"title": "Runtime Configuration",
"path": "/docs/api-reference/next.config.js/runtime-configuration.md"
......
module.exports = {
devIndicators: {
autoPrerender: false,
},
}
......@@ -969,9 +969,6 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_BUILD_INDICATOR': JSON.stringify(
config.devIndicators.buildActivity
),
'process.env.__NEXT_PRERENDER_INDICATOR': JSON.stringify(
config.devIndicators.autoPrerender
),
'process.env.__NEXT_PLUGINS': JSON.stringify(
config.experimental.plugins
),
......@@ -1196,7 +1193,6 @@ export default async function getBaseWebpackConfig(
trailingSlash: config.trailingSlash,
modern: config.experimental.modern,
buildActivity: config.devIndicators.buildActivity,
autoPrerender: config.devIndicators.autoPrerender,
plugins: config.experimental.plugins,
reactStrictMode: config.reactStrictMode,
reactMode: config.experimental.reactMode,
......
import Router from '../router'
export default function initializeBuildWatcher() {
const shadowHost = document.createElement('div')
shadowHost.id = '__next-prerender-indicator'
// Make sure container is fixed and on a high zIndex so it shows
shadowHost.style.position = 'fixed'
shadowHost.style.bottom = '20px'
shadowHost.style.right = '10px'
shadowHost.style.width = 0
shadowHost.style.height = 0
shadowHost.style.zIndex = 99998
shadowHost.style.transition = 'all 100ms ease'
document.body.appendChild(shadowHost)
let shadowRoot
let prefix = ''
if (shadowHost.attachShadow) {
shadowRoot = shadowHost.attachShadow({ mode: 'open' })
} else {
// If attachShadow is undefined then the browser does not support
// the Shadow DOM, we need to prefix all the names so there
// will be no conflicts
shadowRoot = shadowHost
prefix = '__next-prerender-indicator-'
}
// Container
const container = createContainer(prefix)
shadowRoot.appendChild(container)
// CSS
const css = createCss(prefix)
shadowRoot.appendChild(css)
const expandEl = container.querySelector('a')
const closeEl = container.querySelector(`#${prefix}close`)
// State
const dismissKey = '__NEXT_DISMISS_PRERENDER_INDICATOR'
const dismissUntil = parseInt(window.localStorage.getItem(dismissKey), 10)
const dismissed = dismissUntil > new Date().getTime()
let isVisible = !dismissed && window.__NEXT_DATA__.nextExport
function updateContainer() {
if (isVisible) {
container.classList.add(`${prefix}visible`)
} else {
container.classList.remove(`${prefix}visible`)
}
}
const expandedClass = `${prefix}expanded`
let toggleTimeout
const toggleExpand = (expand = true) => {
clearTimeout(toggleTimeout)
toggleTimeout = setTimeout(() => {
if (expand) {
expandEl.classList.add(expandedClass)
closeEl.style.display = 'flex'
} else {
expandEl.classList.remove(expandedClass)
closeEl.style.display = 'none'
}
}, 50)
}
closeEl.addEventListener('click', () => {
const oneHourAway = new Date().getTime() + 1 * 60 * 60 * 1000
window.localStorage.setItem(dismissKey, oneHourAway + '')
isVisible = false
updateContainer()
})
closeEl.addEventListener('mouseenter', () => toggleExpand())
closeEl.addEventListener('mouseleave', () => toggleExpand(false))
expandEl.addEventListener('mouseenter', () => toggleExpand())
expandEl.addEventListener('mouseleave', () => toggleExpand(false))
Router.events.on('routeChangeComplete', () => {
isVisible = window.next.isPrerendered
updateContainer()
})
updateContainer()
}
function createContainer(prefix) {
const container = document.createElement('div')
container.id = `${prefix}container`
container.innerHTML = `
<button id="${prefix}close" title="Hide indicator for session">
<span>×</span>
</button>
<a href="https://nextjs.org/docs/api-reference/next.config.js/static-optimization-indicator" target="_blank" rel="noreferrer">
<div id="${prefix}icon-wrapper">
<svg width="15" height="20" viewBox="0 0 60 80" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M36 3L30.74 41H8L36 3Z" fill="black"/>
<path d="M25 77L30.26 39H53L25 77Z" fill="black"/>
<path d="M13.5 33.5L53 39L47.5 46.5L7 41.25L13.5 33.5Z" fill="black"/>
</svg>
Prerendered Page
</div>
</a>
`
return container
}
function createCss(prefix) {
const css = document.createElement('style')
css.textContent = `
#${prefix}container {
position: absolute;
display: none;
bottom: 10px;
right: 15px;
}
#${prefix}close {
top: -10px;
right: -10px;
border: none;
width: 18px;
height: 18px;
color: #333333;
font-size: 16px;
cursor: pointer;
display: none;
position: absolute;
background: #ffffff;
border-radius: 100%;
align-items: center;
flex-direction: column;
justify-content: center;
}
#${prefix}container a {
color: inherit;
text-decoration: none;
width: 15px;
height: 23px;
overflow: hidden;
border-radius: 3px;
background: #fff;
color: #000;
font: initial;
cursor: pointer;
letter-spacing: initial;
text-shadow: initial;
text-transform: initial;
visibility: initial;
font-size: 14px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
padding: 4px 2px;
align-items: center;
box-shadow: 0 11px 40px 0 rgba(0, 0, 0, 0.25), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
display: flex;
transition: opacity 0.1s ease, bottom 0.1s ease, width 0.3s ease;
animation: ${prefix}fade-in 0.1s ease-in-out;
}
#${prefix}icon-wrapper {
width: 140px;
height: 20px;
display: flex;
flex-shrink: 0;
align-items: center;
position: relative;
}
#${prefix}icon-wrapper svg {
flex-shrink: 0;
margin-right: 3px;
}
#${prefix}container a.${prefix}expanded {
width: 135px;
}
#${prefix}container.${prefix}visible {
display: flex;
bottom: 10px;
opacity: 1;
}
@keyframes ${prefix}fade-in {
from {
bottom: 0px;
opacity: 0;
}
to {
bottom: 10px;
opacity: 1;
}
}
`
return css
}
......@@ -4,7 +4,6 @@ import EventSourcePolyfill from './dev/event-source-polyfill'
import initOnDemandEntries from './dev/on-demand-entries-client'
import initWebpackHMR from './dev/webpack-hot-middleware-client'
import initializeBuildWatcher from './dev/dev-build-watcher'
import initializePrerenderIndicator from './dev/prerender-indicator'
import { displayContent } from './dev/fouc'
import { getEventSourceWrapper } from './dev/error-overlay/eventsource'
import * as querystring from '../next-server/lib/router/utils/querystring'
......@@ -80,13 +79,6 @@ initNext({ webpackHMR })
buildIndicatorHandler = handler
})
}
if (
process.env.__NEXT_PRERENDER_INDICATOR &&
// disable by default in electron
!(typeof process !== 'undefined' && 'electron' in process.versions)
) {
initializePrerenderIndicator()
}
// delay rendering until after styles have been applied in development
displayContent(() => {
......
......@@ -33,7 +33,6 @@ const defaultConfig: { [key: string]: any } = {
},
devIndicators: {
buildActivity: true,
autoPrerender: true,
},
onDemandEntries: {
maxInactiveAge: 60 * 1000,
......
......@@ -23,9 +23,6 @@ const runTests = () => {
amp: {
canonicalBase: undefined,
},
devIndicators: {
autoPrerender: undefined,
},
}
`
)
......@@ -47,9 +44,6 @@ const runTests = () => {
amp: {
canonicalBase: null,
},
devIndicators: {
autoPrerender: null,
},
}
`
)
......
......@@ -104,22 +104,14 @@ describe('config', () => {
it('Should ignore configs set to `undefined`', () => {
const config = loadConfig(PHASE_DEVELOPMENT_SERVER, null, {
target: undefined,
devIndicators: {
autoPrerender: undefined,
},
})
expect(config.target).toBe('server')
expect(config.devIndicators.autoPrerender).toBe(true)
})
it('Should ignore configs set to `null`', () => {
const config = loadConfig(PHASE_DEVELOPMENT_SERVER, null, {
target: null,
devIndicators: {
autoPrerender: null,
},
})
expect(config.target).toBe('server')
expect(config.devIndicators.autoPrerender).toBe(true)
})
})
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