Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/with-stomp/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module.exports = {
env: {
STOMP_SERVER: process.env.STOMP_SERVER
}
}
}
4 changes: 4 additions & 0 deletions packages/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const defaultConfig: { [key: string]: any } = {
target: 'server',
poweredByHeader: true,
compress: true,
devIndicators: {
buildActivity: true,
autoPrerender: true,
},
onDemandEntries: {
maxInactiveAge: 60 * 1000,
pagesBufferLength: 2,
Expand Down
14 changes: 14 additions & 0 deletions packages/next/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,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.
> `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

To deploy, instead of running `next`, you want to build for production usage ahead of time. Therefore, building and starting are separate commands:
Expand Down
15 changes: 12 additions & 3 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,18 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_EXPORT_TRAILING_SLASH': JSON.stringify(
config.exportTrailingSlash
),
'process.env.__NEXT_MODERN_BUILD': config.experimental.modern && !dev,
'process.env.__NEXT_GRANULAR_CHUNKS':
config.experimental.granularChunks && !dev,
'process.env.__NEXT_MODERN_BUILD': JSON.stringify(
config.experimental.modern && !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
? {
// Fix bad-actors in the npm ecosystem (e.g. `node-formidable`)
Expand Down
32 changes: 17 additions & 15 deletions packages/next/client/dev/prerender-indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default function initializeBuildWatcher () {
shadowHost.style.height = 0
shadowHost.style.zIndex = 99998
shadowHost.style.transition = 'all 100ms ease'
shadowHost.title = 'Click to hide for page'

document.body.appendChild(shadowHost)

Expand Down Expand Up @@ -47,10 +46,6 @@ export default function initializeBuildWatcher () {
}
}

shadowHost.addEventListener('click', () => {
shadowHost.style.opacity = 0
shadowHost.style.pointerEvents = 'none'
})
shadowHost.addEventListener('mouseenter', () => {
container.classList.add(`${prefix}expanded`)
})
Expand All @@ -70,16 +65,17 @@ function createContainer (prefix) {
const container = document.createElement('div')
container.id = `${prefix}container`
container.innerHTML = `
<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 href="https://nextjs.org/docs#automatic-prerender-indicator" target="_blank">
<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
}

Expand Down Expand Up @@ -116,12 +112,18 @@ function createCss (prefix) {
animation: ${prefix}fade-in 0.1s ease-in-out;
}

#${prefix}container a {
color: inherit;
text-decoration: none;
}

#${prefix}icon-wrapper {
width: 140px;
height: 20px;
display: flex;
flex-shrink: 0;
align-items: center;
position: relative;
}

#${prefix}icon-wrapper svg {
Expand All @@ -130,7 +132,7 @@ function createCss (prefix) {
}

#${prefix}container.${prefix}expanded {
width: 140px;
width: 135px;
}

#${prefix}container.${prefix}visible {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/client/next-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ window.next = next
initNext({ webpackHMR })
.then(emitter => {
initOnDemandEntries({ assetPrefix: prefix })
initializeBuildWatcher()
initializePrerenderIndicator()
if (process.env.__NEXT_BUILD_INDICATOR) initializeBuildWatcher()
if (process.env.__NEXT_PRERENDER_INDICATOR) initializePrerenderIndicator()

let lastScroll

Expand Down
93 changes: 60 additions & 33 deletions test/integration/build-indicator/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs'
import fs from 'fs-extra'
import { join } from 'path'
import webdriver from 'next-webdriver'
import { findPort, launchApp, killApp, waitFor } from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app

Expand All @@ -24,42 +25,68 @@ const installCheckVisible = browser => {
}

describe('Build Activity Indicator', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
describe('Enabled', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))

it('Adds the build indicator container', async () => {
const browser = await webdriver(appPort, '/')
const html = await browser.eval('document.body.innerHTML')
expect(html).toMatch(/__next-build-watcher/)
await browser.close()
})
it('Adds the build indicator container', async () => {
const browser = await webdriver(appPort, '/')
const html = await browser.eval('document.body.innerHTML')
expect(html).toMatch(/__next-build-watcher/)
await browser.close()
})

it('Shows the build indicator when a page is built during navigation', async () => {
const browser = await webdriver(appPort, '/')
await installCheckVisible(browser)
await browser.elementByCss('#to-a').click()
await waitFor(500)
const wasVisible = await browser.eval('window.showedBuilder')
expect(wasVisible).toBe(true)
await browser.close()
})
it('Shows the build indicator when a page is built during navigation', async () => {
const browser = await webdriver(appPort, '/')
await installCheckVisible(browser)
await browser.elementByCss('#to-a').click()
await waitFor(500)
const wasVisible = await browser.eval('window.showedBuilder')
expect(wasVisible).toBe(true)
await browser.close()
})

it('Shows build indicator when page is built from modifying', async () => {
const browser = await webdriver(appPort, '/b')
await installCheckVisible(browser)
const pagePath = join(appDir, 'pages/b.js')
const origContent = await fs.readFile(pagePath, 'utf8')
const newContent = origContent.replace('b', 'c')

it('Shows build indicator when page is built from modifying', async () => {
const browser = await webdriver(appPort, '/b')
await installCheckVisible(browser)
const pagePath = join(appDir, 'pages/b.js')
const origContent = fs.readFileSync(pagePath, 'utf8')
const newContent = origContent.replace('b', 'c')
await fs.writeFile(pagePath, newContent, 'utf8')
await waitFor(500)
const wasVisible = await browser.eval('window.showedBuilder')

fs.writeFileSync(pagePath, newContent, 'utf8')
await waitFor(500)
const wasVisible = await browser.eval('window.showedBuilder')
expect(wasVisible).toBe(true)
await fs.writeFile(pagePath, origContent, 'utf8')
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)
})

expect(wasVisible).toBe(true)
fs.writeFileSync(pagePath, origContent, 'utf8')
await browser.close()
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()
})
})
})