Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.
Open
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
13 changes: 10 additions & 3 deletions src/next-edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export interface CreateApiHandlerOptions {
* If you need to forward additional headers you can use this setting to define them.
*/
forwardAdditionalHeaders?: string[]

/*
* You can pass your next.config.js basePath here if your app lives on a subpath of the domain.
*/
appBasePath?: string
Comment on lines +120 to +123
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to instead load this from next itself directly? That way we can avoid another config parameter and it would just work with magic? :)

}

/**
Expand All @@ -134,6 +139,7 @@ export function createApiHandler(options: CreateApiHandlerOptions) {

const path = Array.isArray(paths) ? paths.join("/") : paths
const url = `${baseUrl}/${path}?${search.toString()}`
const appBasePath = options.appBasePath || ""

if (path === "ui/welcome") {
// A special for redirecting to the home page
Expand Down Expand Up @@ -175,14 +181,15 @@ export function createApiHandler(options: CreateApiHandlerOptions) {
if (res.headers.location.indexOf(baseUrl) === 0) {
res.headers.location = res.headers.location.replace(
baseUrl,
"/api/.ory",
appBasePath + "/api/.ory",
)
} else if (
res.headers.location.indexOf("/api/kratos/public/") === 0 ||
res.headers.location.indexOf("/self-service/") === 0 ||
res.headers.location.indexOf("/ui/") === 0
) {
res.headers.location = "/api/.ory" + res.headers.location
res.headers.location =
appBasePath + "/api/.ory" + res.headers.location
}
}

Expand Down Expand Up @@ -230,7 +237,7 @@ export function createApiHandler(options: CreateApiHandlerOptions) {
res.send(
buf
.toString("utf-8")
.replace(new RegExp(baseUrl, "g"), "/api/.ory"),
.replace(new RegExp(baseUrl, "g"), appBasePath + "/api/.ory"),
)
} else {
res.write(buf)
Expand Down