Skip to content

Commit

Permalink
Merge branch 'master' into code-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaa77 committed Jan 13, 2020
2 parents 2ab908d + 3112cd4 commit 7088e27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "@jaredpalmer/after",
"private": false,
"version": "1.3.2",
"version": "1.4.0",
"description": "Build isomorphic Javascript applications with ease.",
"repository": "jaredpalmer/after.js",
"author": "Jared Palmer <jared@palmer.net>",
Expand Down
42 changes: 15 additions & 27 deletions src/After.tsx
Expand Up @@ -47,28 +47,26 @@ class Afterparty extends React.Component<AfterpartyProps, AfterpartyState> {
componentWillReceiveProps(nextProps: AfterpartyProps) {
const navigated = nextProps.location !== this.props.location;
if (navigated) {
// save the location so we can render the old screen
// save the location and data so we can render the old screen
// first we try to use previousLocation and then location from props
this.setState({
previousLocation: this.props.location,
data: undefined, // unless you want to keep it
previousLocation: this.state.previousLocation || this.props.location,
});

const {
data,
match,
routes,
history,
location,
staticContext,
...rest
} = nextProps;
const { location: currentLocation } = this.props
const { data, match, routes, history, location, staticContext, ...rest } = nextProps;

loadInitialProps(this.props.routes, nextProps.location.pathname, {
location: nextProps.location,
history: nextProps.history,
...rest,
})
.then(({ data }) => {
// if data is not for current location just don't do anything
if (currentLocation !== nextProps.location) {
return
}

// Only for page changes, prevent scroll up for anchor links
if (
(this.state.previousLocation &&
Expand Down Expand Up @@ -102,28 +100,18 @@ class Afterparty extends React.Component<AfterpartyProps, AfterpartyState> {
render() {
const { previousLocation, data } = this.state;
const { location } = this.props;
const initialData = this.prefetcherCache[location.pathname] || data;
const initialData = this.prefetcherCache[(previousLocation || location).pathname] || data;

return (
<Switch>
{initialData &&
initialData.statusCode &&
initialData.statusCode === 404 && (
<Route
component={this.NotfoundComponent}
path={location.pathname}
/>
)}
{initialData && initialData.redirectTo && initialData.redirectTo && (
<Redirect to={initialData.redirectTo} />
)}
<Switch location={previousLocation || location}>
{initialData && initialData.statusCode && initialData.statusCode === 404 && <Route component={this.NotfoundComponent} path={location.pathname} />}
{initialData && initialData.redirectTo && initialData.redirectTo && <Redirect to={initialData.redirectTo} />}
{getAllRoutes(this.props.routes).map((r, i) => (
<Route
key={`route--${i}`}
path={r.path}
exact={r.exact}
location={previousLocation || location}
render={props =>
render={(props) =>
React.createElement(r.component, {
...initialData,
history: props.history,
Expand Down

0 comments on commit 7088e27

Please sign in to comment.