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

Docs: async rewrites examples are not async #77598

Open
masto opened this issue Mar 28, 2025 · 2 comments
Open

Docs: async rewrites examples are not async #77598

masto opened this issue Mar 28, 2025 · 2 comments

Comments

@masto
Copy link

masto commented Mar 28, 2025

What is the documentation issue?

Per

`rewrites` is an async function that expects to return either an array or an object of arrays (see below) holding objects with `source` and `destination` properties:
and the type definition (
rewrites?: () => Promise<
), rewrites is an async function. The examples, however, neither await anything, nor explicitly return a promise.

JavaScript of course wraps the return value in a promise, so this works, but it may be considered bad form. And some of us have our linters turned up to 11.

Is there any context that might help us understand?

I ran an automated installation script for PostHog, which added this configuration and produced an eslint failure, and that led me to noticing this in the Next.js documentation.

Image

I know that it's trivial to work around and I have done so, but I thought it might be worth considering changing the docs.

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/app/api-reference/config/next-config-js/rewrites

@snivels
Copy link

snivels commented Mar 29, 2025

In javascript async functions don't need to have an await inside them, like you've said the return value is implicitly wrapped in a promise.

I believe it's an async function because you might want to do something asynchronous before applying the rewrite e.g. getting a users locale from a cookie and rewriting them to the correct localised URL.

I do agree that perhaps an example code snippet using asynchronous behaviour inside the rewrite would reduce confusion, though.

@masto
Copy link
Author

masto commented Mar 29, 2025

Yeah, I'm assuming I am unusual for turning every linter rule up to 11 and enforcing zero warnings in my pre-commit hook (I have a greenfield project so I thought it would be nice to prevent any jank from creeping in). I just changed mine to

return Promise.resolve([...

but I don't know if doing that in the docs would actually confuse more people than it will help.

Anyway, definitely not making any demands or even expressing a strong opinion, just bringing this up in case the maintainers felt like taking it into account.

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

No branches or pull requests

2 participants