Skip to content

Commit 5ca1d03

Browse files
committed
fix(dev-overrides): make assets and imageLoader work with basePath
1 parent ad701c2 commit 5ca1d03

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/open-next/src/overrides/imageLoader/fs-dev.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import fs from "node:fs";
22
import path from "node:path";
33

4+
import { NextConfig } from "config/index";
45
import type { ImageLoader } from "types/overrides";
56
import { getMonorepoRelativePath } from "utils/normalize-path";
67

78
export default {
89
name: "fs-dev",
910
load: async (url: string) => {
10-
const imagePath = path.join(getMonorepoRelativePath(), "assets", url);
11+
const urlWithoutBasePath = NextConfig.basePath
12+
? url.slice(NextConfig.basePath.length)
13+
: url;
14+
const imagePath = path.join(
15+
getMonorepoRelativePath(),
16+
"assets",
17+
urlWithoutBasePath,
18+
);
1119
const body = fs.createReadStream(imagePath);
1220
const contentType = url.endsWith(".png") ? "image/png" : "image/jpeg";
1321
return {

packages/open-next/src/overrides/wrappers/express-dev.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import path from "node:path";
22
import express from "express";
33

4+
import { NextConfig } from "config/index";
45
import type { StreamCreator } from "types/open-next.js";
56
import type { WrapperHandler } from "types/overrides.js";
67
import { getMonorepoRelativePath } from "utils/normalize-path";
78

89
const wrapper: WrapperHandler = async (handler, converter) => {
910
const app = express();
1011
// To serve static assets
11-
app.use(express.static(path.join(getMonorepoRelativePath(), "assets")));
12+
const basePath = NextConfig.basePath ?? "";
13+
app.use(
14+
basePath,
15+
express.static(path.join(getMonorepoRelativePath(), "assets")),
16+
);
1217

1318
const imageHandlerPath = path.join(
1419
getMonorepoRelativePath(),
@@ -17,7 +22,7 @@ const wrapper: WrapperHandler = async (handler, converter) => {
1722

1823
const imageHandler = await import(imageHandlerPath).then((m) => m.handler);
1924

20-
app.all("/_next/image", async (req, res) => {
25+
app.all(`${NextConfig.basePath ?? ""}/_next/image`, async (req, res) => {
2126
const internalEvent = await converter.convertFrom(req);
2227
const streamCreator: StreamCreator = {
2328
writeHeaders: (prelude) => {

0 commit comments

Comments
 (0)