Skip to content

Commit a7a5218

Browse files
cache blogData to make site deployable to Cloudflare
1 parent 7139f2c commit a7a5218

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

apps/site/.cloudflare/node/fs.mjs

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fsPromises from 'fs/promises';
22

3+
import nodejsorgCacheManifest from '../.asset-manifests/.nodejsorg-cache.mjs';
34
import pagesManifest from '../.asset-manifests/pages.mjs';
45
import snippetsManifest from '../.asset-manifests/snippets.mjs';
56

@@ -28,18 +29,33 @@ export function readdir(path, options, cb) {
2829
}
2930

3031
function findInDirentLikes(path) {
31-
if (!path.startsWith('/pages') && !path.startsWith('/snippets')) {
32+
if (
33+
allowedPaths.every(
34+
allowedPath =>
35+
!path.startsWith(allowedPath) && !path.startsWith(`.${allowedPath}`)
36+
)
37+
) {
3238
return null;
3339
}
3440

41+
if (path.startsWith('./.next')) {
42+
// remove the leading `.`
43+
path = path.slice('./.next'.length);
44+
}
45+
3546
// remove the leading `/`
3647
path = path.slice(1);
3748

3849
const paths = path.split('/');
3950

4051
const manifestType = paths.shift();
4152

42-
const manifest = manifestType === 'pages' ? pagesManifest : snippetsManifest;
53+
const manifest =
54+
manifestType === '.nodejsorg-cache'
55+
? nodejsorgCacheManifest
56+
: manifestType === 'pages'
57+
? pagesManifest
58+
: snippetsManifest;
4359

4460
return recursivelyFindInDirentLikes(paths, manifest);
4561
function recursivelyFindInDirentLikes(paths, direntLikes) {
@@ -63,7 +79,12 @@ export function existsSync(path) {
6379
}
6480

6581
function existsImpl(path) {
66-
if (!path.startsWith('/pages') && !path.startsWith('/snippets')) {
82+
if (
83+
allowedPaths.every(
84+
allowedPath =>
85+
!path.startsWith(allowedPath) && !path.startsWith(`.${allowedPath}`)
86+
)
87+
) {
6788
return false;
6889
}
6990
return !!findInDirentLikes(path);
@@ -82,6 +103,8 @@ export function createReadStream(path) {
82103
return env.ASSETS.fetch(url);
83104
}
84105

106+
const allowedPaths = ['/pages', '/snippets', '/.next/.nodejsorg-cache'];
107+
85108
export default {
86109
readdir,
87110
exists,

apps/site/.cloudflare/prepare-build.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import nodePath from 'node:path';
55

66
await collectAndCopyDirToAssets('./pages');
77
await collectAndCopyDirToAssets('./snippets');
8+
await collectAndCopyDirToAssets('./.next/.nodejsorg-cache');
89

910
/**
1011
* @param {string} path

apps/site/next-data/generators/blogData.mjs

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict';
22

3-
import { createReadStream } from 'node:fs';
4-
import { basename, extname, join } from 'node:path';
3+
import {
4+
createReadStream,
5+
existsSync,
6+
mkdirSync,
7+
writeFileSync,
8+
} from 'node:fs';
9+
import { readFile } from 'node:fs/promises';
10+
import { basename, dirname, extname, join } from 'node:path';
511
import readline from 'node:readline';
612

713
import graymatter from 'gray-matter';
@@ -58,6 +64,18 @@ const getFrontMatter = (filename, source) => {
5864
* @return {Promise<import('../../types').BlogData>}
5965
*/
6066
const generateBlogData = async () => {
67+
// Note: we cache the result of this function in a json file, we do this so that the cached
68+
// version can be read instead of re processing all the files, this is necessary for the
69+
// site to be deployed on a Cloudflare worker (which can't cope with reading hundred
70+
// of files)
71+
const blogDataCacheFile = './.next/.nodejsorg-cache/blogData.json';
72+
if (existsSync(blogDataCacheFile)) {
73+
const rawContent = await readFile(blogDataCacheFile, 'utf8');
74+
const content = JSON.parse(rawContent);
75+
content.posts.forEach(p => (p.date = new Date(p.date)));
76+
return content;
77+
}
78+
6179
// We retrieve the full pathnames of all Blog Posts to read each file individually
6280
const filenames = await getMarkdownFiles(process.cwd(), 'pages/en/blog', [
6381
'**/index.md',
@@ -102,7 +120,10 @@ const generateBlogData = async () => {
102120
)
103121
);
104122

105-
return { categories: [...blogCategories], posts };
123+
const result = { categories: [...blogCategories], posts };
124+
mkdirSync(dirname(blogDataCacheFile), { recursive: true });
125+
writeFileSync(blogDataCacheFile, JSON.stringify(result));
126+
return result;
106127
};
107128

108129
export default generateBlogData;

0 commit comments

Comments
 (0)