mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Rewrite urls with hashes correct for static export. (#2242)
* Rewrite urls with hashes correct for static export. * Fix some lint issues inside an example app.
This commit is contained in:
parent
320b94a94b
commit
a36be58124
|
@ -1,7 +1,7 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
export default class extends React.PureComponent {
|
export default class extends React.PureComponent {
|
||||||
componentDidMount() {
|
componentDidMount () {
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
navigator.serviceWorker
|
navigator.serviceWorker
|
||||||
.register('/service-worker.js')
|
.register('/service-worker.js')
|
||||||
|
@ -9,11 +9,11 @@ export default class extends React.PureComponent {
|
||||||
console.log('service worker registration successful')
|
console.log('service worker registration successful')
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.warn('service worker registration failed')
|
console.warn('service worker registration failed', err.message)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
render() {
|
render () {
|
||||||
return (
|
return (
|
||||||
<p>Check the console for the Service Worker registration status.</p>
|
<p>Check the console for the Service Worker registration status.</p>
|
||||||
)
|
)
|
||||||
|
|
|
@ -87,21 +87,20 @@ export function _notifyBuildIdMismatch (nextRoute) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function _rewriteUrlForNextExport (url) {
|
export function _rewriteUrlForNextExport (url) {
|
||||||
// If there are no query strings
|
const [, hash] = url.split('#')
|
||||||
if (!/\?/.test(url)) {
|
url = url.replace(/#.*/, '')
|
||||||
return rewritePath(url)
|
|
||||||
|
let [path, qs] = url.split('?')
|
||||||
|
path = path.replace(/\/$/, '')
|
||||||
|
|
||||||
|
let newPath = `${path}/`
|
||||||
|
if (qs) {
|
||||||
|
newPath = `${newPath}?${qs}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const [path, qs] = url.split('?')
|
if (hash) {
|
||||||
|
newPath = `${newPath}#${hash}`
|
||||||
const newPath = rewritePath(path)
|
|
||||||
return `${newPath}?${qs}`
|
|
||||||
|
|
||||||
function rewritePath (path) {
|
|
||||||
// If ends with slash simply return that path
|
|
||||||
if (/\/$/.test(path)) {
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
return `${path}/`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return newPath
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,24 @@
|
||||||
|
/* global location */
|
||||||
|
import React from 'react'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
|
|
||||||
const DynamicPage = ({ text }) => (
|
export default class DynamicPage extends React.Component {
|
||||||
|
state = {}
|
||||||
|
|
||||||
|
static getInitialProps ({ query }) {
|
||||||
|
return { text: query.text }
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount () {
|
||||||
|
const [, hash] = location.href.split('#')
|
||||||
|
this.setState({ hash })
|
||||||
|
}
|
||||||
|
|
||||||
|
render () {
|
||||||
|
const { text } = this.props
|
||||||
|
const { hash } = this.state
|
||||||
|
|
||||||
|
return (
|
||||||
<div id='dynamic-page'>
|
<div id='dynamic-page'>
|
||||||
<div>
|
<div>
|
||||||
<Link href='/'>
|
<Link href='/'>
|
||||||
|
@ -8,11 +26,8 @@ const DynamicPage = ({ text }) => (
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
<p>{ text }</p>
|
<p>{ text }</p>
|
||||||
|
<div id='hash'>Hash: {hash}</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
}
|
||||||
DynamicPage.getInitialProps = ({ query }) => {
|
|
||||||
return { text: query.text }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default DynamicPage
|
|
||||||
|
|
|
@ -39,6 +39,11 @@ export default () => (
|
||||||
>
|
>
|
||||||
<a id='dynamic-2'>Dynamic 2</a>
|
<a id='dynamic-2'>Dynamic 2</a>
|
||||||
</Link>
|
</Link>
|
||||||
|
<Link
|
||||||
|
href='/dynamic?text=zeit+is+awesome#cool'
|
||||||
|
>
|
||||||
|
<a id='with-hash'>With Hash</a>
|
||||||
|
</Link>
|
||||||
<Link href='/level1'>
|
<Link href='/level1'>
|
||||||
<a id='level1-home-page'>Level1 home page</a>
|
<a id='level1-home-page'>Level1 home page</a>
|
||||||
</Link>
|
</Link>
|
||||||
|
|
|
@ -111,6 +111,30 @@ export default function (context) {
|
||||||
browser.close()
|
browser.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should render pages with url hash correctly', async () => {
|
||||||
|
const browser = await webdriver(context.port, '/')
|
||||||
|
|
||||||
|
// Check for the query string content
|
||||||
|
const text = await browser
|
||||||
|
.elementByCss('#with-hash').click()
|
||||||
|
.waitForElementByCss('#dynamic-page')
|
||||||
|
.elementByCss('#dynamic-page p').text()
|
||||||
|
|
||||||
|
expect(text).toBe('zeit is awesome')
|
||||||
|
|
||||||
|
// Check for the hash
|
||||||
|
while (true) {
|
||||||
|
const hashText = await browser
|
||||||
|
.elementByCss('#hash').text()
|
||||||
|
|
||||||
|
if (/cool/.test(hashText)) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
browser.close()
|
||||||
|
})
|
||||||
|
|
||||||
describe('pages in the nested level: level1', () => {
|
describe('pages in the nested level: level1', () => {
|
||||||
it('should render the home page', async () => {
|
it('should render the home page', async () => {
|
||||||
const browser = await webdriver(context.port, '/')
|
const browser = await webdriver(context.port, '/')
|
||||||
|
|
Loading…
Reference in a new issue