setPreviewData on Next.js /api serverless throws error (not a funciton)

Viewed 292

I'm using next.js for presenting data from sanity.io and I'm deploying it as a serverless application on AWS (edge-optimized). At first this functionality was working but at some point it just stopped working on the aws deployment and I just can' figure out what it is. On the http://localhost:3000 endpoint it is still working.

I used the official next.js guide to implement https://nextjs.org/docs/advanced-features/preview-mode

This is a simple version of the code

export default async function handler(req, res) {

// code that checks that that the request is correct here, it works

console.log('about to call setPreviewData', res.setPreviewData.toString());
// the previous log command is called and this is how the function is logged
// (data,options= {} )=>setPreviewData(apiRes,data,Object.assign({} apiContext,options))
res.setPreviewData({})

// the function crashes between the previous and this log

console.log('Location to go to', `/${req.query.slug}`)
res.writeHead(307, {
    Location: `/${post.type}/${post.slug}`
})
res.end()

}

relevant dependencies

"next": "^10.0.3",

dev dependencies

"serverless-next.js": "^1.15.0-alpha.2",
"serverless-nextjs-plugin": "^2.5.2-alpha.0"

Even stranger is that in the same api folder I have an exit-preview function

export default async function handler(_, res) {
res.clearPreviewData()
res.writeHead(307, { Location: '/' })
res.end()

}

I have checked the CloudFront configuration and 'Forward Cookies' is turned on.

In Cloudwatch I get a chain of errors around e[t] is not a function

"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "TypeError: e[t] is not a function",
"reason": {
    "errorType": "TypeError",
    "errorMessage": "e[t] is not a function",
    "stack": [
        "TypeError: e[t] is not a function",
        "    at __webpack_require__ (/var/task/pages/api/previewArticle.js:7249:30130)"
...

Does anyone have any suggestion to what I can try? Thank you :)

0 Answers
Related