nathan
(Nathan Searles)
September 20, 2023, 5:05pm
1
Next.js 13.5.1 causes the following type error related to api/preview/route.ts and api/exit-preview/route.ts.
Type error: Route "app/api/preview/route.ts" has an invalid export:
"Promise<void>" is not a valid GET return type:
Expected "Response | Promise<Response>", got "Promise<void>".
Expected "Promise<Response>", got "Promise<void>".
Expected "Response", got "void".
ali
(AKA)
September 21, 2023, 8:41am
2
We get exactly the same error. All was working without any problem before. We use 13.5.2.
Phil
(Phil Snow)
September 21, 2023, 9:50am
4
Hey Team,
I'll be happy to help.
Can you show me how your route resolver looks?
Also if one of you would be kind enough to share one of your project with me so I can test on my end.
Thanks.
fakhreddine
(fakhreddine ben fraj)
September 21, 2023, 1:11pm
5
this is the linkResolver function
export const linkResolver = (doc: FilledLinkToDocumentField) => {
if (doc.isBroken) {
return "/404";
}
if (doc.type === PRISMIC_TYPES.PAGE && doc.uid !== HOMEPAGE_UID) {
return `/${doc.uid}`;
}
return "/";
};
nathan
(Nathan Searles)
September 21, 2023, 3:46pm
6
Hey @Phil , thanks for taking a look!
Am I able to PM you through this forum? If so, I can share some other details.
Here's our current routes:
const routes: prismic.ClientConfig["routes"] = [
{
type: "homepage",
path: "/",
},
{
type: "page",
path: "/:uid",
},
{
type: "industry",
path: "/industry/:uid",
},
{
type: "news",
path: "/news",
},
{
type: "news_post",
path: "/news/:uid",
},
{
type: "media",
path: "/media",
},
{
type: "media_post",
path: "/media/:uid",
},
{
type: "careers",
path: "/careers",
},
];
Phil
(Phil Snow)
September 21, 2023, 4:46pm
7
I've sent you a pm @nathan . You can send me your project there.
Thanks.
ali
(AKA)
September 21, 2023, 5:12pm
8
@Phil We have exactly the same problem. We can't deploy to production anymore.
I also have the same issue. I'm using nextjs and deploying on vercel.
As a temporary workaround, I deployed the site without the preview folders.
Hope we can resolve this soon.
Phil
(Phil Snow)
September 22, 2023, 12:20pm
10
So is everyone only experiencing this in production or do you get this error locally as well?
Phil
(Phil Snow)
September 22, 2023, 12:44pm
12
Thanks. I'm looking in to it
nathan
(Nathan Searles)
September 22, 2023, 3:35pm
13
I can reproduce when running npm run build
locally. Thanks!
1 Like
ali
(AKA)
September 23, 2023, 7:30am
14
Me too, yarn build reproduce the bug in local @Phil @fakhreddine
I just check again. Sorry
ali
(AKA)
September 24, 2023, 5:26pm
15
Is there any news about this problem ?
Please @jamesminxyz Can you explain us how you can deployed the site without the preview folders ?
Hi @ali . As a temporary workaround, I added underscores to the route.js files. Obviously you can't use preview on production this way, but it works with Vercel.
When I am in local dev mode, I just remove the underscores to use the preview function locally.
Phil
(Phil Snow)
September 25, 2023, 9:10am
17
Hi @here ,
The error should be fixed as of @prismicio/next
v1.3.5
.
This wasn't an error in our package, but instead a breaking type change in Next.js.
Developers will need to make a change to the /api/preview
endpoint, as described in this PR under "How to fix your project":
prismicio:main
← prismicio:aa/fix-preview-endpoint-types
opened 08:22PM - 22 Sep 23 UTC
## Types of changes
- [ ] Chore (a non-breaking change which is related t… o package maintenance)
- [x] Bug fix (a non-breaking change which fixes an issue)
- [ ] New feature (a non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
## Description
This PR fixes a TypeScript error thrown when running `next build` in TypeScript projects. The error comes from `redirectToPreviewURL()` and `exitPreview()`'s return types.
The error only happens in projects using Next.js >13.5.1; the return type of Route Handlers changed to require a `Response` return value.
Although the changes in this packages are technically non-breaking, project code must be updated as a result of Next.js' changes.
Fixes #84
### How to fix your project
1. Update to the latest version of `@prismicio/next`:
```sh
npm install @prismicio/next@latest
```
2. Add `return` to `app/api/preview/route.ts` (or wherever you are using `redirectToPreviewURL()`):
```diff
// app/api/preview/route.ts
export async function GET(request: NextRequest) {
const client = createClient();
- await redirectToPreviewURL({ client, request });
+ return await redirectToPreviewURL({ client, request });
}
```
## Checklist:
- [ ] My change requires an update to the official documentation.
- [x] All [TSDoc](https://tsdoc.org) comments are up-to-date and new ones have been added where necessary.
- [x] All new and existing tests are passing.
🦩
We've opened a PR that updates @slicemachine/init
to generate the updated endpoint code:
prismicio:dev-next-release
← prismicio:aa/adapter-next-fix-preview-endpoints
opened 08:52PM - 22 Sep 23 UTC
## Context
This PR updates `@slicemachine/adapter-next`'s `/api/preview` and … `/api/exit-preview` templates.
The template changes fix a TypeScript build bug in projects using Next.js >13.1.5.
See https://github.com/prismicio/prismic-next/pull/85 for more details.
## The Solution
The `/api/preview` template must return a value.
The changes to `/api/exit-preview` are not necessary, but they remove a TypeScript warning stating `await` is not needed.
## Impact / Dependencies
Existing projects should update their `/api/preview` endpoints accordingly.
## Checklist before requesting a review
- [x] I hereby declare my code ready for review.
- [x] If it is a critical feature, I have added tests.
- [x] The CI is successful.
- [x] If there could backward compatibility issues, it has been discussed and planned.
Thanks.
1 Like
nathan
(Nathan Searles)
September 25, 2023, 5:47pm
18
Fix confirmed. Thanks so much @Phil and @angeloashmore !
1 Like
Phil
(Phil Snow)
September 26, 2023, 9:38am
19
Next v13.5.3, released today, and reverted the type regression
vercel:canary
← vercel:fix/route-handler
opened 09:14PM - 22 Sep 23 UTC
### What
#51394 introduced a pretty strict type of return value of route type… that causing failure with `next build`.
There're few ways of writing a app route, it could contain few return values based on the usage:
* return a `Response` or promise of it
* return `NextResponse` of promise of it, since it's extended from `Response`, same type
* use `redirect()` or `notFound(), since it returns `never`, and the below code is not reached, the handler itself could still return void. e.g. using `redirect` in a `GET` route
We loosed the type so `redirect()` can be still allowed without specifying the return value there.
Related typescript issue: https://github.com/microsoft/TypeScript/issues/16608#issuecomment-309327984
### How
* Re-enable the bail on types / build error in the app-routes tests
* Separate the tests, move runtime erroring ones to `test/e2e/app-dir/app-routes-errors`
* Add new case to app-routes tests of mixed return value
Closes #55623
Related #55604
Our changes are still good, but they are no longer required if you update to at least Next.js v13.5.3.