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(() => {
<div> const isAmp = useAmp()
<Head>
<title>The Dog</title> return (
</Head> <div>
<h1>The Dog</h1> <Head>
<Byline author='Meow Meow Fuzzyface' /> <title>The Dog</title>
<p className='caption'>Woooooooooooof</p> </Head>
<p> <h1>The Dog</h1>
Wafer donut candy soufflé lemon drops icing. Marzipan gummi bears pie <Byline author='Meow Meow Fuzzyface' />
danish lollipop pudding powder gummi bears sweet. Pie sweet roll sweet <p>
roll topping chocolate bar dragée pudding chocolate cake. Croissant sweet <a href={isAmp ? '/dog' : '/dog?amp=1'}>
chocolate bar cheesecake candy canes. Tootsie roll icing macaroon bonbon {isAmp ? 'View Non-AMP' : 'View AMP'} Version
cupcake apple pie candy canes biscuit candy canes. Jujubes jelly liquorice </a>
toffee gingerbread. Candy tootsie roll macaroon chocolate bar icing sugar </p>
plum pie. Icing gummies chocolate bar chocolate marzipan bonbon cookie <p className='caption'>Woooooooooooof</p>
chocolate tart. Caramels danish halvah croissant. Cheesecake cookie <p>
tootsie roll ice cream. Powder dessert carrot cake muffin tiramisu lemon Wafer donut candy soufflé{' '}
drops liquorice topping brownie. Soufflé chocolate cake croissant cupcake <a href={isAmp ? '/?amp=1' : '/'}>lemon drops</a> icing. Marzipan gummi
jelly. bears pie danish lollipop pudding powder gummi bears sweet. Pie sweet
</p> roll sweet roll topping chocolate bar dragée pudding chocolate cake.
<p> Croissant sweet chocolate bar cheesecake candy canes. Tootsie roll icing
Muffin gummies dessert cheesecake candy canes. Candy canes danish cotton macaroon bonbon cupcake apple pie candy canes biscuit candy canes.
candy tart dessert powder bear claw marshmallow. Muffin chocolate Jujubes jelly liquorice toffee gingerbread. Candy tootsie roll macaroon
marshmallow danish. Chocolate bar biscuit cake tiramisu. Topping sweet chocolate bar icing sugar plum pie. Icing gummies chocolate bar
brownie jujubes powder marzipan. Croissant wafer bonbon chupa chups cake chocolate marzipan bonbon cookie chocolate tart. Caramels danish halvah
cake marzipan caramels jujubes. Cupcake cheesecake sweet roll marshmallow croissant. Cheesecake cookie tootsie roll ice cream. Powder dessert
lollipop danish jujubes jelly icing. Apple pie chupa chups lollipop carrot cake muffin tiramisu lemon drops liquorice topping brownie.
jelly-o cheesecake jelly beans cake dessert. Tootsie roll tootsie roll Soufflé chocolate cake croissant cupcake jelly.
bonbon pastry croissant gummi bears cake cake. Fruitcake sugar plum halvah </p>
gingerbread cookie pastry chupa chups wafer lemon drops. Marshmallow <p>
liquorice oat cake lollipop. Lemon drops oat cake halvah liquorice danish Muffin gummies dessert cheesecake candy canes. Candy canes danish cotton
powder cupcake soufflé. Cake tart topping jelly-o tart sugar plum. candy tart dessert powder bear claw marshmallow. Muffin chocolate
Chocolate bar cookie wafer tootsie roll candy cotton candy toffee pie marshmallow danish. Chocolate bar biscuit cake tiramisu. Topping sweet
donut. brownie jujubes powder marzipan. Croissant wafer bonbon chupa chups cake
</p> cake marzipan caramels jujubes. Cupcake cheesecake sweet roll
<p> marshmallow lollipop danish jujubes jelly icing. Apple pie chupa chups
Ice cream lollipop marshmallow tiramisu jujubes croissant. Bear claw lemon lollipop jelly-o cheesecake jelly beans cake dessert. Tootsie roll
drops marzipan candy bonbon cupcake powder. Candy canes cheesecake bear tootsie roll bonbon pastry croissant gummi bears cake cake. Fruitcake
claw pastry cake donut jujubes. Icing tart jelly-o soufflé bonbon apple sugar plum halvah gingerbread cookie pastry chupa chups wafer lemon
pie. Cheesecake pie chupa chups toffee powder. Bonbon lemon drops carrot drops. Marshmallow liquorice oat cake lollipop. Lemon drops oat cake
cake pudding candy halvah cheesecake lollipop cupcake. Pudding marshmallow halvah liquorice danish powder cupcake soufflé. Cake tart topping
fruitcake. Gummi bears bonbon chupa chups lemon drops. Wafer dessert jelly-o tart sugar plum. Chocolate bar cookie wafer tootsie roll candy
gummies gummi bears biscuit donut tiramisu gummi bears brownie. Tootsie cotton candy toffee pie donut.
roll liquorice bonbon cookie. Sesame snaps chocolate bar cake croissant </p>
chupa chups cheesecake gingerbread tiramisu jelly. Cheesecake ice cream <p>
muffin lollipop gummies. Sesame snaps jelly beans sweet bear claw tart. Ice cream lollipop marshmallow tiramisu jujubes croissant. Bear claw
</p> lemon drops marzipan candy bonbon cupcake powder. Candy canes cheesecake
<p> bear claw pastry cake donut jujubes. Icing tart jelly-o soufflé bonbon
Sweet topping chupa chups chocolate cake jelly-o liquorice danish. Pastry apple pie. Cheesecake pie chupa chups toffee powder. Bonbon lemon drops
jelly beans apple pie dessert pastry lemon drops marzipan gummies. Jelly carrot cake pudding candy halvah cheesecake lollipop cupcake. Pudding
beans macaroon bear claw cotton candy. Toffee sweet lollipop toffee oat marshmallow fruitcake. Gummi bears bonbon chupa chups lemon drops. Wafer
cake. Jelly-o oat cake fruitcake chocolate bar sweet. Lemon drops gummies dessert gummies gummi bears biscuit donut tiramisu gummi bears brownie.
chocolate cake lollipop bear claw croissant danish icing. Chocolate bar Tootsie roll liquorice bonbon cookie. Sesame snaps chocolate bar cake
donut brownie chocolate cake lemon drops chocolate bar. Cake fruitcake croissant chupa chups cheesecake gingerbread tiramisu jelly. Cheesecake
pudding chocolate apple pie. Brownie tiramisu chocolate macaroon lemon ice cream muffin lollipop gummies. Sesame snaps jelly beans sweet bear
drops wafer soufflé jujubes icing. Cheesecake tiramisu cake macaroon tart claw tart.
lollipop donut. Gummi bears dragée pudding bear claw. Muffin cake cupcake </p>
candy canes. Soufflé candy canes biscuit. Macaroon gummies danish. <p>
</p> Sweet topping chupa chups chocolate cake jelly-o liquorice danish.
<p> Pastry jelly beans apple pie dessert pastry lemon drops marzipan
Cupcake cupcake tart. Cotton candy danish candy canes oat cake ice cream gummies. Jelly beans macaroon bear claw cotton candy. Toffee sweet
candy canes powder wafer. Chocolate sesame snaps oat cake dragée lollipop toffee oat cake. Jelly-o oat cake fruitcake chocolate bar
cheesecake. Sesame snaps marshmallow topping liquorice cookie marshmallow. sweet. Lemon drops gummies chocolate cake lollipop bear claw croissant
Liquorice pudding chocolate bar. Cake powder brownie fruitcake. Carrot danish icing. Chocolate bar donut brownie chocolate cake lemon drops
cake dessert marzipan sugar plum cupcake cheesecake pastry. Apple pie chocolate bar. Cake fruitcake pudding chocolate apple pie. Brownie
macaroon ice cream fruitcake apple pie cookie. Tootsie roll ice cream oat tiramisu chocolate macaroon lemon drops wafer soufflé jujubes icing.
cake cheesecake donut cheesecake bear claw. Sesame snaps marzipan jelly Cheesecake tiramisu cake macaroon tart lollipop donut. Gummi bears
beans chocolate tootsie roll. Chocolate bar donut dragée ice cream dragée pudding bear claw. Muffin cake cupcake candy canes. Soufflé candy
biscuit. Pie candy canes muffin candy canes ice cream tiramisu. canes biscuit. Macaroon gummies danish.
</p> </p>
</div> <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.
/* 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