Skip to content

How to cache results from API calls when building SSG pages? Traditional in-memory memoization doesn't seem to work #13765

Answered by lfades
Vadorequest asked this question in Help
Discussion options

You must be logged in to vote

We may need an example to show how to do this 🤔

You could create a build time only file that stores the data and read from that file instead of repeating the request, we do something like that for our docs. In our docs we fetch the latest tag of the repo to get the manifest of routes, and because that can happen once per documentation page, we cache the result in a file (build time only) to avoid being rate limited:

import path from 'path';
import { readFile, writeFile } from '../fs-utils';
import { GITHUB_API_URL, REPO_NAME } from './constants';

const USE_CACHE = process.env.USE_CACHE === 'true';
const TAG_CACHE_PATH = path.resolve('.github-latest-tag');

export async function getLatestTag

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
6 replies
@lfades
Comment options

@Vadorequest
Comment options

@lfades
Comment options

@Vadorequest
Comment options

@pip8786
Comment options

Answer selected by Vadorequest
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
4 participants