Skip to content

Commit 06dfa9c

Browse files
chore(examples): Add two redux examples (#31928)
Co-authored-by: LekoArts <[email protected]>
1 parent 66c7b79 commit 06dfa9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+9251
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Gatsby Redux Lazy Loading
2+
3+
## Goal
4+
5+
The goal is to show a pattern of _not_ just loading a slice of a Redux store when a component mounts, but _when the user interacts with the site_. In this case, there is an expensive reducer that:
6+
7+
- has a large 3rd party dependency
8+
- imports a large JSON file and sets it in the store
9+
10+
This is very useful for parts of the store that are:
11+
12+
- not present on every page
13+
- not present on a page without a certain user action
14+
15+
For less expensive slices of the store, this pattern may be overkill, but for expensive parts it is worth it.
16+
17+
## Why
18+
19+
The reason we have to be especially "careful" with Redux with Gatsby is that Gatsby's default code-splitting will be looking for code that is unique to a certain page and bundle it up, but if it sees code across more than one page, it will end up in the `app` or `commons` bundle and be loaded on every page, whether it is used or not. In this example the large 3rd party dependency and large array of dummy data would otherwise be loaded on every page, over 400kb! [See this article by Ben Robertson on Gatsby code splitting](https://benrobertson.io/notes/gatsby-and-bundle-chunking).
20+
21+
## Pattern
22+
23+
The app works based on the [implementation of the Redux Modules or Reducer Registry pattern found here](https://nicolasgallagher.com/redux-modules-and-code-splitting/) by Nicholas Gallagher.
24+
25+
## Running Locally
26+
27+
```shell
28+
cd examples/using-redux-w-interaction-code-splitting
29+
npm install
30+
npm run start
31+
```
32+
33+
## Live demo:
34+
35+
[gatsbyreduxcodesplittingexampl.gtsb.io](https://gatsbyreduxcodesplittingexampl.gtsb.io)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as React from "react"
2+
import WrapRootElement from "./src/redux/reduxWrapper"
3+
4+
export const wrapRootElement = ({ element }) => (
5+
<WrapRootElement element={element} />
6+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
siteMetadata: {
3+
title: `Gatsby + Dynamic Redux`,
4+
description: `Lazy load slices of your Redux store based on user interaction.`,
5+
author: `Grayson Hicks`,
6+
},
7+
plugins: [
8+
`gatsby-plugin-react-helmet`,
9+
`gatsby-transformer-sharp`,
10+
`gatsby-plugin-sharp`,
11+
{
12+
resolve: `gatsby-plugin-manifest`,
13+
options: {
14+
name: `gatsby-starter-default`,
15+
short_name: `starter`,
16+
start_url: `/`,
17+
background_color: `#663399`,
18+
theme_color: `#663399`,
19+
display: `minimal-ui`,
20+
icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
21+
},
22+
},
23+
`gatsby-plugin-perf-budgets`,
24+
],
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
exports.onCreateWebpackConfig = ({ actions }) => {
2+
// Turn off polyfills for the large library we are importing in dummy.js so build is successful. Not relevant to overall purpose of the example.
3+
actions.setWebpackConfig({
4+
resolve: {
5+
fallback: {
6+
stream: false,
7+
http: false,
8+
},
9+
},
10+
})
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as React from "react"
2+
import WrapRootElement from "./src/redux/reduxWrapper"
3+
4+
export const wrapRootElement = ({ element }) => (
5+
<WrapRootElement element={element} />
6+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "gatsby-starter-default",
3+
"private": true,
4+
"description": "A simple starter to get up and developing quickly with Gatsby",
5+
"version": "0.1.0",
6+
"author": "Kyle Mathews <[email protected]>",
7+
"dependencies": {
8+
"gatsby": "next",
9+
"gatsby-plugin-manifest": "next",
10+
"gatsby-plugin-perf-budgets": "0.0.17",
11+
"gatsby-plugin-react-helmet": "next",
12+
"gatsby-plugin-sharp": "next",
13+
"gatsby-transformer-sharp": "next",
14+
"node-libs-browser": "^2.2.1",
15+
"prop-types": "^15.7.2",
16+
"react": "^17.0.0",
17+
"react-dom": "^17.0.0",
18+
"react-helmet": "^6.1.0",
19+
"react-redux": "^7.2.2",
20+
"redux": "^4.0.5"
21+
},
22+
"devDependencies": {
23+
"prettier": "2.2.1"
24+
},
25+
"keywords": [
26+
"gatsby"
27+
],
28+
"license": "0BSD",
29+
"scripts": {
30+
"build": "gatsby build",
31+
"develop": "gatsby develop",
32+
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
33+
"start": "npm run develop",
34+
"serve": "gatsby serve",
35+
"clean": "gatsby clean",
36+
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
37+
},
38+
"repository": {
39+
"type": "git",
40+
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
41+
},
42+
"bugs": {
43+
"url": "https://github.com/gatsbyjs/gatsby/issues"
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as React from "react"
2+
import { Link } from "gatsby"
3+
import PropTypes from "prop-types"
4+
5+
const Header = ({ siteTitle }) => (
6+
<header
7+
style={{
8+
background: `transparent`,
9+
marginBottom: `1.45rem`,
10+
}}
11+
>
12+
<div
13+
style={{
14+
margin: `0 auto`,
15+
maxWidth: 960,
16+
padding: `1.45rem 1.0875rem`,
17+
}}
18+
>
19+
<h1 style={{ margin: 0 }}>
20+
<Link
21+
to="/"
22+
style={{
23+
color: `rebeccapurple`,
24+
textDecoration: "underline",
25+
}}
26+
>
27+
{siteTitle}
28+
</Link>
29+
</h1>
30+
</div>
31+
</header>
32+
)
33+
34+
Header.propTypes = {
35+
siteTitle: PropTypes.string,
36+
}
37+
38+
Header.defaultProps = {
39+
siteTitle: ``,
40+
}
41+
42+
export default Header

0 commit comments

Comments
 (0)