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

[Bug] reloadClientData() does not reload site data #1093

Closed
Blobson opened this issue Apr 3, 2019 · 12 comments
Closed

[Bug] reloadClientData() does not reload site data #1093

Blobson opened this issue Apr 3, 2019 · 12 comments
Assignees
Labels

Comments

@Blobson
Copy link
Contributor

Blobson commented Apr 3, 2019

Short description
I am using site.yaml file to store site data and in development mode I want to hot-reload site data on every change in site.yaml.

Steps to reproduce the issue

  1. Create new project using react-static v7.0.5 using blank template.
  2. Add data/site.yaml with following contents to the project:
title: My Site!
  1. You will need to install node-watch and js-yaml for the next step:
$ yarn add node-watch js-yaml
  1. Add getSiteData and a watcher for data/site.yaml inside static.config.js like this:
import fs from 'fs'
import path from 'path'
import yaml from 'js-yaml'
import watch from 'node-watch'
import { reloadClientData } from 'react-static/node'

const dataDir = path.join(__dirname, 'data')

watch(dataDir, { recursive: true, filter: /\.(?:yaml|yml)$/ }, (evt, name) => {
  console.log(`Detected changes in '${name}',  reloading client data...`)
  reloadClientData()
})

export default {
  maxThreads: 1, // Remove this when you start doing any static generation
  getSiteData: () => {
    try {
      const rawSiteData = fs.readFileSync(path.join(dataDir, 'site.yaml'))
      const yamlSiteData = yaml.safeLoad(rawSiteData)
      console.log('SiteData: ', yamlSiteData)
      return yamlSiteData
    } catch(e) {
      console.error(e)
      return {}
    }
  }
}
  1. Add use of const { title } = useSiteData() to the src/App.js component like this:
import React, { Suspense } from 'react'
import { useSiteData } from 'react-static'

import './app.css'
import logo from './logo.png'

const Title = () => {
  const { title } = useSiteData()
  return (
    <h1>{title}</h1>
  )
}

const App = () => (
  <Suspense fallback="Loading...">
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <Title />
      </header>
    </div>
  </Suspense>
)

export default App
  1. Start development server
  2. Try editing title in data/site.yaml and you'll see that changes does not hot-reload.
    Also in the stdout of dev server you'll see that reloadClientData() is called but it does not trigger call for getSiteData().

Solution that worked for me
For now the only solution that worked for me is to add:

latestState = await fetchSiteData(latestState)

inside reloadClientData() inside runDevServer.js here.

The only downside is that now getSiteData() is called twice at the start of development server.

@tannerlinsley
Copy link
Contributor

The double call will be fixed in the patch version I'll release later today. Huzzah!

@happybeing
Copy link

happybeing commented Aug 11, 2019

Doesn't this fix also require the above mod to runDevServer.js:

latestState = await fetchSiteData(latestState)

EDIT: Actually even with that mod it isn't working for me with RS v7.1.0, but I'm just editing runDevServer.js. Maybe I need to rebuild something? Looking at the PR I think I may have misunderstood the fix so my change isn't necessary in theory. However, this is not working for me.

If I call reloadClientData() on a timer or when a content file changes it still doesn't work. getRoutes() is only called at the start and never called as a result of calling reloadClientData()

@Blobson does this still work for you in v7.1.0?
@SleeplessByte maybe needs re-opening?

@Blobson
Copy link
Contributor Author

Blobson commented Aug 11, 2019

It doesn't fully work and I think the reason is the fetchSiteData() call missing in runDevServer.js

@happybeing
Copy link

happybeing commented Aug 11, 2019 via email

@Blobson
Copy link
Contributor Author

Blobson commented Aug 11, 2019

Try editing version inside node_modules/react-static/...
Unfortunately, I can't check it myself because I am away from my PC.

@happybeing
Copy link

Still no joy with this in my node_modules RS:

  reloadClientData.current = async () => {
    latestState = await fetchSiteData(latestState)
    socket.emit('message', { type: 'reloadClientData' })
  }

@Blobson
Copy link
Contributor Author

Blobson commented Aug 11, 2019

As I remember Tenner had reworked the code a bit. I can take a closer look at it after I return from vacation on 18th of august.

@SleeplessByte
Copy link
Member

@theWebalyst certainly, although your issue might relate but not equate. After the maintenance update I'll actively work on React-Static including these issues.

@krishan711
Copy link
Contributor

Hey i'm running into the same problem now. I'd love to contribute and will start based on the above. If anyone has more insights im sure it would be helpful

@krishan711
Copy link
Contributor

Hey I made a PR to try and fix this by adding await buildDevRoutes into the reloadClientData call instead of just fetchSiteData. It seems to work locally, im trying to build a package to deploy now to test fully.

@krishan711
Copy link
Contributor

@SleeplessByte i believe this can be closed now? it works with my locally built version "@kibalabs/react-static": "7.2.3-kiba.2" if anyone else has the same problem before 7.3.0 is release 👍

@SleeplessByte
Copy link
Member

@krishan711 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants