Skip to content

Commit

Permalink
fix(cache): remove cache rules from fetch() (nodejs#7270)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviv Keller authored Nov 21, 2024
1 parent 881afd4 commit fd3c538
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
6 changes: 2 additions & 4 deletions apps/site/app/[locale]/next-data/api-data/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ export const GET = async () => {

const gitHubApiResponse = await fetch(
getGitHubApiDocsUrl(versionWithPrefix),
{ ...authorizationHeaders, cache: 'force-cache' }
authorizationHeaders
);

return gitHubApiResponse.json().then((apiDocsFiles: Array<GitHubApiFile>) => {
// maps over each api file and get the download_url, fetch the content and deflates it
const mappedApiFiles = apiDocsFiles.map(
async ({ name, path: filename, download_url }) => {
const apiFileResponse = await fetch(download_url, {
cache: 'force-cache',
});
const apiFileResponse = await fetch(download_url);

// Retrieves the content as a raw text string
const source = await apiFileResponse.text();
Expand Down
7 changes: 3 additions & 4 deletions apps/site/next-data/blogData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ const getBlogData = (cat: string, page?: number): Promise<BlogPostsRSC> => {

const fetchURL = `${NEXT_DATA_URL}blog-data/${cat}/${page ?? 0}`;

// When we're on RSC with Server capabilities we prefer using Next.js Data Fetching
// as this will load cached data from the server instead of generating data on the fly
// this is extremely useful for ISR and SSG as it will not generate this data on every request
return fetch(fetchURL, { cache: 'force-cache' })
// This data cannot be cached because it is continuously updated. Caching it would lead to
// outdated information being shown to the user.
return fetch(fetchURL)
.then(response => response.text())
.then(response => parseBlogDataResponse(response));
};
Expand Down
7 changes: 3 additions & 4 deletions apps/site/next-data/releaseData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ const getReleaseData = (): Promise<Array<NodeRelease>> => {

const fetchURL = `${NEXT_DATA_URL}release-data`;

// When we're on RSC with Server capabilities we prefer using Next.js Data Fetching
// as this will load cached data from the server instead of generating data on the fly
// this is extremely useful for ISR and SSG as it will not generate this data on every request
// This data cannot be cached because it is continuously updated. Caching it would lead to
// outdated information being shown to the user.
// Note: We do manual JSON.parse after response.text() to prevent React from throwing an Error
// that does not provide a clear stack trace of which request is failing and what the JSON.parse error is
return fetch(fetchURL, { cache: 'force-cache' })
return fetch(fetchURL)
.then(response => response.text())
.then(JSON.parse);
};
Expand Down
2 changes: 1 addition & 1 deletion apps/site/next.calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getCalendarEvents = async (calendarId = '', maxResults = 20) => {
calendarQueryUrl.searchParams.append(key, value)
);

return fetch(calendarQueryUrl, { cache: 'force-cache' })
return fetch(calendarQueryUrl)
.then(response => response.json())
.then(calendar => calendar.items ?? []);
};

0 comments on commit fd3c538

Please sign in to comment.