-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add type/field policy code splitting docs section #6766
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,3 +366,65 @@ persistCache({ | |
``` | ||
|
||
For more advanced usage, such as persisting the cache when the app is in the background, and additional configuration options, please check the [README of `apollo-cache-persist`](https://github.com/apollographql/apollo-cache-persist). | ||
|
||
## Code splitting | ||
|
||
Depending on the complexity and size of your cache type/field policies, you might not always want to define them up front, when you create your initial `InMemoryCache` instance. If you have type/field policies that are only needed in a specific part of your application, you can leverage `InMemoryCache`'s `.policies.addTypePolicies()` method to adjust your cache policy map at any point. This can be really useful when leveraging techniques like route based code-splitting, using a tool like [`react-loadable`](https://www.npmjs.com/package/react-loadable). | ||
|
||
Let's say we're building a messaging app and have a `/stats` route that is used to return the total number of messages stored locally. If we use `react-loadable` to load our `Stats` component like: | ||
|
||
```jsx | ||
import Loadable from 'react-loadable'; | ||
|
||
import Loading from './components/Loading'; | ||
|
||
export const Stats = Loadable({ | ||
loader: () => import('./components/stats/Stats'), | ||
loading: Loading, | ||
}); | ||
``` | ||
|
||
and wait until our `Stats` component is called to add a new type/field policy (using `cache.policies.addTypePolicies()`): | ||
|
||
```jsx:title=Stats.jsx | ||
import React from "react"; | ||
import { useApolloClient, useQuery, gql } from "@apollo/client"; | ||
|
||
const GET_MESSAGE_COUNT = gql` | ||
{ | ||
messageCount @client { | ||
total | ||
} | ||
} | ||
`; | ||
|
||
const newPolicy = { | ||
Query: { | ||
fields: { | ||
messageCount() { | ||
// calculate and return the number of messages stored locally ... | ||
return { | ||
total: 123, | ||
}; | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export function Stats() { | ||
const client = useApolloClient(); | ||
client.cache.policies.addTypePolicies(newPolicy); | ||
Comment on lines
+414
to
+416
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to avoid re-adding the policies every time the component is rendered. I realize the component function gives us access to the client/cache, but maybe we can come up with a better pattern here? Maybe keep a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing @benjamn, I'l adjust. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great to clarify the best practice here. We could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder how that relates to this comment (that the query gets fired before to policies are loaded, unless you use a lazy query). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the link @coler-j, the effect may be the reason why I was seeing the failure in the issue I opened (as you linked). This is our hook that adds type policies: https://github.com/magento/pwa-studio/blob/develop/packages/peregrine/lib/hooks/useTypePolicies.js#L29. |
||
|
||
const { loading, data: { messageCount } } = useQuery(GET_MESSAGE_COUNT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix @benjamn - thanks! |
||
|
||
if (loading) return "Loading ..."; | ||
|
||
return ( | ||
<p> | ||
Total number of messages: {messageCount.total} | ||
</p> | ||
); | ||
}; | ||
``` | ||
|
||
our type/field policy code will only be included in the bundle a user downloads when (if) they access `/stats`. It won't be included in the initial application bundle, which helps keep the size of our initial bundle down, and ultimately helps with download and application startup times. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend documenting "Loadable Components" instead since
react-loadable
is more or less unmaintained and the official React docs have moved on with their suggestion: https://reactjs.org/docs/code-splitting.html#reactlazyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point @bwhitty! These docs were carried over / modified from the AC 2.x docs, so they definitely need to be updated further. Thanks!