Skip to content

Commit

Permalink
Update: Add appropriate error message when for getServerSideProps inv…
Browse files Browse the repository at this point in the history
…alid return value (#35887)
  • Loading branch information
samueldusek committed Apr 6, 2022
1 parent 07723be commit cb565d6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
21 changes: 21 additions & 0 deletions errors/invalid-getserversideprops-return-value.md
@@ -0,0 +1,21 @@
# Invalid getServerSideProps Return Value

#### Why This Error Occurred

In one of the page's `getServerSideProps` the return value had the incorrect shape.

#### Possible Ways to Fix It

Make sure to return the following shape from `getServerSideProps`:

```ts
export async function getServerSideProps(ctx: GetServerSidePropsContext) {
return {
props: { [key: string]: any }
}
}
```

### Useful Links

- [getServerSideProps](https://nextjs.org/docs/api-reference/data-fetching/get-server-side-props)
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -645,6 +645,10 @@
{
"title": "import-next",
"path": "/errors/import-next.md"
},
{
"title": "invalid-getserversideprops-return-value",
"path": "/errors/invalid-getserversideprops-return-value.md"
}
]
}
Expand Down
9 changes: 7 additions & 2 deletions packages/next/server/render.tsx
Expand Up @@ -255,12 +255,17 @@ export type RenderOptsPartial = {

export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial

const invalidKeysMsg = (methodName: string, invalidKeys: string[]) => {
const invalidKeysMsg = (
methodName: 'getServerSideProps' | 'getStaticProps',
invalidKeys: string[]
) => {
const docsPathname = `invalid-${methodName.toLocaleLowerCase()}-value`

return (
`Additional keys were returned from \`${methodName}\`. Properties intended for your component must be nested under the \`props\` key, e.g.:` +
`\n\n\treturn { props: { title: 'My Title', content: '...' } }` +
`\n\nKeys that need to be moved: ${invalidKeys.join(', ')}.` +
`\nRead more: https://nextjs.org/docs/messages/invalid-getstaticprops-value`
`\nRead more: https://nextjs.org/docs/messages/${docsPathname}`
)
}

Expand Down

0 comments on commit cb565d6

Please sign in to comment.