forked from magento/pwa-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Router working with real routes (#21)
* Working router implementation using GraphQL API * First fully working iteration of Router integrated with API * Add docs for router * Fix misleading comment * Bug fix * Add test to validate we dont double-fetch components * Router >> using
- Loading branch information
Showing
19 changed files
with
501 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Peregrine Router | ||
|
||
The Peregrine Router is a client-side router that is designed to understand the | ||
different storefront routes within Magento 2. If using Peregrine to bootstrap | ||
your PWA, it is configured automatically. If not, the Router can be manually | ||
consumed. | ||
|
||
## Manual Usage | ||
|
||
```jsx | ||
import ReactDOM from 'react-dom'; | ||
import { Router } from '@magento/peregrine'; | ||
|
||
ReactDOM.render( | ||
<Router apiBase="https://mystore.com" />, | ||
document.querySelector('main') | ||
); | ||
``` | ||
|
||
## Props | ||
|
||
| Prop Name | Required? | Description | | ||
| ------------- | :-------: | -----------------------------------------------------------------------------------------------------: | | ||
| `apiBase` | ✅ | Root URL of the Magento store, including protocol and hostname | | ||
| `using` | | The Router implementation to use from React-Router. Can be `BrowserRouter`/`HashRouter`/`MemoryRouter` | | ||
| `routerProps` | | Any additional props to be passed to React-Router | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
module.exports = { | ||
setupFiles: ['<rootDir>/scripts/shim.js'], | ||
setupFiles: [ | ||
'<rootDir>/scripts/shim.js', | ||
'<rootDir>/scripts/fetch-mock.js' | ||
], | ||
verbose: true, | ||
collectCoverageFrom: ['src/**/*.js'] | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global.fetch = require('jest-fetch-mock'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { createElement, Component } from 'react'; | ||
import { string, shape } from 'prop-types'; | ||
import resolveUnknownRoute from './resolveUnknownRoute'; | ||
import fetchRootComponent from './fetchRootComponent'; | ||
|
||
export default class MagentoRouteHandler extends Component { | ||
static propTypes = { | ||
apiBase: string.isRequired, | ||
__tmp_webpack_public_path__: string.isRequired, | ||
location: shape({ | ||
pathname: string.isRequired | ||
}).isRequired | ||
}; | ||
|
||
state = {}; | ||
|
||
componentDidMount() { | ||
this.getRouteComponent(this.props.location.pathname); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
const { location } = this.props; | ||
const changed = nextProps.location.pathname !== location.pathname; | ||
const seen = !!this.state[nextProps.location.pathname]; | ||
|
||
if (changed && !seen) { | ||
this.getRouteComponent(nextProps.location.pathname); | ||
} | ||
} | ||
|
||
getRouteComponent(pathname) { | ||
const { apiBase, __tmp_webpack_public_path__ } = this.props; | ||
|
||
resolveUnknownRoute({ | ||
route: pathname, | ||
apiBase, | ||
__tmp_webpack_public_path__ | ||
}) | ||
.then(({ rootChunkID, rootModuleID, matched }) => { | ||
if (!matched) { | ||
// TODO: User-defined 404 page | ||
// when the API work is done to support it | ||
throw new Error('404'); | ||
} | ||
return fetchRootComponent(rootChunkID, rootModuleID); | ||
}) | ||
.then(Component => { | ||
this.setState({ | ||
[pathname]: Component | ||
}); | ||
}) | ||
.catch(err => { | ||
console.log('Routing resolve failed\n', err); | ||
}); | ||
} | ||
|
||
render() { | ||
const { location } = this.props; | ||
const Page = this.state[location.pathname]; | ||
|
||
if (!Page) { | ||
// TODO (future iteration): User-defined loading content | ||
return <div>Loading</div>; | ||
} | ||
|
||
return <Page />; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.