Let’s be honest: sometimes you just want to store some structured data and read it from another domain, without messing with a real backend or complicated APIs. But then, CORS shows up and ruins your fetch-from-anywhere dreams.
Actually, there’s a neat little hack: just use a JavaScript file as your “database.”
Seriously.
You basically create a .js
file that contains your data as a JavaScript variable or array. Something like this:
// blogs.js
var blogs = [
{
title: "First Post",
date: "2020-01-01",
content: "<p>Hello world!</p>"
},
{
title: "Another Post",
date: "2020-01-15",
content: "<p>More content here.</p>"
}
// ...more entries
];
Then, on any site, even it’s crossing domains, just import your JS file with a script tag:
<script src="https://your.cdn.com/blogs.js"></script>
<script>
// you can basically just use any variable declared in blogs.js!
blogs.forEach(post => {
// Do something with post.title, post.content, etc.
});
</script>
No CORS. No backend. Just data, ready to go.
Pros:
<script>
tags behave (no CORS headaches).Cons:
Still exploring!