Skip to content

Add an option for createStaticAssets to respect the basePath of Next.js #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/open-next/src/build/createAssets.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
import fs from "node:fs";
import path from "node:path";

import { loadConfig } from "config/util.js";
import logger from "../logger.js";
import type { TagCacheMetaFile } from "../types/cache.js";
import { isBinaryContentType } from "../utils/binary.js";
import * as buildHelper from "./helper.js";

export function createStaticAssets(options: buildHelper.BuildOptions) {
/**
* Copy the static assets to the output folder
*
* WARNING: `useBasePath` should be set to `false` when the output file is used.
*
* @param options OpenNext build options
* @param useBasePath whether to copy files into the to Next.js configured basePath
*/
export function createStaticAssets(
options: buildHelper.BuildOptions,
{ useBasePath = false } = {},
) {
logger.info("Bundling static assets...");

const { appBuildOutputPath, appPublicPath, outputDir, appPath } = options;

const NextConfig = loadConfig(path.join(appBuildOutputPath, ".next"));
const basePath = useBasePath ? (NextConfig.basePath ?? "") : "";

// Create output folder
const outputPath = path.join(outputDir, "assets");
const outputPath = path.join(outputDir, "assets", basePath);
fs.mkdirSync(outputPath, { recursive: true });

/**
* Next.js outputs assets into multiple files. Copy into the same directory.
* Next.js outputs assets into multiple files.
*
* Copy into the same directory:
* - `.open-next/assets` when `useBasePath` is `false`
* - `.open-next/assets/basePath` when `useBasePath` is `true`
*
* Copy over:
* - .next/BUILD_ID => BUILD_ID
Expand All @@ -30,6 +49,7 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
path.join(appBuildOutputPath, ".next/BUILD_ID"),
path.join(outputPath, "BUILD_ID"),
);

fs.cpSync(
path.join(appBuildOutputPath, ".next/static"),
path.join(outputPath, "_next", "static"),
Expand Down