File tree 5 files changed +73
-30
lines changed
examples/with-sw-precache/pages
5 files changed +73
-30
lines changed Original file line number Diff line number Diff line change 1
1
import React from 'react'
2
2
3
3
export default class extends React . PureComponent {
4
- componentDidMount ( ) {
4
+ componentDidMount ( ) {
5
5
if ( 'serviceWorker' in navigator ) {
6
6
navigator . serviceWorker
7
7
. register ( '/service-worker.js' )
8
8
. then ( registration => {
9
9
console . log ( 'service worker registration successful' )
10
10
} )
11
11
. catch ( err => {
12
- console . warn ( 'service worker registration failed' )
12
+ console . warn ( 'service worker registration failed' , err . message )
13
13
} )
14
14
}
15
15
}
16
- render ( ) {
16
+ render ( ) {
17
17
return (
18
18
< p > Check the console for the Service Worker registration status.</ p >
19
19
)
Original file line number Diff line number Diff line change @@ -87,21 +87,20 @@ export function _notifyBuildIdMismatch (nextRoute) {
87
87
}
88
88
89
89
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 ( / # .* / , '' )
94
92
95
- const [ path , qs ] = url . split ( '?' )
93
+ let [ path , qs ] = url . split ( '?' )
94
+ path = path . replace ( / \/ $ / , '' )
96
95
97
- const newPath = rewritePath ( path )
98
- return `${ newPath } ?${ qs } `
96
+ let newPath = `${ path } /`
97
+ if ( qs ) {
98
+ newPath = `${ newPath } ?${ qs } `
99
+ }
99
100
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 } `
106
103
}
104
+
105
+ return newPath
107
106
}
Original file line number Diff line number Diff line change
1
+ /* global location */
2
+ import React from 'react'
1
3
import Link from 'next/link'
2
4
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 = { }
13
7
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
+ }
17
16
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
+ }
Original file line number Diff line number Diff line change @@ -39,6 +39,11 @@ export default () => (
39
39
>
40
40
< a id = 'dynamic-2' > Dynamic 2</ a >
41
41
</ Link >
42
+ < Link
43
+ href = '/dynamic?text=zeit+is+awesome#cool'
44
+ >
45
+ < a id = 'with-hash' > With Hash</ a >
46
+ </ Link >
42
47
< Link href = '/level1' >
43
48
< a id = 'level1-home-page' > Level1 home page</ a >
44
49
</ Link >
Original file line number Diff line number Diff line change @@ -111,6 +111,30 @@ export default function (context) {
111
111
browser . close ( )
112
112
} )
113
113
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 ( / c o o l / . test ( hashText ) ) {
131
+ break
132
+ }
133
+ }
134
+
135
+ browser . close ( )
136
+ } )
137
+
114
138
describe ( 'pages in the nested level: level1' , ( ) => {
115
139
it ( 'should render the home page' , async ( ) => {
116
140
const browser = await webdriver ( context . port , '/' )
You can’t perform that action at this time.
0 commit comments