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

Fix/Prevent erounus Next.js API route warning, on non-Next.js apps #11611

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Lissy93
Copy link

@Lissy93 Lissy93 commented May 18, 2024

Summary

This PR resolves an issue where developers receive misleading warnings about the placement of API files in non-Next.js projects hosted on Vercel.

The current logic triggers a warning when any file under the pages/ directory loosely starts with pages/api, which could include unrelated directories like pages/api-docs. This results in unnecessary confusion for developers not using Next.js for their project.

Changes

  • Refined API Route Detection Logic: I updated the if statement in hasNextApiFiles to strictly check for files within pages/api/ or src/pages/api/, and NOT file names which just happen to start with the word api-
  • Framework-Specific Warnings: I added a check to ensure that warnings related to Next.js API file placement are only shown if the project is indeed using Next.js. I just used options.projectSettings.framework to verify the framework type before issuing a warning.

Copy link

changeset-bot bot commented May 18, 2024

⚠️ No Changeset found

Latest commit: a343163

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Lissy93
Copy link
Author

Lissy93 commented May 18, 2024

I'm sorry for the direct PR, without any issue attached (issues are disabled for this repo). If it helps, and you'd like to create a ticket for this, here's content which can go into a bug report:


Summary

When using the Vercel CLI with an Astro app (without React or Next.js), any pages which start with api, such as src/pages/api-setup-guide.astro will throw the following warning

WARN! When using Next.js, it is recommended to place JavaScript Functions inside of the pages/api (provided by Next.js) directory instead of api (provided by Vercel). Other languages (Python, Go, etc) should still go in the api directory.
Learn More: https://nextjs.org/docs/api-routes/introduction

Steps to Reproduce

  1. npm create astro@latest
  2. npx astro add vercel
  3. touch src/pages/api-docs.astro
  4. vercel dev

Minimal reproducible example: https://stackblitz.com/edit/github-yogmzg-d6z7xm

Expected Behavior

No warnings. Because everything is 200 okay :)

Actual Behavior

User sees a barage of warnings from the Vercel CLI, informing them not to put Next.js API routes in Astro pages. This occours on a totally vanillar Astro app, with no traces of either React nor Next.js, just because they have an Astro route beginning with pages/api-XXX

image

Environment Info

Vercel CLI: 34.2.0
Node: v20.13.1
Astro: 4.8.6
@astrojs/vercel: 7.5.4
OS: Arch Linux, 2024.05.01

Areas Affected

Vercel CLI

Root Cause

This is caused by fileName.startsWith('pages/api') in

if (
!hasNextApiFiles &&
(fileName.startsWith('pages/api') || fileName.startsWith('src/pages/api'))
) {
hasNextApiFiles = true;
}

Which then results in an warning message being repeatidly displayed

if (
hasNextApiFiles &&
apiBuilders.some(b => isOfficialRuntime('node', b.use))
) {
warnings.push({
code: 'conflicting_files',
message:
'When using Next.js, it is recommended to place JavaScript Functions inside of the `pages/api` (provided by Next.js) directory instead of `api` (provided by Vercel). Other languages (Python, Go, etc) should still go in the `api` directory.',
link: 'https://nextjs.org/docs/api-routes/introduction',
action: 'Learn More',
});
}
}

This feature appears to have been added in #3784 (specifically, this commit specifically).

Solution

The solution is to either implement one of the following:
A) Only show this warning if running in a Next.js app
B) Only show this warning if the file is pages/api/xxx or pages/api (but ignore routes which just start with the word api, like pages/api-xxx)
C) Provide a method for users to disable such warnings


Hope that helps 💖

@Lissy93 Lissy93 changed the title Fix/prevent next api warn Fix/Prevent erounus Next.js API route warning, on non-Next.js apps May 19, 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 this pull request may close these issues.

None yet

1 participant