gatsby#GatsbyCache TypeScript Examples

The following examples show how to use gatsby#GatsbyCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: cache.ts    From gatsby-project-kb with MIT License 6 votes vote down vote up
getAllCachedNodes = async (cache: GatsbyCache, getNode: Function) => {
  const dir = cacheDirectory(cache)
  const files = await fs.promises.readdir(dir)

  return (
    await Promise.all(
      files.map((f) => {
        if (f === inboundFile) {
          return
        }
        const id = decodeURIComponent(f.replace(/\.json$/, ''))
        return getCachedNode(cache, id, getNode)
      })
    )
  ).filter(nonNullable)
}
Example #2
Source File: cache.ts    From gatsby-project-kb with MIT License 6 votes vote down vote up
setCachedNode = (cache: GatsbyCache, id: string, data: CachedNode) => {
  return fs.promises.writeFile(
    path.join(cacheDirectory(cache), `${encodeURIComponent(id)}.json`),
    JSON.stringify({
      outboundReferences: data.outboundReferences,
      title: data.title,
      aliases: data.aliases,
      resolvedOutboundReferences: data.resolvedOutboundReferences,
    })
  )
}
Example #3
Source File: cache.ts    From gatsby-project-kb with MIT License 6 votes vote down vote up
getCachedNode = async (
  cache: GatsbyCache,
  id: string,
  getNode: Function
): Promise<CachedNode | undefined> => {
  const node = getNode(id)

  if (!node) {
    try {
      // clean up the cache if we have some file that aren't node
      await fs.promises.unlink(
        path.join(cacheDirectory(cache), `${encodeURIComponent(id)}.json`)
      )
    } catch (err) {}
    return undefined
  }

  try {
    const data = JSON.parse(
      await fs.promises.readFile(
        path.join(cacheDirectory(cache), `${encodeURIComponent(id)}.json`),
        'utf8'
      )
    )

    return { node, ...data }
  } catch (err) {
    return undefined
  }
}
Example #4
Source File: compute-inbounds.ts    From gatsby-project-kb with MIT License 4 votes vote down vote up
export async function generateData(cache: GatsbyCache, getNode: Function) {
  if (currentGeneration) {
    return currentGeneration
  }

  currentGeneration = Promise.resolve().then(async () => {
    const nodes = await getAllCachedNodes(cache, getNode)
    const inboundReferences: InboundReferences = {}

    let tree: ContentTree
    let cachedNodeToContentNodeMap: Map<CachedNode, ContentNode>
    const contextTreeRepr = await cache.get(CONTEXT_TREE_CACHE_KEY)
    if (contextTreeRepr) {
      tree = ContentTree.fromRepr(contextTreeRepr)

      cachedNodeToContentNodeMap = new Map<CachedNode, ContentNode>()
      nodes.forEach((cachedNode) => {
        const filePath = getFilePathFromCachedNode(cachedNode)
        if (!filePath) return
        const contentNode = tree.getNode(filePath)
        if (contentNode) {
          cachedNodeToContentNodeMap.set(cachedNode, contentNode)
        }
      })
    }

    function getRefNode(ref: Reference) {
      const title = ref.target
      if (!title) return
      let node = nodes.find((x) => {
        if (x.title === title || x.aliases.some((alias) => alias === title)) {
          return true
        }

        const filePath = getFilePathFromCachedNode(x)
        if (filePath) {
          if (tree) {
            if (cachedNodeToContentNodeMap.has(x)) {
              const contentNodeByTitle = tree.getNode(title)
              if (contentNodeByTitle === cachedNodeToContentNodeMap.get(x)) {
                return true
              }
            }
          } else {
            return basename(filePath, extname(filePath)) === title
          }
        }
      })
      return node
    }

    await Promise.all(
      nodes
        .map((node) => {
          const mapped = node.outboundReferences.pages
            .map((reference) => {
              const cachedNode = getRefNode(reference)
              if (!cachedNode) return null
              return {
                contextLine: reference.contextLine,
                target: cachedNode.node,
                referrer: reference.referrerNode,
                refWord: reference.target,
                targetAnchor: reference.targetAnchor,
              } as NodeReference
            })
            .filter(nonNullable)

          mapped.forEach((item) => {
            const nodeId = item.target.id
            if (!inboundReferences[nodeId]) {
              inboundReferences[nodeId] = []
            }
            inboundReferences[nodeId].push({
              contextLine: item.contextLine,
              target: item.target,
              referrer: node.node,
              refWord: item.refWord,
            })
          })

          return {
            ...node,
            resolvedOutboundReferences: mapped,
          }
        })
        .map((data) => setCachedNode(cache, data.node.id, data))
    )

    Object.keys(inboundReferences).forEach((nodeId) => {
      inboundReferences[nodeId] = inboundReferences[nodeId].filter(
        (reference) =>
          getNode(reference.target.parent) &&
          !hasChildInArrayExcept(
            getNode(reference.target.parent),
            inboundReferences[nodeId].map((o) => o.target),
            reference.target.id,
            getNode as any
          )
      )
    })

    await setInboundReferences(cache, inboundReferences)

    currentGeneration = undefined

    return true
  })

  return currentGeneration
}