Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function initCache (ctx, App, client) {
try {
const AppWithApollo = h(ApolloProvider, { client }, h(App))
await getDataFromTree(
ctx.app.routable(AppWithApollo)
)
} catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop:
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
console.error('Error while running `getDataFromTree`', error)
}
}
async function initCache (ctx, App, client) {
try {
const AppWithApollo = h(ApolloProvider, { client }, h(App))
await getDataFromTree(
ctx.app.routable(AppWithApollo)
)
} catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop:
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
console.error('Error while running `getDataFromTree`', error)
}
}
headers: {
cookie: req.header('Cookie'),
},
fetch,
}),
cache: new InMemoryCache(),
});
const context = {};
const App =
;
await getDataFromTree(App)
const html = ReactDOMServer.renderToString((App));
const initialState = client.extract();
res.write(`
<title>Conduit</title>
: {}))
};
if (!process.browser) {
const client = apolloClient(headers || {}, token || '', {}, ctx);
const store = reduxStore();
try {
const app = (
);
await getDataFromTree(app);
} catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop:
// https://github.com/apollographql/react-apollo/issues/406
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
}
apolloState = client.cache.extract();
serverState = store.getState();
}
return {
reduxState: serverState,
apolloState,
headers,
...props
// Generate unique request id
const requestId = Math.random()
.toString(36)
.substring(7)
const rootElement = wrapRootElement(config)({
element: jsx,
pathname: req.url,
requestId,
})
// Loadable components
jsx = webExtractor.collectChunks(jsx)
await getDataFromTree(jsx)
const apolloState = apolloClient.cache.extract()
// Render app HTML
const appHtml = renderToString(rootElement)
// Handle React router status
if (routerContext.status) {
res.status(routerContext.status)
}
// Handle React Router redirection
if (routerContext.url) {
const status = routerContext.status === 301 ? 301 : 302
res.redirect(status, routerContext.url)
return
}
const context = {};
const App = clientModules.getWrappedRoot(
{clientModules.getDataRoot(
{Routes}
)}
,
req
);
await getDataFromTree(App);
if (context.pageNotFound === true) {
res.status(404);
} else {
res.status(200);
}
const sheet = new ServerStyleSheet();
const html = ReactDOMServer.renderToString(sheet.collectStyles(App));
const css = sheet.getStyleElement().map((el, idx) => (el ? React.cloneElement(el, { key: idx }) : el));
const helmet = Helmet.renderStatic(); // Avoid memory leak while tracking mounted instances
if (context.url) {
res.writeHead(301, { Location: context.url });
res.end();
} else {
props: ctx.query,
});
}
const props = {
url: { ...ctx.query, pathname: ctx.pathname },
...await (Component.getInitialProps ? Component.getInitialProps(ctx) : {}),
};
if (!process.browser) {
const app = (
);
await getDataFromTree(app);
}
const state = store.getState();
return {
initialState: {
...state,
apollo: {
data: client.getInitialState().data,
},
},
headers,
...props,
};
}
const onCreateStore = (instance) => {
onCreateStore.store = instance;
};
const Router = (props) => ;
const app = (
);
await getDataFromTree(app);
const body = renderToString(app);
const html = renderToStaticMarkup(
{body}
,
);
res.setHeader('Cache-Control', 'max-age=0, no-cache');
res.send(`${html}`);
} catch (error) {
opts: {
credentials: 'include',
headers: {
cookie: req.header('Cookie'),
},
},
}),
});
const app = (
);
await getDataFromTree(app);
const state = { apollo: client.getInitialState() };
const content = ReactDOMServer.renderToStaticMarkup(app);
const helmet = Helmet.renderStatic();
const html = ReactDOMServer.renderToStaticMarkup(
,
);
res.status(200);
res.send(`${html}`);
res.end();
});
const client = initClient(headers)
const store = initStore(client, client.initialState)
const props = {
url: { query: ctx.query, pathname: ctx.pathname },
...await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
}
if (!process.browser) {
const app = (
)
await getDataFromTree(app)
}
const state = store.getState()
return {
initialState: {
...state,
apollo: {
data: client.getInitialState().data
}
},
headers,
...props
}
}