Skip to content

Commit

Permalink
Use shallow routing (#27)
Browse files Browse the repository at this point in the history
* Use shallow routing

* Reset state if not going to a search url

* Only prefetch once
  • Loading branch information
timneutkens authored and timothyis committed Apr 16, 2018
1 parent 94b701f commit 2e857cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
37 changes: 27 additions & 10 deletions components/Layout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import Router from 'next/router'
import { withRouter } from 'next/router'
import Meta from './Meta'
import Header from './Header'
import SearchList from './SearchList'
Expand All @@ -8,34 +8,49 @@ import LinuxLogo from '../static/linux-logo.svg'
import WindowsLogo from '../static/windows-logo.svg'
import * as gtag from '../lib/gtag'
import RouterEvents from '../lib/router-events'
import { format } from 'url'

RouterEvents.on('routeChangeComplete', url => {
gtag.pageview(url)
})

export default class extends React.Component {
class Layout extends React.Component {
constructor() {
super()

this.handleSearch = this.handleSearch.bind(this)
}

componentWillReceiveProps(nextProps) {
const { router } = nextProps
if (!/^\/search/.exec(router.asPath)) {
this.setState({
searchQuery: null,
originalURL: router.asPath
})
}
}

componentDidMount() {
const { router } = this.props
this.setState({
originalURL: Router.asPath
originalURL: router.asPath
})
}

handleSearch(query) {
const { router } = this.props
if (query) {
const url = `/search?q=${query}`
window.history.replaceState(
query,
`Hyper Store - Searching for ${query}`,
url
)
// We construct the original href for shallow routing
const href = format({ pathname: router.pathname, query: router.query })
// asPath will be different depending on user input
const asPath = `/search?q=${query}`
router.replace(href, asPath, { shallow: true })
} else {
window.history.replaceState({}, 'Hyper Store', this.state.originalURL)
// When query is empty we render the original url
router.replace(this.state.originalURL, this.state.originalURL, {
shallow: true
})
}

this.setState({
Expand Down Expand Up @@ -65,3 +80,5 @@ export default class extends React.Component {
)
}
}

export default withRouter(Layout)
16 changes: 6 additions & 10 deletions components/PluginsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default class extends React.Component {
return sortedPlugins
}

componentDidMount() {
// Optimization
Router.prefetch('/plugin')
}

render() {
const plugins = this.orderPlugins(this.props.plugins, this.props.filteredBy)

Expand All @@ -37,16 +42,7 @@ export default class extends React.Component {
return (
<div className="plugins-list container">
{plugins.map((plugin, i) => (
<div
key={plugin.name}
className="plugin"
onMouseEnter={() => {
Router.prefetch(
`/plugin?id=${plugin.name}`,
`/plugins/${plugin.name}`
)
}}
>
<div key={plugin.name} className="plugin">
<Link
href={`/plugin?id=${plugin.name}`}
as={`/plugins/${plugin.name}`}
Expand Down

0 comments on commit 2e857cf

Please sign in to comment.