How to use @apollo/react-ssr - 7 common examples

To help you get started, we’ve selected a few @apollo/react-ssr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sync / reason-graphql-demo / src / helpers / withApollo.tsx View on Github external
const { AppTree, req } = ctx;

        let pageProps = {};
        if (PageComponent.getInitialProps) {
          pageProps = await PageComponent.getInitialProps(ctx);
        }

        const baseUrl = getBaseUrl(req);

        // Run all GraphQL queries in the component tree
        // and extract the resulting data
        const apolloClient = initApolloClient(baseUrl, {});

        try {
          // Run all GraphQL queries
          await require('@apollo/react-ssr').getDataFromTree(
            ,
          );
        } catch (error) {
          // Prevent Apollo Client GraphQL errors from crashing SSR.
          // Handle them in components via the data.error prop:
          // https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-query-data-error
          console.error('Error while running `getDataFromTree`', error);
        }

        // getDataFromTree does not call componentWillUnmount
        // head side effect therefore need to be cleared manually
github letterpad / letterpad / client / server.js View on Github external
};

  const sheet = new ServerStyleSheet(); // <-- creating out stylesheet
  const clientApp = (
    
      
        
          
            
          
        
      
    
  );
  try {
    const content = await renderToStringWithData(clientApp);
    const initialState = client.extract();
    return {
      head: Helmet.renderStatic(),
      html: content,
      apolloState: initialState,
      sheet: sheet,
    };
  } catch (error) {
    console.log("error :", error);
  }
};
github letterpad / letterpad / src / client / server / app.tsx View on Github external
const sheet = new ServerStyleSheet(); // <-- creating out stylesheet
  const clientApp = (
    
      
        
          
            
              
            
          
        
      
    
  );
  try {
    const content = await renderToStringWithData(clientApp);
    const initialState = client.extract();
    return {
      head: Helmet.renderStatic(),
      html: content,
      initialData: { ...initialData, initialProps },
      apolloState: initialState,
      sheet: sheet,
    };
  } catch (error) {
    console.log("error :", error);
  }
};
github webiny / webiny-js / examples / apps / site / ssr / handler.js View on Github external
export const handler = async event => {
    const apolloClient = createClient();
    const context = {};

    const app = (
        
            
                
            
        
    );

    // Executes all graphql queries for the current state of application
    await getDataFromTree(app);
    const content = ReactDOMServer.renderToStaticMarkup(app);
    const state = apolloClient.extract();
    const helmet = Helmet.renderStatic();
    return injectContent(content, helmet, state);
};
github mimecuvalo / helloworld / server / app / app.js View on Github external
{!isApolloTraversal
              ? sheets.collect(
                  
                    {coreApp}
                  
                )
              : coreApp}
          
        
      
    
  );

  // This is so we can do `apolloClient.extract()` later on.
  try {
    await getDataFromTree(completeApp(true /* isApolloTraversal */));
  } catch (ex) {
    next(ex);
    return;
  }

  const renderedApp = renderToString(completeApp(false /* isApolloTraversal */));
  if (context.url) {
    res.redirect(301, context.url);
    return;
  }

  const materialUICSS = sheets.toString();
  const nonMaterialCSS = sheetsNonMaterialUI.toString();

  /*
    XXX(mime): Server-side rendering for CSS doesn't allow for inserting CSS the same way we do
github webiny / webiny-js / packages / cli / create / template / apps / site / ssr / ssr.js View on Github external
const context = {};

    const app = (
        
            
                
            
        
    );

    // Executes all graphql queries for the current state of application
    await getDataFromTree(app);
    const content = ReactDOMServer.renderToStaticMarkup(app);
    const state = apolloClient.extract();
    const helmet = Helmet.renderStatic();
    return { html: injectContent(content, helmet, state), state };
};

@apollo/react-ssr

React Apollo server-side rendering utilities

MIT
Latest version published 4 years ago

Package Health Score

64 / 100
Full package analysis