Skip to content
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

basePath is does not works as expected with API routes #522

Open
socsieng opened this issue Sep 23, 2024 · 2 comments · May be fixed by #523
Open

basePath is does not works as expected with API routes #522

socsieng opened this issue Sep 23, 2024 · 2 comments · May be fixed by #523

Comments

@socsieng
Copy link

socsieng commented Sep 23, 2024

Issue

NextJS app deployed using SST and OpenNext does not respect basePath property defined in NextJS config for API routes.

I'd love to work on this but unfortunately I don't know where to start.

Potentially related to: #508

Environment

  • Platform: macOS Sequoia 15.0 (24A335)
  • NodeJS: 22.3.0
  • Package manager: pnpm 9.7.0
  • NextJS: 14.2.11
  • OpenNext: 3.1.3
  • SST: 3.1.11

Reproduction steps

Clone repository at https://github.com/socsieng/next-sst which includes detailed reproduction steps. Reproduction steps also included inline.

1. create-next-app and initialize SST

npx create-next-app@latest next-sst

cd next-sst
sst init

2. Update the next.config.mjs file to include the basePath

/** @type {import('next').NextConfig} */
const nextConfig = {
  basePath: '/base-path',
};

export default nextConfig;

3. Add an API route at app/api/hello/route.ts

import { NextRequest, NextResponse } from "next/server";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function GET(request: NextRequest) {
  return NextResponse.json({ message: "Hello world!" });
}

4. Use latest version of ope-next in sst.config.ts

3.1.3 at the time of writing.

/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
  app(input) {
    return {
      name: "next-sst",
      removal: input?.stage === "production" ? "retain" : "remove",
      home: "aws",
    };
  },
  async run() {
    new sst.aws.Nextjs("MyWeb", {
      openNextVersion: "3.1.3"
    });
  },
});

5. Deploy the app

sst deploy --stage api-test

6. Open the following URLs in the browser

  1. https://assignedhost.cloudfront.net/base-path
  2. https://assignedhost.cloudfront.net/base-path/api/hello

Replace assignedhost with the actual CloudFront distribution URL.

Expected results

Both 6.1 and 6.2 return successful responses.

Actual results

6.1 returns a successful response, but 6.2 returns a 404 error. Note that changing the URL for 6.2 to exclude the basePath (https://assignedhost.cloudfront.net/api/hello) works.

Note that this works as expected with sst dev:

  1. http://localhost:3000/base-path
  2. http://localhost:3000/base-path/api/hello
@conico974
Copy link
Collaborator

It should be a pretty easy fix, we probably just need to append the basePath if necessary here

const isApiRoute =
internalEvent.rawPath === "/api" ||
internalEvent.rawPath.startsWith("/api/");

@socsieng
Copy link
Author

@conico974, I assume no one is working on this. I'll raise a PR.

socsieng added a commit to socsieng/open-next that referenced this issue Sep 23, 2024
@socsieng socsieng linked a pull request Sep 23, 2024 that will close this issue
socsieng added a commit to socsieng/open-next that referenced this issue Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants