Skip to content

Commit 0cbbbce

Browse files
authored
ref: Reduce bundle size for Vercel functions (#14179)
1 parent c95cd42 commit 0cbbbce

File tree

3 files changed

+22
-66
lines changed

3 files changed

+22
-66
lines changed

next.config.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,38 @@ import {redirects} from './redirects.js';
55

66
const outputFileTracingExcludes = process.env.NEXT_PUBLIC_DEVELOPER_DOCS
77
? {
8-
'/**/*': ['./.git/**/*', './apps/**/*', 'docs/**/*'],
8+
'/**/*': [
9+
'**/*.map',
10+
'./.git/**/*',
11+
'./apps/**/*',
12+
'./.next/cache/mdx-bundler/**/*',
13+
'./.next/cache/md-exports/**/*',
14+
'docs/**/*',
15+
],
916
}
1017
: {
1118
'/**/*': [
19+
'**/*.map',
1220
'./.git/**/*',
21+
'./.next/cache/mdx-bundler/**/*',
22+
'./.next/cache/md-exports/**/*',
1323
'./apps/**/*',
1424
'develop-docs/**/*',
15-
'node_modules/@esbuild/darwin-arm64',
25+
'node_modules/@esbuild/*',
1626
],
17-
'/platform-redirect': ['**/*.gif', 'public/mdx-images/**/*', '*.pdf'],
27+
'/platform-redirect': ['**/*.gif', 'public/mdx-images/**/*', '**/*.pdf'],
1828
'\\[\\[\\.\\.\\.path\\]\\]': [
1929
'docs/**/*',
2030
'node_modules/prettier/plugins',
2131
'node_modules/rollup/dist',
2232
],
23-
'sitemap.xml': ['docs/**/*', 'public/mdx-images/**/*', '*.gif', '*.pdf', '*.png'],
33+
'sitemap.xml': [
34+
'docs/**/*',
35+
'public/mdx-images/**/*',
36+
'**/*.gif',
37+
'**/*.pdf',
38+
'**/*.png',
39+
],
2440
};
2541

2642
if (

scripts/generate-md-exports.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,7 @@ async function processTaskList({id, tasks, cacheDir, noCache}) {
283283
const fileHash = md5(data);
284284
if (r2Hash !== fileHash) {
285285
r2CacheMisses.push(relativePath);
286-
console.log(
287-
`📤 Worker[${id}]: Uploading ${relativePath} to R2, hash mismatch: ${r2Hash} !== ${fileHash}`
288-
);
286+
289287
await uploadToCFR2(s3Client, relativePath, data);
290288
}
291289
}
@@ -296,7 +294,7 @@ async function processTaskList({id, tasks, cacheDir, noCache}) {
296294
const success = tasks.length - failedTasks.length;
297295
if (r2CacheMisses.length / tasks.length > 0.1) {
298296
console.warn(
299-
`⚠️ Worker[${id}]: More than 10% of files had a different hash on R2, this might indicate a problem with the cache or the generation process.`
297+
`⚠️ Worker[${id}]: More than 10% of files had a different hash on R2 with the generation process.`
300298
);
301299
} else if (r2CacheMisses.length > 0) {
302300
console.log(

src/mdx.ts

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
import {BinaryLike, createHash} from 'crypto';
2-
31
import {cache} from 'react';
42
import matter from 'gray-matter';
53
import {s} from 'hastscript';
64
import yaml from 'js-yaml';
75
import {bundleMDX} from 'mdx-bundler';
8-
import {createReadStream, createWriteStream, mkdirSync} from 'node:fs';
96
import {access, opendir, readFile} from 'node:fs/promises';
107
import path from 'node:path';
11-
// @ts-expect-error ts(2305) -- For some reason "compose" is not recognized in the types
12-
import {compose, Readable} from 'node:stream';
13-
import {json} from 'node:stream/consumers';
14-
import {pipeline} from 'node:stream/promises';
15-
import {
16-
constants as zlibConstants,
17-
createBrotliCompress,
18-
createBrotliDecompress,
19-
} from 'node:zlib';
208
import {limitFunction} from 'p-limit';
219
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
2210
import rehypePresetMinify from 'rehype-preset-minify';
@@ -60,33 +48,6 @@ const root = process.cwd();
6048
// Functions which looks like AWS Lambda and we get `EMFILE` errors when trying to open
6149
// so many files at once.
6250
const FILE_CONCURRENCY_LIMIT = 200;
63-
const CACHE_COMPRESS_LEVEL = 4;
64-
const CACHE_DIR = path.join(root, '.next', 'cache', 'mdx-bundler');
65-
mkdirSync(CACHE_DIR, {recursive: true});
66-
67-
const md5 = (data: BinaryLike) => createHash('md5').update(data).digest('hex');
68-
69-
async function readCacheFile<T>(file: string): Promise<T> {
70-
const reader = createReadStream(file);
71-
const decompressor = createBrotliDecompress();
72-
73-
return (await json(compose(reader, decompressor))) as T;
74-
}
75-
76-
async function writeCacheFile(file: string, data: string) {
77-
await pipeline(
78-
Readable.from(data),
79-
createBrotliCompress({
80-
chunkSize: 32 * 1024,
81-
params: {
82-
[zlibConstants.BROTLI_PARAM_MODE]: zlibConstants.BROTLI_MODE_TEXT,
83-
[zlibConstants.BROTLI_PARAM_QUALITY]: CACHE_COMPRESS_LEVEL,
84-
[zlibConstants.BROTLI_PARAM_SIZE_HINT]: data.length,
85-
},
86-
}),
87-
createWriteStream(file)
88-
);
89-
}
9051

9152
function formatSlug(slug: string) {
9253
return slug.replace(/\.(mdx|md)/, '');
@@ -523,20 +484,6 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
523484
);
524485
}
525486

526-
const cacheKey = md5(source);
527-
const cacheFile = path.join(CACHE_DIR, cacheKey);
528-
529-
try {
530-
const cached = await readCacheFile<SlugFile>(cacheFile);
531-
return cached;
532-
} catch (err) {
533-
if (err.code !== 'ENOENT' && err.code !== 'ABORT_ERR') {
534-
// If cache is corrupted, ignore and proceed
535-
// eslint-disable-next-line no-console
536-
console.warn(`Failed to read MDX cache: ${cacheFile}`, err);
537-
}
538-
}
539-
540487
process.env.ESBUILD_BINARY_PATH = path.join(
541488
root,
542489
'node_modules',
@@ -662,11 +609,6 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
662609
},
663610
};
664611

665-
writeCacheFile(cacheFile, JSON.stringify(resultObj)).catch(e => {
666-
// eslint-disable-next-line no-console
667-
console.warn(`Failed to write MDX cache: ${cacheFile}`, e);
668-
});
669-
670612
return resultObj;
671613
}
672614

0 commit comments

Comments
 (0)