devvit icon indicating copy to clipboard operation
devvit copied to clipboard

Docs | Incorrect example about sending data from Devvit to a webview

Open virtuallyunknown opened this issue 8 months ago • 0 comments

Hey folks. There is some slight issue with the example provided in the documentation:

window.addEventListener('message', (event) => {
  if (event.data.type === 'devvit-message') {
    const { message } = event.data;
    console.log('Received from Devvit:', message);
  }
});

https://developers.reddit.com/docs/webviews#from-devvit-to-web-view

When we send data from Devvit to the webview, it's structured differently, so trying to destructure the event.data here is invalid.

type DevvitMessage = {
    type: 'devvit-message';
    data: {
        message: {
             // actual contents of whatever data I send from devvit backend
        }
    }
}

// the destructuring in the example from the docs does not work
// because there is no message key/prop inside event.data
// instead, the data is in event.data.data.message
// and the top level message has a "devvit-message" type hard-coded

Image

virtuallyunknown avatar Apr 17 '25 17:04 virtuallyunknown