Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created May 15, 2020 13:55
Show Gist options
  • Save sibelius/85789f6b82c2a79cc3dc0452a72ddce8 to your computer and use it in GitHub Desktop.
Save sibelius/85789f6b82c2a79cc3dc0452a72ddce8 to your computer and use it in GitHub Desktop.
initEnvironment to be used for SSR
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,
log: process.env.NODE_ENV === 'development' ? relayTransactionLogger : null,
});
}
// reuse Relay environment on client-side
if (!relayEnvironment) {
relayEnvironment = new Environment({
configName: 'client',
network,
store,
log: process.env.NODE_ENV === 'development' ? relayTransactionLogger : null,
});
}
return relayEnvironment;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment