Next.js Discord

Discord Forum

Error with searchParams in generateMetadata

Answered
Naeemgg posted this in #help-forum
Open in Discord
I'm just trying to get searchParams inside generateMetadata function and return the object but its throwing this error
import type { Metadata } from 'next'

interface Props {
  searchParams: {
    id?: string
    title?: string
    message?: string
  }
}

export async function generateMetadata({ searchParams }: Props): Promise<Metadata> {
  const {  title, message } = searchParams
  const metadata: Metadata = {
    title: title,
    description: message
  }

  return metadata
}

.next/types/app/count/page.ts:38:31
Type error: Type 'Props' does not satisfy the constraint 'PageProps'.
  Types of property 'searchParams' are incompatible.
    Type '{ id?: string | undefined; title?: string | undefined; message?: string | undefined; }' is missing the following properties from type 'Promise<any>': then, catch, finally, [Symbol.toStringTag]

  36 | // Check the arguments and return type of the generateMetadata function
  37 | if ('generateMetadata' in entry) {
> 38 |   checkFields<Diff<PageProps, FirstArg<MaybeField<TEntry, 'generateMetadata'>>, 'generateMetadata'>>()
     |                               ^
  39 |   checkFields<Diff<ResolvingMetadata, SecondArg<MaybeField<TEntry, 'generateMetadata'>>, 'generateMetadata'>>()
  40 | }
  41 |
Next.js build worker exited with code: 1 and signal: null
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Answered by Asian black bear
searchParams has to be a Promise since Next v15.
View full answer

5 Replies

Asian black bear
searchParams has to be a Promise since Next v15.
Answer
@Asian black bear `searchParams` has to be a `Promise` since Next v15.
export async function generateMetadata({ searchParams }: Props): Promise<Metadata> {....
do you mean
const {bla} = await searchParams
Asian black bear
This is how you eventually access it, but you need to adjust the type of searchParams.