Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions packages/open-next/src/build/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ export async function createMiddleware(options: buildHelper.BuildOptions) {
return { useMiddleware: false };
}

// Create output folder
let outputPath = path.join(outputDir, "server-function");

const commonMiddlewareOptions = {
middlewareInfo: entry,
options,
appBuildOutputPath,
};

if (config.middleware?.external) {
outputPath = path.join(outputDir, "middleware");
const outputPath = path.join(outputDir, "middleware");
fs.mkdirSync(outputPath, { recursive: true });

// Copy open-next.config.mjs
Expand Down
18 changes: 7 additions & 11 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";

import type {
FunctionOptions,
OpenNextConfig,
SplittedFunctionOptions,
} from "types/open-next";
import type { FunctionOptions, SplittedFunctionOptions } from "types/open-next";

import logger from "../logger.js";
import { minifyAll } from "../minimize-js.js";
Expand Down Expand Up @@ -40,9 +36,9 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
const routes = fnOptions.routes;
routes.forEach((route) => foundRoutes.add(route));
if (fnOptions.runtime === "edge") {
await generateEdgeBundle(name, config, options, fnOptions);
await generateEdgeBundle(name, options, fnOptions);
} else {
await generateBundle(name, config, options, fnOptions);
await generateBundle(name, options, fnOptions);
}
});

Expand All @@ -53,7 +49,7 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {

const remainingRoutes = new Set<string>();

const { monorepoRoot, appBuildOutputPath } = options;
const { appBuildOutputPath, monorepoRoot } = options;

const packagePath = path.relative(monorepoRoot, appBuildOutputPath);

Expand Down Expand Up @@ -104,7 +100,7 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
}

// Generate default function
await generateBundle("default", config, options, {
await generateBundle("default", options, {
...defaultFn,
// @ts-expect-error - Those string are RouteTemplate
routes: Array.from(remainingRoutes),
Expand All @@ -114,11 +110,11 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {

async function generateBundle(
name: string,
config: OpenNextConfig,
options: buildHelper.BuildOptions,
fnOptions: SplittedFunctionOptions,
) {
const { appPath, appBuildOutputPath, outputDir, monorepoRoot } = options;
const { appPath, appBuildOutputPath, config, outputDir, monorepoRoot } =
options;
logger.info(`Building server function: ${name}...`);

// Create output folder
Expand Down
9 changes: 2 additions & 7 deletions packages/open-next/src/build/edge/createEdgeBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import path from "path";
import type { MiddlewareInfo, MiddlewareManifest } from "types/next-types";
import type {
IncludedConverter,
OpenNextConfig,
OverrideOptions,
RouteTemplate,
SplittedFunctionOptions,
Expand All @@ -20,7 +19,6 @@ import type { BuildOptions } from "../helper.js";
import { copyOpenNextConfig, esbuildAsync } from "../helper.js";

interface BuildEdgeBundleOptions {
appBuildOutputPath: string;
middlewareInfo: MiddlewareInfo;
entrypoint: string;
outfile: string;
Expand All @@ -34,7 +32,6 @@ interface BuildEdgeBundleOptions {
}

export async function buildEdgeBundle({
appBuildOutputPath,
middlewareInfo,
entrypoint,
outfile,
Expand Down Expand Up @@ -95,7 +92,7 @@ export async function buildEdgeBundle({
}),
openNextEdgePlugins({
middlewareInfo,
nextDir: path.join(appBuildOutputPath, ".next"),
nextDir: path.join(options.appBuildOutputPath, ".next"),
edgeFunctionHandlerPath: path.join(
options.openNextDistDir,
"core",
Expand Down Expand Up @@ -165,7 +162,6 @@ export function copyMiddlewareAssetsAndWasm({}) {}

export async function generateEdgeBundle(
name: string,
config: OpenNextConfig,
options: BuildOptions,
fnOptions: SplittedFunctionOptions,
) {
Expand Down Expand Up @@ -218,7 +214,6 @@ export async function generateEdgeBundle(
}

await buildEdgeBundle({
appBuildOutputPath,
middlewareInfo: fn,
entrypoint: path.join(
options.openNextDistDir,
Expand All @@ -228,6 +223,6 @@ export async function generateEdgeBundle(
outfile: path.join(outputPath, "index.mjs"),
options,
overrides: fnOptions.override,
additionalExternals: config.edgeExternals,
additionalExternals: options.config.edgeExternals,
});
}
5 changes: 3 additions & 2 deletions packages/open-next/src/core/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,9 @@ export function fixDataPage(
}

if (rawPath.startsWith(dataPattern) && rawPath.endsWith(".json")) {
let newPath = rawPath.replace(dataPattern, "").replace(/\.json$/, "");
newPath = newPath === "/index" ? "/" : newPath;
const newPath = rawPath
.slice(dataPattern.length, -".json".length)
.replace(/^\/index$/, "/");
query.__nextDataReq = "1";

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/open-next/src/core/routingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function applyMiddlewareHeaders(
export default async function routingHandler(
event: InternalEvent,
): Promise<InternalResult | MiddlewareOutputEvent> {
const nextHeaders = addNextConfigHeaders(event, ConfigHeaders) ?? {};
const nextHeaders = addNextConfigHeaders(event, ConfigHeaders);

let internalEvent = fixDataPage(event, BuildId);
if ("statusCode" in internalEvent) {
Expand Down
2 changes: 1 addition & 1 deletion packages/open-next/src/plugins/replacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const importPattern = /\/\/#import([\s\S]*?)\n\/\/#endImport/gm;
* deletes: ["id1"],
* })
*
* To inject arbritary code by using (import at top of file):
* To inject arbitrary code by using (import at top of file):
*
* //#import
*
Expand Down