GithubHelp home page GithubHelp logo

Comments (3)

pshrmn avatar pshrmn commented on May 13, 2024

What about setting the data in the HTML markup, like Redux?

https://redux.js.org/recipes/server-rendering#inject-initial-component-html-and-state

You could then have your resolve function check if the initial data exists and only fire off a request if it doesn't. The code below is pretty generic, but hopefully paints a clear picture.

I think that there are strategies for including components in the server rendered markup, but I think that including that isn't really worth the hassle.

function renderHandler(req, res) {
  const router = createRouter(reusable, routes, {
    history: { location: req.url }
  });
  router.once(({ response }) => {
    const Router = createRouterComponent(router);
    const markup = renderToString(
      <Router>
        <App />
      </Router>
    );
    const html = insertMarkup(markup, response);
    res.send(html);
  });
}

function insertMarkup(markup, response) {
  return `<!doctype html>
<html>
  <body>
    <div id="root">${markup}</div>
    <script src="/static/js/bundle.js"></script>
    <script>
      window.__INITIAL_DATA__ = ${JSON.stringify(response.data)};
    </script>
  </body>
</html>`;
}

from curi.

faergeek avatar faergeek commented on May 13, 2024

Yes, I thought of something like that. For example, I could inject initialData into external, then make some wrapper for resolvers which would check if data is already there, return it and then reset for subsequent transitions on client, so that other routes do not return other routes' data. I just hoped that it's possible to solve this problem at library level somehow. Not sure how though. Anyway, this probably needs to be added to server side rendering guide as a recipe. What do you think? I would try to write it once I figure out what to do myself))

from curi.

pshrmn avatar pshrmn commented on May 13, 2024

It would definitely be helpful to have this laid out for others.

I think that I would personally set up a local API that initializes itself with the data set by the server and caches new data after API results. I believe that data stores like Apollo will actually do this for you (if you've jumped on the GraphQL train), but you can also roll your own.

// api.js
const data = window.__INITIAL_DATA__
  ? window.__INITIAL_DATA__
  : {
      products: []
    };

const api = {
  product(id) {
    const cached = data.products.find(p => p.id === id);
    if (cached) {
      return Promise.resolve(cached);
    } else {
      return fetch(`/api/product/${id}`)
        .then(response => response.json())
        .then(result => {
          // cache result
          data.products.push(result);
          return result;
        });
    }
  }
}

export default api;
// routes.js
import api from "./api";

export default prepareRoutes([
  // ...
  {
    name: "Product",
    path: "product/:id",
    resolve({ params }) {
      return api.product(params.id);
    }
]);

from curi.

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.