Skip to content

Commit 4cbac67

Browse files
committed
Add an option for createStaticAssets to respect the basePath of Next.js
1 parent 73281c9 commit 4cbac67

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

packages/open-next/src/build/createAssets.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import fs from "node:fs";
22
import path from "node:path";
33

4+
import { loadConfig } from "config/util.js";
45
import logger from "../logger.js";
56
import type { TagCacheMetaFile } from "../types/cache.js";
67
import { isBinaryContentType } from "../utils/binary.js";
78
import * as buildHelper from "./helper.js";
89

9-
export function createStaticAssets(options: buildHelper.BuildOptions) {
10+
/**
11+
* Copy the static assets to the output folder
12+
*
13+
* @param options
14+
* @param useBasePath whether to adjust paths according to Next.js configure basePath
15+
*/
16+
export function createStaticAssets(
17+
options: buildHelper.BuildOptions,
18+
{ useBasePath = false } = {},
19+
) {
1020
logger.info("Bundling static assets...");
1121

1222
const { appBuildOutputPath, appPublicPath, outputDir, appPath } = options;
@@ -20,7 +30,7 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
2030
*
2131
* Copy over:
2232
* - .next/BUILD_ID => BUILD_ID
23-
* - .next/static => _next/static
33+
* - .next/static => _next/static or basePath/_next/static when useBasePath is true
2434
* - public/* => *
2535
* - app/favicon.ico or src/app/favicon.ico => favicon.ico
2636
*
@@ -30,9 +40,13 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
3040
path.join(appBuildOutputPath, ".next/BUILD_ID"),
3141
path.join(outputPath, "BUILD_ID"),
3242
);
43+
44+
const NextConfig = loadConfig(path.join(appBuildOutputPath, ".next"));
45+
const basePath = useBasePath ? (NextConfig.basePath ?? "") : "";
46+
3347
fs.cpSync(
3448
path.join(appBuildOutputPath, ".next/static"),
35-
path.join(outputPath, "_next", "static"),
49+
path.join(outputPath, basePath, "_next", "static"),
3650
{ recursive: true },
3751
);
3852
if (fs.existsSync(appPublicPath)) {

0 commit comments

Comments
 (0)