Skip to content

Commit 15eb0ee

Browse files
PeterswisJustinvicb
authored andcommitted
feat: integrate OpenNext static assets (#128)
- Rely on OpenNext to copy the static assets - Remove useless code in the worker - assets are served when path matches
1 parent bff1a6e commit 15eb0ee

File tree

3 files changed

+5
-26
lines changed

3 files changed

+5
-26
lines changed

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { existsSync, readFileSync } from "node:fs";
2-
import { cp, readFile, writeFile } from "node:fs/promises";
1+
import { readFileSync } from "node:fs";
2+
import { readFile, writeFile } from "node:fs/promises";
33
import { dirname, join } from "node:path";
44
import { fileURLToPath } from "node:url";
55

@@ -29,19 +29,6 @@ const packageDistDir = join(dirname(fileURLToPath(import.meta.url)), "..");
2929
* @param config
3030
*/
3131
export async function buildWorker(config: Config): Promise<void> {
32-
console.log(`\x1b[35m⚙️ Copying files...\n\x1b[0m`);
33-
34-
// Copy over client-side generated files
35-
await cp(join(config.paths.source.dotNext, "static"), join(config.paths.output.assets, "_next", "static"), {
36-
recursive: true,
37-
});
38-
39-
// Copy over any static files (e.g. images) from the source project
40-
const publicDir = join(config.paths.source.root, "public");
41-
if (existsSync(publicDir)) {
42-
await cp(publicDir, config.paths.output.assets, { recursive: true });
43-
}
44-
4532
// Copy over prerendered assets (e.g. SSG routes)
4633
copyPrerenderedRoutes(config);
4734

packages/cloudflare/src/cli/build/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { dirname, join } from "node:path";
44

55
import { buildNextjsApp, setStandaloneBuildMode } from "@opennextjs/aws/build/buildNextApp.js";
66
import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.js";
7+
import { createStaticAssets } from "@opennextjs/aws/build/createAssets.js";
78
import { createMiddleware } from "@opennextjs/aws/build/createMiddleware.js";
89
import * as buildHelper from "@opennextjs/aws/build/helper.js";
910
import { printHeader, showWarningOnWindows } from "@opennextjs/aws/build/utils.js";
@@ -64,6 +65,8 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
6465
// Compile middleware
6566
await createMiddleware(options, { forceOnlyBuildOnce: true });
6667

68+
createStaticAssets(options);
69+
6770
// Copy the .next directory to the output directory so it can be mutated.
6871
cpSync(join(projectOpts.sourceDir, ".next"), join(projectOpts.outputDir, ".next"), { recursive: true });
6972

packages/cloudflare/src/cli/templates/worker.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@ export default {
6464
}).getRequestHandler();
6565
}
6666

67-
const url = new URL(request.url);
68-
69-
if (url.pathname === "/_next/image") {
70-
const imageUrl =
71-
url.searchParams.get("url") ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg";
72-
if (imageUrl.startsWith("/")) {
73-
return env.ASSETS.fetch(new URL(imageUrl, request.url));
74-
}
75-
return fetch(imageUrl, { cf: { cacheEverything: true } });
76-
}
77-
7867
const { req, res, webResponse } = getWrappedStreams(request, ctx);
7968

8069
ctx.waitUntil(Promise.resolve(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res))));

0 commit comments

Comments
 (0)