Retrieving post.raw only works right after creating post
The issue is that the raw post data is not available after a page refresh. Mathijs_V encountered this problem while working on a custom PostMenuButton and found that only the cooked post data is available after a refresh.
sam explained that this is by design, as shipping raw data to all clients would increase the topic payload size unnecessarily. Instead, raw data should be requested on-demand.
Mathijs_V was looking for a way to retrieve raw post data from JavaScript and initially used a workaround by fetching the /t/raw/{topicid}/{postid}.json endpoint. However, sam confirmed that this is the only available option.
paresy asked if this is still true and if there’s a JS API for making an authenticated request. paresy later shared a solution using the discourse/lib/ajax module to fetch the raw post data:
import {ajax} from "discourse/lib/ajax";
let post = this.findAncestorModel();
ajax(`/posts/${post.id}`, { type: "GET", cache: false }).then(
(result) => {
console.log(result.raw);
}
);
This solution allows for on-demand retrieval of raw post data from JavaScript.
Thanks for your reply! Now I can stop wasting time
Ahh that makes so much sense!
Could you point me in the right direction to retrieve post data from javascript?
I had a sort of workaround to get the .json by retrieving the /t/raw/{topicid}/{postid}.json but that was really ugly and there must be a better way.