question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any method from cache to return cache data to client ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
marcin-pielacommented, Apr 25, 2019

There is method for that:

client.cache.get(action)

And it’s working on server side for our simple cache provider.

Example app for next.js framework (responses are cached for 10s on server side):

next.config.js:

const withTM = require('next-transpile-modules')

module.exports = withTM({
  transpileModules: ['react-fetching-library']
});

client.js

import { createClient, createCache } from "react-fetching-library";

const cache = createCache(
  (action) => {
    return action.method === 'GET';
  },
  (response) => {
    return new Date().getTime() - response.timestamp < 10000;
  },
);

const client = createClient({
  cacheProvider: cache,
});

export default client;

pages/_app.js

import App, { Container } from 'next/app';
import 'isomorphic-unfetch';

import { ClientContextProvider } from "react-fetching-library";
import client from '../client';

class MyApp extends App {
  render () {
    const { Component, pageProps } = this.props

    return (
      <Container>
        <ClientContextProvider client={client}>
          <Component {...pageProps} />
        </ClientContextProvider>
      </Container>
    )
  }
}

export default MyApp

pages/index.js

import React from "react";
import client from "../client";

const action = {
  method: "GET",
  endpoint: "https://private-34f3a-reactapiclient.apiary-mock.com/users"
};

const Users = ({ loading, payload, error }) => {
  return (
    <div>
      {loading && <span>Loading</span>}

      {error && <span>Error</span>}

      {payload && <span>{payload.length}</span>}
    </div>
  );
};

Users.getInitialProps = async ({ req }) => {
  // fetch
  const res = await client.query(action);

  // get data from cache
  console.log(client.cache.get(action));

  return res;
}

export default Users;
1reaction
ntuckercommented, Apr 25, 2019

That would be pretty easy if https://github.com/marcin-piela/react-fetching-library/issues/13 happens. Then you can pull the state directly from the redux store. You won’t have to use redux in the browser as you could have a split point based on if it was running server-side.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deep dive into SSG, SSR and how a CDN and HTTP caching ...
Deep dive into SSG, SSR and how a CDN and HTTP caching can increase your performance by 10x. First, we dive into the...
Read more >
Server Side Rendering Cache | Vue Storefront 2
Caching SSR output in Vue Storefront requires two packages: @vue-storefront/cache - Nuxt.js module, that does the heavy lifting.
Read more >
Make your SSR site Lightning Fast with Redis Cache - Fjolt
Caching your SSR (Server Side Rendered) site with Redis can make it up to 95% faster. Let's look at how to do it...
Read more >
Server-Side Rendering Performance: Overview
In this article, we'll show you how to build the fastest-possible SSR PWA by making sure that most pages are cached through the...
Read more >
Caching - Vue SSR Guide
Page-level Caching. A server-rendered app in most cases relies on external data, so the content is dynamic by nature and cannot be cached...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found