Skip to content

Commit a36be58

Browse files
authored
Rewrite urls with hashes correct for static export. (vercel#2242)
* Rewrite urls with hashes correct for static export. * Fix some lint issues inside an example app.
1 parent 320b94a commit a36be58

File tree

5 files changed

+73
-30
lines changed

5 files changed

+73
-30
lines changed

examples/with-sw-precache/pages/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import React from 'react'
22

33
export default class extends React.PureComponent {
4-
componentDidMount() {
4+
componentDidMount () {
55
if ('serviceWorker' in navigator) {
66
navigator.serviceWorker
77
.register('/service-worker.js')
88
.then(registration => {
99
console.log('service worker registration successful')
1010
})
1111
.catch(err => {
12-
console.warn('service worker registration failed')
12+
console.warn('service worker registration failed', err.message)
1313
})
1414
}
1515
}
16-
render() {
16+
render () {
1717
return (
1818
<p>Check the console for the Service Worker registration status.</p>
1919
)

lib/router/index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,20 @@ export function _notifyBuildIdMismatch (nextRoute) {
8787
}
8888

8989
export function _rewriteUrlForNextExport (url) {
90-
// If there are no query strings
91-
if (!/\?/.test(url)) {
92-
return rewritePath(url)
93-
}
90+
const [, hash] = url.split('#')
91+
url = url.replace(/#.*/, '')
9492

95-
const [path, qs] = url.split('?')
93+
let [path, qs] = url.split('?')
94+
path = path.replace(/\/$/, '')
9695

97-
const newPath = rewritePath(path)
98-
return `${newPath}?${qs}`
96+
let newPath = `${path}/`
97+
if (qs) {
98+
newPath = `${newPath}?${qs}`
99+
}
99100

100-
function rewritePath (path) {
101-
// If ends with slash simply return that path
102-
if (/\/$/.test(path)) {
103-
return path
104-
}
105-
return `${path}/`
101+
if (hash) {
102+
newPath = `${newPath}#${hash}`
106103
}
104+
105+
return newPath
107106
}
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1+
/* global location */
2+
import React from 'react'
13
import Link from 'next/link'
24

3-
const DynamicPage = ({ text }) => (
4-
<div id='dynamic-page'>
5-
<div>
6-
<Link href='/'>
7-
<a>Go Back</a>
8-
</Link>
9-
</div>
10-
<p>{ text }</p>
11-
</div>
12-
)
5+
export default class DynamicPage extends React.Component {
6+
state = {}
137

14-
DynamicPage.getInitialProps = ({ query }) => {
15-
return { text: query.text }
16-
}
8+
static getInitialProps ({ query }) {
9+
return { text: query.text }
10+
}
11+
12+
componentDidMount () {
13+
const [, hash] = location.href.split('#')
14+
this.setState({ hash })
15+
}
1716

18-
export default DynamicPage
17+
render () {
18+
const { text } = this.props
19+
const { hash } = this.state
20+
21+
return (
22+
<div id='dynamic-page'>
23+
<div>
24+
<Link href='/'>
25+
<a>Go Back</a>
26+
</Link>
27+
</div>
28+
<p>{ text }</p>
29+
<div id='hash'>Hash: {hash}</div>
30+
</div>
31+
)
32+
}
33+
}

test/integration/static/pages/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export default () => (
3939
>
4040
<a id='dynamic-2'>Dynamic 2</a>
4141
</Link>
42+
<Link
43+
href='/dynamic?text=zeit+is+awesome#cool'
44+
>
45+
<a id='with-hash'>With Hash</a>
46+
</Link>
4247
<Link href='/level1'>
4348
<a id='level1-home-page'>Level1 home page</a>
4449
</Link>

test/integration/static/test/browser.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ export default function (context) {
111111
browser.close()
112112
})
113113

114+
it('should render pages with url hash correctly', async () => {
115+
const browser = await webdriver(context.port, '/')
116+
117+
// Check for the query string content
118+
const text = await browser
119+
.elementByCss('#with-hash').click()
120+
.waitForElementByCss('#dynamic-page')
121+
.elementByCss('#dynamic-page p').text()
122+
123+
expect(text).toBe('zeit is awesome')
124+
125+
// Check for the hash
126+
while (true) {
127+
const hashText = await browser
128+
.elementByCss('#hash').text()
129+
130+
if (/cool/.test(hashText)) {
131+
break
132+
}
133+
}
134+
135+
browser.close()
136+
})
137+
114138
describe('pages in the nested level: level1', () => {
115139
it('should render the home page', async () => {
116140
const browser = await webdriver(context.port, '/')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy