Skip to content

fix(dev-overrides): make assets and imageLoader work with basePath #876

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/real-otters-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix(dev-overrides): make assets and imageLoader work with basePath
10 changes: 9 additions & 1 deletion packages/open-next/src/overrides/imageLoader/fs-dev.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import fs from "node:fs";
import path from "node:path";

import { NextConfig } from "config/index";
import type { ImageLoader } from "types/overrides";
import { getMonorepoRelativePath } from "utils/normalize-path";

export default {
name: "fs-dev",
load: async (url: string) => {
const imagePath = path.join(getMonorepoRelativePath(), "assets", url);
const urlWithoutBasePath = NextConfig.basePath
? url.slice(NextConfig.basePath.length)
: url;
const imagePath = path.join(
getMonorepoRelativePath(),
"assets",
urlWithoutBasePath,
);
const body = fs.createReadStream(imagePath);
const contentType = url.endsWith(".png") ? "image/png" : "image/jpeg";
return {
Expand Down
9 changes: 7 additions & 2 deletions packages/open-next/src/overrides/wrappers/express-dev.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import path from "node:path";
import express from "express";

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

const wrapper: WrapperHandler = async (handler, converter) => {
const app = express();
// To serve static assets
app.use(express.static(path.join(getMonorepoRelativePath(), "assets")));
const basePath = NextConfig.basePath ?? "";
app.use(
basePath,
express.static(path.join(getMonorepoRelativePath(), "assets")),
);

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

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

app.all("/_next/image", async (req, res) => {
app.all(`${NextConfig.basePath ?? ""}/_next/image`, async (req, res) => {
const internalEvent = await converter.convertFrom(req);
const streamCreator: StreamCreator = {
writeHeaders: (prelude) => {
Expand Down
Loading