Skip to content

Commit 6b981ee

Browse files
authored
_Really_ don't inline dynamic imports (#946)
1 parent ef660f3 commit 6b981ee

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

.changeset/nervous-balloons-rule.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudflare/next-on-pages': patch
3+
---
4+
5+
chore: Extract module name in dynamic imports so that esbuild doesn't minify them

packages/next-on-pages/templates/_worker.js/utils/cache.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CacheAdaptor, IncrementalCacheValue } from '../../cache';
22
import { SUSPENSE_CACHE_URL } from '../../cache';
3+
import { doImport } from './doImport';
34

45
// https://github.com/vercel/next.js/blob/48a566bc/packages/next/src/server/lib/incremental-cache/fetch-cache.ts#L19
56
const CACHE_TAGS_HEADER = 'x-vercel-cache-tags';
@@ -118,7 +119,7 @@ async function getInternalCacheAdaptor(
118119
type: 'kv' | 'cache-api',
119120
): Promise<CacheAdaptor> {
120121
const moduleName = `./__next-on-pages-dist__/cache/${type}.js`;
121-
const adaptor = await import(moduleName);
122+
const adaptor = await doImport(moduleName);
122123
return new adaptor.default();
123124
}
124125

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Newer versions of esbuild try to resolve dynamic imports by crawling the filesystem for matching modules.
3+
* This doesn't work with the code next-on-pages generates, because the modules don't necessarily exist on the filesystem.
4+
* This forces esbuild _not_ to look for matching modules on the filesystem (because esbuild doesn't inline functions when minifying)
5+
*/
6+
export async function doImport(m: string) {
7+
return import(m);
8+
}

packages/next-on-pages/templates/_worker.js/utils/fetch.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { handleSuspenseCacheRequest } from './cache';
2+
import { doImport } from './doImport';
23

34
/**
45
* Patches the global fetch in ways necessary for Next.js (/next-on-pages) applications
@@ -50,7 +51,7 @@ async function handleInlineAssetRequest(request: Request) {
5051
try {
5152
const url = new URL(request.url);
5253
const moduleName = `./__next-on-pages-dist__/assets/${url.pathname}.bin`;
53-
const binaryContent = (await import(moduleName)).default;
54+
const binaryContent = (await doImport(moduleName)).default;
5455

5556
// Note: we can't generate a real Response object here because this fetch might be called
5657
// at the top level of a dynamically imported module, and such cases produce the following

0 commit comments

Comments
 (0)