File size: 2,006 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Component } from 'react'
import Link from 'next/link'
import Router, { withRouter } from 'next/router'

let getInitialPropsRunCount = 1

const linkStyle = {
  marginRight: 10,
}

export default withRouter(
  class extends Component {
    static getInitialProps({ res }) {
      if (res) return { getInitialPropsRunCount: 1 }
      getInitialPropsRunCount++

      return { getInitialPropsRunCount }
    }

    getCurrentCounter() {
      const { router } = this.props
      return router.query.counter ? parseInt(router.query.counter) : 0
    }

    increase(scroll) {
      const counter = this.getCurrentCounter()
      const href = `/nav/shallow-routing?counter=${counter + 1}`
      Router.push(href, href, { shallow: true, scroll })
    }

    increaseNonShallow() {
      const counter = this.getCurrentCounter()
      const href = `/nav/shallow-routing?counter=${counter + 1}`
      Router.push(href, href, {})
    }

    gotoNavShallow() {
      const href = `/nav`
      Router.push(href, href, { shallow: true })
    }

    render() {
      return (
        <div className="shallow-routing">
          <Link href="/nav" id="home-link" style={linkStyle}>
            Home
          </Link>
          <div id="counter" style={{ marginBottom: 4000 }}>
            Counter: {this.getCurrentCounter()}
          </div>
          <div id="get-initial-props-run-count">
            getInitialProps run count: {this.props.getInitialPropsRunCount}
          </div>
          <button id="increase" onClick={() => this.increase()}>
            Increase
          </button>
          <button id="increaseWithScroll" onClick={() => this.increase(true)}>
            Increase with scroll
          </button>
          <button id="increase2" onClick={() => this.increaseNonShallow()}>
            Increase Non-Shallow
          </button>
          <button id="invalidShallow" onClick={() => this.gotoNavShallow()}>
            Invalid Shallow Nav
          </button>
        </div>
      )
    }
  }
)