Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shallow routing #27

Merged
merged 3 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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