Commit fe7c7342 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Add link to docs for prerender indicator and allow disabling (#8610)

* Add link to docs for prerender indicator and allow disabling

* Fix lint error from example

* Disable by default when electron is detected

* Add button to dismiss indicator for session

* Update disabling indicator in electron
parent ed5dc210
...@@ -2159,6 +2159,20 @@ There is no configuration or special handling required. ...@@ -2159,6 +2159,20 @@ There is no configuration or special handling required.
> **Note**: If you have a [custom `<Document>`](#custom-document) with `getInitialProps` be sure you check if `ctx.req` is defined before assuming the page is server-side rendered. > **Note**: If you have a [custom `<Document>`](#custom-document) with `getInitialProps` be sure you check if `ctx.req` is defined before assuming the page is server-side rendered.
> `ctx.req` will be `undefined` for pages that are prerendered. > `ctx.req` will be `undefined` for pages that are prerendered.
## Automatic Prerender Indicator
When a page qualifies for automatic prerendering we show an indicator to let you know. This is helpful since the automatic prerendering optimization can be very beneficial and knowing immediately in development if it qualifies can be useful. See above for information on the benefits of this optimization.
In some cases this indicator might not be as useful like when working on electron applications. For these cases you can disable the indicator in your `next.config.js` by setting
```js
module.exports = {
devIndicators: {
autoPrerender: false
}
}
```
## Production deployment ## Production deployment
To deploy, instead of running `next`, you want to build for production usage ahead of time. Therefore, building and starting are separate commands: To deploy, instead of running `next`, you want to build for production usage ahead of time. Therefore, building and starting are separate commands:
......
...@@ -551,9 +551,18 @@ export default async function getBaseWebpackConfig( ...@@ -551,9 +551,18 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_EXPORT_TRAILING_SLASH': JSON.stringify( 'process.env.__NEXT_EXPORT_TRAILING_SLASH': JSON.stringify(
config.exportTrailingSlash config.exportTrailingSlash
), ),
'process.env.__NEXT_MODERN_BUILD': config.experimental.modern && !dev, 'process.env.__NEXT_MODERN_BUILD': JSON.stringify(
'process.env.__NEXT_GRANULAR_CHUNKS': config.experimental.modern && !dev
config.experimental.granularChunks && !dev, ),
'process.env.__NEXT_GRANULAR_CHUNKS': JSON.stringify(
config.experimental.granularChunks && !dev
),
'process.env.__NEXT_BUILD_INDICATOR': JSON.stringify(
config.devIndicators.buildActivity
),
'process.env.__NEXT_PRERENDER_INDICATOR': JSON.stringify(
config.devIndicators.autoPrerender
),
...(isServer ...(isServer
? { ? {
// Fix bad-actors in the npm ecosystem (e.g. `node-formidable`) // Fix bad-actors in the npm ecosystem (e.g. `node-formidable`)
......
...@@ -11,7 +11,6 @@ export default function initializeBuildWatcher () { ...@@ -11,7 +11,6 @@ export default function initializeBuildWatcher () {
shadowHost.style.height = 0 shadowHost.style.height = 0
shadowHost.style.zIndex = 99998 shadowHost.style.zIndex = 99998
shadowHost.style.transition = 'all 100ms ease' shadowHost.style.transition = 'all 100ms ease'
shadowHost.title = 'Click to hide for page'
document.body.appendChild(shadowHost) document.body.appendChild(shadowHost)
...@@ -36,8 +35,15 @@ export default function initializeBuildWatcher () { ...@@ -36,8 +35,15 @@ export default function initializeBuildWatcher () {
const css = createCss(prefix) const css = createCss(prefix)
shadowRoot.appendChild(css) shadowRoot.appendChild(css)
const expandEl = container.querySelector('a')
const closeEl = container.querySelector(`#${prefix}close`)
// State // State
let isVisible = window.__NEXT_DATA__.nextExport 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 () { function updateContainer () {
if (isVisible) { if (isVisible) {
...@@ -46,21 +52,36 @@ export default function initializeBuildWatcher () { ...@@ -46,21 +52,36 @@ export default function initializeBuildWatcher () {
container.classList.remove(`${prefix}visible`) container.classList.remove(`${prefix}visible`)
} }
} }
const expandedClass = `${prefix}expanded`
let toggleTimeout
shadowHost.addEventListener('click', () => { const toggleExpand = (expand = true) => {
shadowHost.style.opacity = 0 clearTimeout(toggleTimeout)
shadowHost.style.pointerEvents = 'none'
}) toggleTimeout = setTimeout(() => {
shadowHost.addEventListener('mouseenter', () => { if (expand) {
container.classList.add(`${prefix}expanded`) expandEl.classList.add(expandedClass)
}) closeEl.style.display = 'flex'
shadowHost.addEventListener('mouseleave', () => { } else {
container.classList.remove(`${prefix}expanded`) 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', () => { Router.events.on('routeChangeComplete', () => {
isVisible = window.next.isPrerendered isVisible = window.next.isPrerendered
shadowHost.style.opacity = 1
updateContainer() updateContainer()
}) })
updateContainer() updateContainer()
...@@ -70,6 +91,10 @@ function createContainer (prefix) { ...@@ -70,6 +91,10 @@ function createContainer (prefix) {
const container = document.createElement('div') const container = document.createElement('div')
container.id = `${prefix}container` container.id = `${prefix}container`
container.innerHTML = ` container.innerHTML = `
<button id="${prefix}close" title="Hide indicator for session">
<span>×</span>
</button>
<a href="https://nextjs.org/docs#automatic-prerender-indicator" target="_blank">
<div id="${prefix}icon-wrapper"> <div id="${prefix}icon-wrapper">
<svg width="15" height="20" viewBox="0 0 60 80" fill="none" xmlns="http://www.w3.org/2000/svg"> <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="M36 3L30.74 41H8L36 3Z" fill="black"/>
...@@ -78,8 +103,8 @@ function createContainer (prefix) { ...@@ -78,8 +103,8 @@ function createContainer (prefix) {
</svg> </svg>
Prerendered Page Prerendered Page
</div> </div>
</a>
` `
return container return container
} }
...@@ -88,8 +113,32 @@ function createCss (prefix) { ...@@ -88,8 +113,32 @@ function createCss (prefix) {
css.textContent = ` css.textContent = `
#${prefix}container { #${prefix}container {
position: absolute; position: absolute;
display: none;
bottom: 10px; bottom: 10px;
right: 15px; 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; width: 15px;
height: 23px; height: 23px;
overflow: hidden; overflow: hidden;
...@@ -110,8 +159,7 @@ function createCss (prefix) { ...@@ -110,8 +159,7 @@ function createCss (prefix) {
align-items: center; 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); box-shadow: 0 11px 40px 0 rgba(0, 0, 0, 0.25), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
display: none; display: flex;
opacity: 0;
transition: opacity 0.1s ease, bottom 0.1s ease, width 0.3s ease; transition: opacity 0.1s ease, bottom 0.1s ease, width 0.3s ease;
animation: ${prefix}fade-in 0.1s ease-in-out; animation: ${prefix}fade-in 0.1s ease-in-out;
} }
...@@ -122,6 +170,7 @@ function createCss (prefix) { ...@@ -122,6 +170,7 @@ function createCss (prefix) {
display: flex; display: flex;
flex-shrink: 0; flex-shrink: 0;
align-items: center; align-items: center;
position: relative;
} }
#${prefix}icon-wrapper svg { #${prefix}icon-wrapper svg {
...@@ -129,8 +178,8 @@ function createCss (prefix) { ...@@ -129,8 +178,8 @@ function createCss (prefix) {
margin-right: 3px; margin-right: 3px;
} }
#${prefix}container.${prefix}expanded { #${prefix}container a.${prefix}expanded {
width: 140px; width: 135px;
} }
#${prefix}container.${prefix}visible { #${prefix}container.${prefix}visible {
......
...@@ -27,8 +27,14 @@ window.next = next ...@@ -27,8 +27,14 @@ window.next = next
initNext({ webpackHMR }) initNext({ webpackHMR })
.then(emitter => { .then(emitter => {
initOnDemandEntries({ assetPrefix: prefix }) initOnDemandEntries({ assetPrefix: prefix })
initializeBuildWatcher() if (process.env.__NEXT_BUILD_INDICATOR) initializeBuildWatcher()
if (
process.env.__NEXT_PRERENDER_INDICATOR &&
// disable by default in electron
!(typeof process !== 'undefined' && 'electron' in process.versions)
) {
initializePrerenderIndicator() initializePrerenderIndicator()
}
let lastScroll let lastScroll
......
...@@ -21,6 +21,10 @@ const defaultConfig: { [key: string]: any } = { ...@@ -21,6 +21,10 @@ const defaultConfig: { [key: string]: any } = {
target: 'server', target: 'server',
poweredByHeader: true, poweredByHeader: true,
compress: true, compress: true,
devIndicators: {
buildActivity: true,
autoPrerender: true,
},
onDemandEntries: { onDemandEntries: {
maxInactiveAge: 60 * 1000, maxInactiveAge: 60 * 1000,
pagesBufferLength: 2, pagesBufferLength: 2,
......
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import fs from 'fs' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { findPort, launchApp, killApp, waitFor } from 'next-test-utils' import { findPort, launchApp, killApp, waitFor } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let appPort let appPort
let app let app
...@@ -24,7 +25,9 @@ const installCheckVisible = browser => { ...@@ -24,7 +25,9 @@ const installCheckVisible = browser => {
} }
describe('Build Activity Indicator', () => { describe('Build Activity Indicator', () => {
describe('Enabled', () => {
beforeAll(async () => { beforeAll(async () => {
await fs.remove(nextConfig)
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
}) })
...@@ -51,15 +54,39 @@ describe('Build Activity Indicator', () => { ...@@ -51,15 +54,39 @@ describe('Build Activity Indicator', () => {
const browser = await webdriver(appPort, '/b') const browser = await webdriver(appPort, '/b')
await installCheckVisible(browser) await installCheckVisible(browser)
const pagePath = join(appDir, 'pages/b.js') const pagePath = join(appDir, 'pages/b.js')
const origContent = fs.readFileSync(pagePath, 'utf8') const origContent = await fs.readFile(pagePath, 'utf8')
const newContent = origContent.replace('b', 'c') const newContent = origContent.replace('b', 'c')
fs.writeFileSync(pagePath, newContent, 'utf8') await fs.writeFile(pagePath, newContent, 'utf8')
await waitFor(500) await waitFor(500)
const wasVisible = await browser.eval('window.showedBuilder') const wasVisible = await browser.eval('window.showedBuilder')
expect(wasVisible).toBe(true) expect(wasVisible).toBe(true)
fs.writeFileSync(pagePath, origContent, 'utf8') await fs.writeFile(pagePath, origContent, 'utf8')
await browser.close() await browser.close()
}) })
})
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)
})
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-watcher/)
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