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

improve performance and generation of cache.json file #277

Closed
1 of 5 tasks
thescientist13 opened this issue Jan 17, 2020 · 2 comments
Closed
1 of 5 tasks

improve performance and generation of cache.json file #277

thescientist13 opened this issue Jan 17, 2020 · 2 comments
Assignees
Labels
chore unit testing, maintenance, etc CLI Content as Data good first issue Good for newcomers invalid This doesn't seem right
Milestone

Comments

@thescientist13
Copy link
Member

thescientist13 commented Jan 17, 2020

Type of Change

  • New Feature Request
  • Documentation / Website
  • Improvement / Suggestion
  • Bug
  • Other (please clarify below)

Summary

Technical debt tracking for some of the issue rough and dirty technical design decisions made as part of implementing #115 .

Details

Reading and merging / writing cache.json files in serialize.js is a little clunky. For instance, the about route has 4 sub pages, but only needs one cache.json for all of them.

pages.forEach((pagePath) => {
  const contents = fs.readFileSync(pagePath, 'utf-8');
  // TODO hardcoded root / depth - #273
  const pageRoot = pagePath.replace(publicDir, '').split('/')[1];
  const cacheRoot = pageRoot === 'index.html'
    ? ''
    : `${pageRoot}`;
  let cacheContents = {};
  
  // this only needs to be _once_ per page, e.g. about/, docs/
  glob.sync(`${publicDir}/${cacheRoot}/*-cache.json`).forEach((file) => {
    cacheContents = deepmerge(cacheContents, require(file));
  });

  // this (cache.json) also only needs to be done _once_ per page, e.g. about/, docs/
  fs.writeFileSync(`${publicDir}/${cacheRoot}/cache.json`, JSON.stringify(cacheContents));
  fs.writeFileSync(pagePath, contents.replace('___DATA___', JSON.stringify(cacheContents)));
});

In this case, this file would be written 4 times.

@thescientist13 thescientist13 added chore unit testing, maintenance, etc Content as Data labels Jan 17, 2020
@thescientist13 thescientist13 added this to the MVP milestone Jan 17, 2020
@thescientist13 thescientist13 self-assigned this Jan 17, 2020
@thescientist13 thescientist13 added CLI good first issue Good for newcomers labels Apr 22, 2020
@thescientist13
Copy link
Member Author

thescientist13 commented Apr 22, 2020

A naive way could be to simply check if cache.json exists already, and then just not write the file?

const targetFile = `${publicDir}/${cacheRoot}/cache.json`;

if (!fs.existsSync(targetFile) { 
  glob.sync(`${publicDir}/${cacheRoot}/*-cache.json`).forEach((file) => {
    cacheContents = deepmerge(cacheContents, require(file));
  });

  fs.writeFileSync(targetFile, JSON.stringify(cacheContents));
  fs.writeFileSync(pagePath, contents.replace('___DATA___', JSON.stringify(cacheContents)));
}

@thescientist13 thescientist13 added the invalid This doesn't seem right label Apr 11, 2021
@thescientist13
Copy link
Member Author

Not valid as per 0.10.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore unit testing, maintenance, etc CLI Content as Data good first issue Good for newcomers invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

1 participant