Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lrucache.load cannot read property 'length' of undefined #1

Closed
zwhitchcox opened this issue Mar 30, 2019 · 3 comments
Closed

lrucache.load cannot read property 'length' of undefined #1

zwhitchcox opened this issue Mar 30, 2019 · 3 comments

Comments

@zwhitchcox
Copy link

I'm getting the error:

index.js:238 Uncaught TypeError: Cannot read property 'length' of undefined
    at LRUCache.load (index.js:238)
    at loadCache (index.js:27)
    at Module../src/client.js (client.js:17)
    at __webpack_require__ (bootstrap:19)
    at Object.0 (client.js:34108)
    at __webpack_require__ (bootstrap:19)
    at bootstrap:83
    at bootstrap:83

I've basically just followed the instructions here

The relevant server code:

const axios = Axios.create({
  baseURL: 'http://localhost:3000'
})
const cache = new LRU({ max: 500 })
configure({axios, cache})
app.get('*', async (req, res) => {
  const markup = renderToString(
    <StaticRouter location={req.url}>
      <App />
    </StaticRouter>
  )
  const cache = await serializeCache()
  console.log(cache)
  
  res.end(`
  <!DOCTYPE html>
  <head>
    <title>Eggshell</title>
    <link type="text/css" rel="stylesheet" href="main.css">
    <meta name="robots" content="noindex, nofollow">
    <script src="/client.js" defer></script>
    <script>
    window.__AXIOS_HOOKS_CACHE__ = ${cache}
    </script>
  </head>
  <body>
    <div id="app">${markup}</div>
  </body>
  `)
})

The relevant client code:

import './style.css'
import {hydrate} from 'react-dom'
import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'

import { App } from './app'
import { loadCache, configure } from 'axios-hooks';
import Axios from 'axios'
import LRU from 'lru-cache'

const axios = Axios.create({
  baseURL: 'http://localhost:3000'
})
const cache = new LRU({ max: 500 })
configure({axios, cache})

loadCache(window.__AXIOS_HOOKS_CACHE__)
delete window.__AXIOS_HOOKS_CACHE__

hydrate(
    <Router>
        <App />
    </Router>
    , document.querySelector('#app')
)
@zwhitchcox
Copy link
Author

Nevermind, the problem was I forgot to serialize the cache

@zwhitchcox
Copy link
Author

This still doesn't render properly on the server side though. The data is loaded, but the app would need to be rerendered with the data loaded in order to do proper server rendering.

@simoneb
Copy link
Owner

simoneb commented Apr 1, 2019

@zwhitchcox did you check the full example here? it may give some clue as to why it's not working correctly in your setup

@simoneb simoneb closed this as completed Jun 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants