Created
February 26, 2020 12:32
-
-
Save sibelius/02f50e9bb4fdb6f79b33d9dad2193819 to your computer and use it in GitHub Desktop.
Relay Environment to work well in SSR
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
let relayEnvironment = null; | |
export const initEnvironment = (records = {}) => { | |
const network = Network.create(cacheHandler); | |
const source = new RecordSource(records); | |
const store = new Store(source, { | |
// This property tells Relay to not immediately clear its cache when the user | |
// navigates around the app. Relay will hold onto the specified number of | |
// query results, allowing the user to return to recently visited pages | |
// and reusing cached data if its available/fresh. | |
gcReleaseBufferSize: 10, | |
}); | |
// Make sure to create a new Relay environment for every server-side request so that data | |
// isn't shared between connections (which would be bad) | |
if (typeof window === 'undefined') { | |
return new Environment({ | |
configName: 'server', | |
network, | |
store, | |
}); | |
} | |
// reuse Relay environment on client-side | |
if (!relayEnvironment) { | |
relayEnvironment = new Environment({ | |
configName: 'client', | |
network, | |
store, | |
}); | |
} | |
return relayEnvironment; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment