Giter Site home page Giter Site logo

Comments (12)

stereobooster avatar stereobooster commented on May 16, 2024 3

Heh it is actually possible to rehydrate Apollo store, so you can do something like this. (Not an exact code)

// Grab the state from a global variable injected into the server-generated HTML
const preloadedState = window.__APOLLO_STORE__

// Allow the passed state to be garbage-collected
delete window.__APOLLO_STORE__

const client = new ApolloClient({
  initialState: preloadedState,
});

// Tell react-snap how to save state
window.snapSaveState = () => ({
  "__APOLLO_STORE__": client.store.getState()
});

from react-snap.

stereobooster avatar stereobooster commented on May 16, 2024

There is a real browser behind the scene. If this thing works in a browser it will work in react-snap. The only question is how you gonna rehydrate the generated code.

from react-snap.

jidan70 avatar jidan70 commented on May 16, 2024

Hi @stereobooster, but is there a way to increase the time react-snap crawls your page? Because i'm using react-apollo but react-snap renders the page before all my request with react-apollo is done.

from react-snap.

stereobooster avatar stereobooster commented on May 16, 2024

Do you have example application that I can test?

There is a waitFor option the same as puppeteer. But obviously this is a hack and there should be a better way to do this, like geelen/react-snapshot#30

from react-snap.

jidan70 avatar jidan70 commented on May 16, 2024

Ok thanks, i will create a small example. So is this better solution to be sure that everything we need has been rendered in the react app?

from react-snap.

stereobooster avatar stereobooster commented on May 16, 2024

There is no better solution so far

from react-snap.

jidan70 avatar jidan70 commented on May 16, 2024

Hi @stereobooster thank you very much, i'll look at this

from react-snap.

stereobooster avatar stereobooster commented on May 16, 2024

Will close this for now. Feel free to reopen

from react-snap.

zomars avatar zomars commented on May 16, 2024

@jidan70 would you kindly share your working example?

from react-snap.

zomars avatar zomars commented on May 16, 2024

I figured it out, I'll share it here for reference:

// index.js
import React from 'react';
import { hydrate, render } from 'react-dom';
import { ApolloClient } from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloProvider } from 'react-apollo';
import { loadComponents, getState } from 'loadable-components';
import App, { query } from './App';
import './app.css';

// Grab the state from a global variable injected into the server-generated HTML
const preloadedState = window.__APOLLO_STORE__;

// Allow the passed state to be garbage-collected
delete window.__APOLLO_STORE__;

const client = new ApolloClient({
  initialState: preloadedState,
});

// Tell react-snap how to save state
window.snapSaveState = () => ({
  __APOLLO_STORE__: client.readQuery({
    query, // you can pass any query you want to prerender here
  }),
});

const query = gql`
  query ReadTodo {
    todo(id: 5) {
      id
      text
      completed
    }
  }
`;

const rootElement = document.getElementById('root');

const AppWithRouter = (
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>
);

loadComponents()
  .then(() => hydrate(AppWithRouter, rootElement))
  .catch(() => render(AppWithRouter, rootElement));

from react-snap.

vishnup95 avatar vishnup95 commented on May 16, 2024

@zomars Where did you add this to?

from react-snap.

zomars avatar zomars commented on May 16, 2024

@vishnup95 I've updated my previous comment for more clarity. But, IDK if this still working TBH. I've moved into other projects.

from react-snap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.