Unverified Commit 09a2fe5c authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Amp page level reload (#6971)

* Only refresh the page when the active
page is updated in AMP mode

* Update handling of page reload to make sure it
still refreshes after a change to another page

* Update checking to be more accurate

* Fix amp-dev not being loaded without
experimental.amp and remove next.config from amp example

* Remove old with-amp example and
rename experimental-amp to with-amp

* update example name
Co-Authored-By: 's avatarijjk <jj@jjsweb.site>

* Update comment wording
Co-Authored-By: 's avatarijjk <jj@jjsweb.site>

* Use document for reload to keep scroll position
Co-Authored-By: 's avatarijjk <jj@jjsweb.site>

* fallback to reloading on error
Co-Authored-By: 's avatarijjk <jj@jjsweb.site>

* Update with-amp example readme
parent 11016221
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/experimental-amp)
# Google AMP
## Warning
This is an experimental feature that is subject to change (breaking) between releases.
## How to use
### Using `create-next-app`
Execute [`create-next-app`](https://github.com/segmentio/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:
```bash
npx create-next-app --example experimental-amp experimental-amp-app
# or
yarn create next-app --example experimental-amp experimental-amp-app
```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/experimental-amp
cd experimental-amp
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
The Next.js team is working on first-class AMP support. You can periodically fetch an updated version of this template to view our progress.
export default ({ author }) => (
<>
<div className='byline'>By {author}</div>
<style jsx>{`
.byline {
color: green;
font-weight: bolder;
}
`}</style>
</>
)
module.exports = {
experimental: {
amp: true
}
}
{
"name": "experimental-amp",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0"
},
"license": "ISC"
}
import Head from 'next/head'
import { useAmp, withAmp } from 'next/amp'
import Byline from '../components/Byline'
export default withAmp(() => {
const isAmp = useAmp()
return (
<div>
<Head>
<title>The Dog</title>
</Head>
<h1>The Dog</h1>
<Byline author='Meow Meow Fuzzyface' />
<p>
<a href={isAmp ? '/dog' : '/dog?amp=1'}>
{isAmp ? 'View Non-AMP' : 'View AMP'} Version
</a>
</p>
<p className='caption'>Woooooooooooof</p>
<p>
Wafer donut candy soufflé{' '}
<a href={isAmp ? '/?amp=1' : '/'}>lemon drops</a> icing. Marzipan gummi
bears pie danish lollipop pudding powder gummi bears sweet. Pie sweet
roll sweet roll topping chocolate bar dragée pudding chocolate cake.
Croissant sweet chocolate bar cheesecake candy canes. Tootsie roll icing
macaroon bonbon cupcake apple pie candy canes biscuit candy canes.
Jujubes jelly liquorice toffee gingerbread. Candy tootsie roll macaroon
chocolate bar icing sugar plum pie. Icing gummies chocolate bar
chocolate marzipan bonbon cookie chocolate tart. Caramels danish halvah
croissant. Cheesecake cookie tootsie roll ice cream. Powder dessert
carrot cake muffin tiramisu lemon drops liquorice topping brownie.
Soufflé chocolate cake croissant cupcake jelly.
</p>
<p>
Muffin gummies dessert cheesecake candy canes. Candy canes danish cotton
candy tart dessert powder bear claw marshmallow. Muffin chocolate
marshmallow danish. Chocolate bar biscuit cake tiramisu. Topping sweet
brownie jujubes powder marzipan. Croissant wafer bonbon chupa chups cake
cake marzipan caramels jujubes. Cupcake cheesecake sweet roll
marshmallow lollipop danish jujubes jelly icing. Apple pie chupa chups
lollipop jelly-o cheesecake jelly beans cake dessert. Tootsie roll
tootsie roll bonbon pastry croissant gummi bears cake cake. Fruitcake
sugar plum halvah gingerbread cookie pastry chupa chups wafer lemon
drops. Marshmallow liquorice oat cake lollipop. Lemon drops oat cake
halvah liquorice danish powder cupcake soufflé. Cake tart topping
jelly-o tart sugar plum. Chocolate bar cookie wafer tootsie roll candy
cotton candy toffee pie donut.
</p>
<p>
Ice cream lollipop marshmallow tiramisu jujubes croissant. Bear claw
lemon drops marzipan candy bonbon cupcake powder. Candy canes cheesecake
bear claw pastry cake donut jujubes. Icing tart jelly-o soufflé bonbon
apple pie. Cheesecake pie chupa chups toffee powder. Bonbon lemon drops
carrot cake pudding candy halvah cheesecake lollipop cupcake. Pudding
marshmallow fruitcake. Gummi bears bonbon chupa chups lemon drops. Wafer
dessert gummies gummi bears biscuit donut tiramisu gummi bears brownie.
Tootsie roll liquorice bonbon cookie. Sesame snaps chocolate bar cake
croissant chupa chups cheesecake gingerbread tiramisu jelly. Cheesecake
ice cream muffin lollipop gummies. Sesame snaps jelly beans sweet bear
claw tart.
</p>
<p>
Sweet topping chupa chups chocolate cake jelly-o liquorice danish.
Pastry jelly beans apple pie dessert pastry lemon drops marzipan
gummies. Jelly beans macaroon bear claw cotton candy. Toffee sweet
lollipop toffee oat cake. Jelly-o oat cake fruitcake chocolate bar
sweet. Lemon drops gummies chocolate cake lollipop bear claw croissant
danish icing. Chocolate bar donut brownie chocolate cake lemon drops
chocolate bar. Cake fruitcake pudding chocolate apple pie. Brownie
tiramisu chocolate macaroon lemon drops wafer soufflé jujubes icing.
Cheesecake tiramisu cake macaroon tart lollipop donut. Gummi bears
dragée pudding bear claw. Muffin cake cupcake candy canes. Soufflé candy
canes biscuit. Macaroon gummies danish.
</p>
<p>
Cupcake cupcake tart. Cotton candy danish candy canes oat cake ice cream
candy canes powder wafer. Chocolate sesame snaps oat cake dragée
cheesecake. Sesame snaps marshmallow topping liquorice cookie
marshmallow. Liquorice pudding chocolate bar. Cake powder brownie
fruitcake. Carrot cake dessert marzipan sugar plum cupcake cheesecake
pastry. Apple pie macaroon ice cream fruitcake apple pie cookie. Tootsie
roll ice cream oat cake cheesecake donut cheesecake bear claw. Sesame
snaps marzipan jelly beans chocolate tootsie roll. Chocolate bar donut
dragée ice cream biscuit. Pie candy canes muffin candy canes ice cream
tiramisu.
</p>
</div>
)
}, { hybrid: true })
This diff is collapsed.
...@@ -41,4 +41,4 @@ now ...@@ -41,4 +41,4 @@ now
## The idea behind the example ## The idea behind the example
Next.js allows the construction of custom Documents. This feature enable the usage of custom attributes and elements. In this case, AMP tags and attributes. This example shows how to create AMP pages using Next.js. It shows a normal page (non-AMP), an AMP only page, and a hybrid AMP page.
export default ({ author }) => <div className='byline'>By {author}</div> export default ({ author }) => (
<>
<div className='byline'>By {author}</div>
<style jsx>{`
.byline {
color: green;
font-weight: bolder;
}
`}</style>
</>
)
import Document, { Head } from 'next/document'
export default class MyDocument extends Document {
render () {
const { html } = this.props
return (
<html amp=''>
<Head>
<link rel='canonical' href='/' />
<meta name='viewport' content='width=device-width,minimum-scale=1' />
<link
rel='stylesheet'
href='https://fonts.googleapis.com/css?family=Roboto'
/>
<style amp-boilerplate=''>{`body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}`}</style>
<noscript>
<style amp-boilerplate=''>{`body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}`}</style>
</noscript>
<style amp-custom=''>{`body {font-family: Roboto, sans-serif; padding: 30px; color: #444;} h1 {margin-bottom: 5px;} .byline { color: #aaa; margin-bottom: 25px; } p {font-size: 18px; line-height: 30px; margin-top: 30px;} .caption {color: #ccc; margin-top: 0; font-size: 14px; text-align: center;}`}</style>
<script async src='https://cdn.ampproject.org/v0.js' />
</Head>
<body>
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
</body>
</html>
)
}
}
import Head from 'next/head' import Head from 'next/head'
import { useAmp, withAmp } from 'next/amp'
import Byline from '../components/Byline' import Byline from '../components/Byline'
export default () => ( export default withAmp(() => {
const isAmp = useAmp()
return (
<div> <div>
<Head> <Head>
<title>The Dog</title> <title>The Dog</title>
</Head> </Head>
<h1>The Dog</h1> <h1>The Dog</h1>
<Byline author='Meow Meow Fuzzyface' /> <Byline author='Meow Meow Fuzzyface' />
<p>
<a href={isAmp ? '/dog' : '/dog?amp=1'}>
{isAmp ? 'View Non-AMP' : 'View AMP'} Version
</a>
</p>
<p className='caption'>Woooooooooooof</p> <p className='caption'>Woooooooooooof</p>
<p> <p>
Wafer donut candy soufflé lemon drops icing. Marzipan gummi bears pie Wafer donut candy soufflé{' '}
danish lollipop pudding powder gummi bears sweet. Pie sweet roll sweet <a href={isAmp ? '/?amp=1' : '/'}>lemon drops</a> icing. Marzipan gummi
roll topping chocolate bar dragée pudding chocolate cake. Croissant sweet bears pie danish lollipop pudding powder gummi bears sweet. Pie sweet
chocolate bar cheesecake candy canes. Tootsie roll icing macaroon bonbon roll sweet roll topping chocolate bar dragée pudding chocolate cake.
cupcake apple pie candy canes biscuit candy canes. Jujubes jelly liquorice Croissant sweet chocolate bar cheesecake candy canes. Tootsie roll icing
toffee gingerbread. Candy tootsie roll macaroon chocolate bar icing sugar macaroon bonbon cupcake apple pie candy canes biscuit candy canes.
plum pie. Icing gummies chocolate bar chocolate marzipan bonbon cookie Jujubes jelly liquorice toffee gingerbread. Candy tootsie roll macaroon
chocolate tart. Caramels danish halvah croissant. Cheesecake cookie chocolate bar icing sugar plum pie. Icing gummies chocolate bar
tootsie roll ice cream. Powder dessert carrot cake muffin tiramisu lemon chocolate marzipan bonbon cookie chocolate tart. Caramels danish halvah
drops liquorice topping brownie. Soufflé chocolate cake croissant cupcake croissant. Cheesecake cookie tootsie roll ice cream. Powder dessert
jelly. carrot cake muffin tiramisu lemon drops liquorice topping brownie.
Soufflé chocolate cake croissant cupcake jelly.
</p> </p>
<p> <p>
Muffin gummies dessert cheesecake candy canes. Candy canes danish cotton Muffin gummies dessert cheesecake candy canes. Candy canes danish cotton
candy tart dessert powder bear claw marshmallow. Muffin chocolate candy tart dessert powder bear claw marshmallow. Muffin chocolate
marshmallow danish. Chocolate bar biscuit cake tiramisu. Topping sweet marshmallow danish. Chocolate bar biscuit cake tiramisu. Topping sweet
brownie jujubes powder marzipan. Croissant wafer bonbon chupa chups cake brownie jujubes powder marzipan. Croissant wafer bonbon chupa chups cake
cake marzipan caramels jujubes. Cupcake cheesecake sweet roll marshmallow cake marzipan caramels jujubes. Cupcake cheesecake sweet roll
lollipop danish jujubes jelly icing. Apple pie chupa chups lollipop marshmallow lollipop danish jujubes jelly icing. Apple pie chupa chups
jelly-o cheesecake jelly beans cake dessert. Tootsie roll tootsie roll lollipop jelly-o cheesecake jelly beans cake dessert. Tootsie roll
bonbon pastry croissant gummi bears cake cake. Fruitcake sugar plum halvah tootsie roll bonbon pastry croissant gummi bears cake cake. Fruitcake
gingerbread cookie pastry chupa chups wafer lemon drops. Marshmallow sugar plum halvah gingerbread cookie pastry chupa chups wafer lemon
liquorice oat cake lollipop. Lemon drops oat cake halvah liquorice danish drops. Marshmallow liquorice oat cake lollipop. Lemon drops oat cake
powder cupcake soufflé. Cake tart topping jelly-o tart sugar plum. halvah liquorice danish powder cupcake soufflé. Cake tart topping
Chocolate bar cookie wafer tootsie roll candy cotton candy toffee pie jelly-o tart sugar plum. Chocolate bar cookie wafer tootsie roll candy
donut. cotton candy toffee pie donut.
</p> </p>
<p> <p>
Ice cream lollipop marshmallow tiramisu jujubes croissant. Bear claw lemon Ice cream lollipop marshmallow tiramisu jujubes croissant. Bear claw
drops marzipan candy bonbon cupcake powder. Candy canes cheesecake bear lemon drops marzipan candy bonbon cupcake powder. Candy canes cheesecake
claw pastry cake donut jujubes. Icing tart jelly-o soufflé bonbon apple bear claw pastry cake donut jujubes. Icing tart jelly-o soufflé bonbon
pie. Cheesecake pie chupa chups toffee powder. Bonbon lemon drops carrot apple pie. Cheesecake pie chupa chups toffee powder. Bonbon lemon drops
cake pudding candy halvah cheesecake lollipop cupcake. Pudding marshmallow carrot cake pudding candy halvah cheesecake lollipop cupcake. Pudding
fruitcake. Gummi bears bonbon chupa chups lemon drops. Wafer dessert marshmallow fruitcake. Gummi bears bonbon chupa chups lemon drops. Wafer
gummies gummi bears biscuit donut tiramisu gummi bears brownie. Tootsie dessert gummies gummi bears biscuit donut tiramisu gummi bears brownie.
roll liquorice bonbon cookie. Sesame snaps chocolate bar cake croissant Tootsie roll liquorice bonbon cookie. Sesame snaps chocolate bar cake
chupa chups cheesecake gingerbread tiramisu jelly. Cheesecake ice cream croissant chupa chups cheesecake gingerbread tiramisu jelly. Cheesecake
muffin lollipop gummies. Sesame snaps jelly beans sweet bear claw tart. ice cream muffin lollipop gummies. Sesame snaps jelly beans sweet bear
claw tart.
</p> </p>
<p> <p>
Sweet topping chupa chups chocolate cake jelly-o liquorice danish. Pastry Sweet topping chupa chups chocolate cake jelly-o liquorice danish.
jelly beans apple pie dessert pastry lemon drops marzipan gummies. Jelly Pastry jelly beans apple pie dessert pastry lemon drops marzipan
beans macaroon bear claw cotton candy. Toffee sweet lollipop toffee oat gummies. Jelly beans macaroon bear claw cotton candy. Toffee sweet
cake. Jelly-o oat cake fruitcake chocolate bar sweet. Lemon drops gummies lollipop toffee oat cake. Jelly-o oat cake fruitcake chocolate bar
chocolate cake lollipop bear claw croissant danish icing. Chocolate bar sweet. Lemon drops gummies chocolate cake lollipop bear claw croissant
donut brownie chocolate cake lemon drops chocolate bar. Cake fruitcake danish icing. Chocolate bar donut brownie chocolate cake lemon drops
pudding chocolate apple pie. Brownie tiramisu chocolate macaroon lemon chocolate bar. Cake fruitcake pudding chocolate apple pie. Brownie
drops wafer soufflé jujubes icing. Cheesecake tiramisu cake macaroon tart tiramisu chocolate macaroon lemon drops wafer soufflé jujubes icing.
lollipop donut. Gummi bears dragée pudding bear claw. Muffin cake cupcake Cheesecake tiramisu cake macaroon tart lollipop donut. Gummi bears
candy canes. Soufflé candy canes biscuit. Macaroon gummies danish. dragée pudding bear claw. Muffin cake cupcake candy canes. Soufflé candy
canes biscuit. Macaroon gummies danish.
</p> </p>
<p> <p>
Cupcake cupcake tart. Cotton candy danish candy canes oat cake ice cream Cupcake cupcake tart. Cotton candy danish candy canes oat cake ice cream
candy canes powder wafer. Chocolate sesame snaps oat cake dragée candy canes powder wafer. Chocolate sesame snaps oat cake dragée
cheesecake. Sesame snaps marshmallow topping liquorice cookie marshmallow. cheesecake. Sesame snaps marshmallow topping liquorice cookie
Liquorice pudding chocolate bar. Cake powder brownie fruitcake. Carrot marshmallow. Liquorice pudding chocolate bar. Cake powder brownie
cake dessert marzipan sugar plum cupcake cheesecake pastry. Apple pie fruitcake. Carrot cake dessert marzipan sugar plum cupcake cheesecake
macaroon ice cream fruitcake apple pie cookie. Tootsie roll ice cream oat pastry. Apple pie macaroon ice cream fruitcake apple pie cookie. Tootsie
cake cheesecake donut cheesecake bear claw. Sesame snaps marzipan jelly roll ice cream oat cake cheesecake donut cheesecake bear claw. Sesame
beans chocolate tootsie roll. Chocolate bar donut dragée ice cream snaps marzipan jelly beans chocolate tootsie roll. Chocolate bar donut
biscuit. Pie candy canes muffin candy canes ice cream tiramisu. dragée ice cream biscuit. Pie candy canes muffin candy canes ice cream
tiramisu.
</p> </p>
</div> </div>
) )
}, { hybrid: true })
This diff is collapsed.
/* globals __webpack_hash__ */ /* globals __webpack_hash__ */
import fetch from 'unfetch'
import EventSourcePolyfill from './event-source-polyfill' import EventSourcePolyfill from './event-source-polyfill'
import { getEventSourceWrapper } from './dev-error-overlay/eventsource' import { getEventSourceWrapper } from './dev-error-overlay/eventsource'
import { setupPing } from './on-demand-entries-utils' import { setupPing } from './on-demand-entries-utils'
...@@ -10,6 +11,47 @@ if (!window.EventSource) { ...@@ -10,6 +11,47 @@ if (!window.EventSource) {
const data = JSON.parse(document.getElementById('__NEXT_DATA__').textContent) const data = JSON.parse(document.getElementById('__NEXT_DATA__').textContent)
let { assetPrefix, page } = data let { assetPrefix, page } = data
assetPrefix = assetPrefix || '' assetPrefix = assetPrefix || ''
let mostRecentHash = null
/* eslint-disable-next-line */
let curHash = __webpack_hash__
const hotUpdatePath = assetPrefix + (assetPrefix.endsWith('/') ? '' : '/') + '_next/static/webpack/'
// Is there a newer version of this code available?
function isUpdateAvailable () {
// __webpack_hash__ is the hash of the current compilation.
// It's a global variable injected by Webpack.
/* eslint-disable-next-line */
return mostRecentHash !== __webpack_hash__
}
// Webpack disallows updates in other states.
function canApplyUpdates () {
return module.hot.status() === 'idle'
}
// This function reads code updates on the fly and hard
// reloads the page when it has changed.
async function tryApplyUpdates () {
if (!isUpdateAvailable() || !canApplyUpdates()) {
return
}
try {
const res = await fetch(`${hotUpdatePath}${curHash}.hot-update.json`)
const data = await res.json()
const curPage = page === '/' ? 'index' : page
const pageUpdated = Object.keys(data.c)
.some(mod => mod.indexOf(`pages/${curPage}`) !== -1)
if (pageUpdated) {
document.location.reload(true)
} else {
curHash = mostRecentHash
}
} catch (err) {
console.error('Error occurred checking for update', err)
document.location.reload(true)
}
}
getEventSourceWrapper({ getEventSourceWrapper({
path: `${assetPrefix}/_next/webpack-hmr` path: `${assetPrefix}/_next/webpack-hmr`
...@@ -25,11 +67,8 @@ getEventSourceWrapper({ ...@@ -25,11 +67,8 @@ getEventSourceWrapper({
if (!message.hash) { if (!message.hash) {
return return
} }
mostRecentHash = message.hash
/* eslint-disable-next-line camelcase */ tryApplyUpdates()
if (message.hash !== __webpack_hash__) {
document.location.reload(true)
}
} else if (message.action === 'reloadPage') { } else if (message.action === 'reloadPage') {
document.location.reload(true) document.location.reload(true)
} }
......
...@@ -173,9 +173,7 @@ export default class HotReloader { ...@@ -173,9 +173,7 @@ export default class HotReloader {
const entrypoints = createEntrypoints(pages, 'server', this.buildId, false, this.config) const entrypoints = createEntrypoints(pages, 'server', this.buildId, false, this.config)
let additionalClientEntrypoints = {} let additionalClientEntrypoints = {}
if (this.config.experimental.amp) {
additionalClientEntrypoints[CLIENT_STATIC_FILES_RUNTIME_AMP] = `.${sep}` + relativePath(this.dir, join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'amp-dev')) additionalClientEntrypoints[CLIENT_STATIC_FILES_RUNTIME_AMP] = `.${sep}` + relativePath(this.dir, join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'amp-dev'))
}
return [ return [
getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config, buildId: this.buildId, entrypoints: { ...entrypoints.client, ...additionalClientEntrypoints } }), getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config, buildId: this.buildId, entrypoints: { ...entrypoints.client, ...additionalClientEntrypoints } }),
......
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