I’m getting a CORS error when trying to use a Backend Function:
Access to fetch at 'https://....execute-api.us-west-2.amazonaws.com/.../.../...' from origin 'https://....dev.8thwall.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I’m following the Tutorials but nothing mentions this. I’ve tried everything. Here is my current code:
import {datoCmsConfig} from 'backends/dato-cms-config'
const configFetch = async (
input: RequestInfo,
init?: RequestInit
) => {
console.log('Input: ', JSON.stringify(input))
console.log('Init: ', JSON.stringify(init))
const modifiedInit = {
...init,
mode: 'no-cors' as RequestMode,
headers: {
...init?.headers,
'Content-Type': 'application/json',
},
}
const res: Response = await datoCmsConfig(input, modifiedInit)
if (!res.ok) {
const errorBody = await res.json()
throw new Error(errorBody.message || 'Error fetching from backend function')
}
return res.json()
}
export {
configFetch,
}
configFetch is exported in the module.js file and utilized by my 8th Wall project like this:
import * as DatoCMS from 'dato-cms-function'
window.DatoCMS = DatoCMS
const datoCmsComponent = {
init() {
DatoCMS.configFetch({
path: '/config',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
},
})
},
}
export {datoCmsComponent}
Where dato-cms-function
is the Function created.
Thanks