Skip to content

Commit 46a2c56

Browse files
reimplement getMarkdownFiles in next.helpers.mjs to be generated at build time
1 parent fbd5772 commit 46a2c56

10 files changed

Lines changed: 40 additions & 1178 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ dist/
3636
# Ignore worker artifacts
3737
apps/site/.open-next
3838
apps/site/.wrangler
39+
40+
.next.helpers.mjs

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { files } from '../../next.helpers.mjs';
1+
import { files } from '../../.next.helpers.mjs';
22

33
export function readdir(params, cb) {
44
console.log('fs#readdir', params);

apps/site/CLOUDFLARE.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ You can monitor and configure the project at https://dash.cloudflare.com/fb4a2d0
7272
The following is an incomplete list of tasks and problems that still need to be resolved:
7373

7474
- [ ] sort out issues with `eval` and MDX and undo edits in `./app/[locale]/[[...path]]/page.tsx`
75-
- [ ] reimplement `getMarkdownFiles` in `next.helpers.mjs` to be generated at build time
76-
- this can be accomplished either via a npm/turbo prebuild task, or possibly as part of next.js SSG/staticProps (but we need to ensure that we don't end up accidentaly downloading this big file to the client as part of hydration)
75+
- [x] reimplement `getMarkdownFiles` in `next.helpers.mjs` to be generated at build time
76+
- this can be accomplished either via a npm/turbo prebuild task, or possibly as part of next.js SSG/staticProps but
77+
- [ ] we need to ensure that we don't end up accidentally downloading this big file to the client as part of hydration
7778
- [ ] once we have easy access to the list of files, we should roll back changes to `next-data/providers/blogData.ts`
7879
- [ ] back out most changes from `next.dynamic.mjs`
7980
- [ ] instead of using runtime detection via `globalThis.navigator?.userAgent`, we should instead use `alias` feature in `wrangler.toml` to override the implementation of `node:fs` calls but only when running in workerd as we need the build to keep on running in node.js for SSG to work

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import readline from 'node:readline';
66

77
import graymatter from 'gray-matter';
88

9-
import { getMarkdownFiles } from '../../next.helpers.mjs';
9+
import { getMarkdownFiles } from '../../.next.helpers.mjs';
1010

1111
// gets the current blog path based on local module path
1212
const blogPath = join(process.cwd(), 'pages/en/blog');

apps/site/next.dynamic.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import { VFile } from 'vfile';
1010

1111
import { readFile as runtimeReadFile } from './.cloudflare/node/fs/promises.mjs';
1212
import { exists as runtimeExists } from './.cloudflare/node/fs.mjs';
13+
import { getMarkdownFiles } from './.next.helpers.mjs';
1314
import { BASE_URL, BASE_PATH, IS_DEVELOPMENT } from './next.constants.mjs';
1415
import {
1516
IGNORED_ROUTES,
1617
DYNAMIC_ROUTES,
1718
PAGE_METADATA,
1819
} from './next.dynamic.constants.mjs';
19-
import { getMarkdownFiles } from './next.helpers.mjs';
2020
import { siteConfig } from './next.json.mjs';
2121
import { availableLocaleCodes, defaultLocale } from './next.locales.mjs';
2222
import { compileMDX } from './next.mdx.compiler.mjs';

apps/site/next.helpers.mjs

Lines changed: 1 addition & 1171 deletions
Large diffs are not rendered by default.

apps/site/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"scripts:release-post": "cross-env NODE_NO_WARNINGS=1 node scripts/release-post/index.mjs",
2020
"dev": "cross-env NODE_NO_WARNINGS=1 next dev --turbo",
2121
"serve": "npm run dev",
22+
"prebuild": "node scripts/build.helpers.mjs",
2223
"build": "cross-env NODE_NO_WARNINGS=1 next build",
2324
"start": "cross-env NODE_NO_WARNINGS=1 next start",
2425
"deploy": "cross-env NEXT_PUBLIC_STATIC_EXPORT=true npm run build",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
import { readFileSync, writeFileSync } from 'node:fs';
4+
import { resolve } from 'node:path';
5+
6+
import { glob } from 'glob';
7+
8+
import { getRelativePath } from '../next.helpers.mjs';
9+
10+
const __dirname = getRelativePath(import.meta.url);
11+
const nextHelpersMjs = readFileSync(
12+
resolve(__dirname, '..', 'next.helpers.mjs'),
13+
'utf8'
14+
);
15+
16+
const files = await glob('**/*.{md,mdx}', { root: 'pages' });
17+
18+
const outputNextHelpersMjs = nextHelpersMjs.replace(
19+
/export const files = \[\s*\/\* generated at build time \*\/\s*\];/,
20+
`export const files = [\n${files
21+
.map(file => ` ${JSON.stringify(file)},\n`)
22+
.join('')}];`
23+
);
24+
25+
writeFileSync(
26+
resolve(__dirname, '..', '.next.helpers.mjs'),
27+
outputNextHelpersMjs
28+
);

apps/site/scripts/orama-search/get-documents.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import zlib from 'node:zlib';
44

55
import { slug } from 'github-slugger';
66

7-
import { getRelativePath } from '../../next.helpers.mjs';
7+
import { getRelativePath } from '../../.next.helpers.mjs';
88

99
const currentRoot = getRelativePath(import.meta.url);
1010
const dataBasePath = join(currentRoot, '../../.next/server/app/en/next-data');

apps/site/scripts/release-post/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { format } from 'prettier';
2929

3030
import { downloadsTable } from './downloadsTable.mjs';
3131
import prettierConfig from '../../../../.prettierrc.json' with { type: 'json' };
32-
import { getRelativePath } from '../../next.helpers.mjs';
32+
import { getRelativePath } from '../../.next.helpers.mjs';
3333

3434
const URLS = {
3535
NODE_DIST_JSON: 'https://nodejs.org/dist/index.json',

0 commit comments

Comments
 (0)