Skip to content

Commit 0cdf44b

Browse files
fix: add support for serving static assets in the public directory
See https://nextjs.org/docs/pages/building-your-application/optimizing/static-assets
1 parent 5503b7e commit 0cdf44b

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

packages/cloudflare/src/build/build-worker.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextjsAppPaths } from "../nextjs-paths";
22
import { build, Plugin } from "esbuild";
3-
import { readFileSync } from "node:fs";
3+
import { existsSync, readFileSync } from "node:fs";
44
import { cp, readFile, writeFile } from "node:fs/promises";
55

66
import { patchRequire } from "./patches/investigated/patch-require";
@@ -20,6 +20,7 @@ import { updateWebpackChunksFile } from "./patches/investigated/update-webpack-c
2020
* @param nextjsAppPaths
2121
*/
2222
export async function buildWorker(
23+
inputNextAppDir: string,
2324
outputDir: string,
2425
nextjsAppPaths: NextjsAppPaths,
2526
templateSrcDir: string
@@ -124,10 +125,19 @@ Request = globalThis.Request;
124125
await updateWorkerBundledCode(workerOutputFile, nextjsAppPaths);
125126

126127
console.log(`\x1b[35m⚙️ Copying asset files...\n\x1b[0m`);
128+
129+
// Copy over client-side generated files
127130
await cp(`${nextjsAppPaths.dotNextDir}/static`, `${outputDir}/assets/_next/static`, {
128131
recursive: true,
129132
});
130133

134+
// Copy over any static files (e.g. images) from the source project
135+
if (existsSync(`${inputNextAppDir}/public`)) {
136+
await cp(`${inputNextAppDir}/public`, `${outputDir}/assets`, {
137+
recursive: true,
138+
});
139+
}
140+
131141
console.log(`\x1b[35mWorker saved in \`${workerOutputFile}\` 🚀\n\x1b[0m`);
132142
}
133143

packages/cloudflare/src/build/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function build(inputNextAppDir: string, opts: BuildOptions): Promis
4343

4444
const templateDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "templates");
4545

46-
await buildWorker(outputDir, nextjsAppPaths, templateDir);
46+
await buildWorker(inputNextAppDir, outputDir, nextjsAppPaths, templateDir);
4747
}
4848

4949
type BuildOptions = {

0 commit comments

Comments
 (0)