// === MAIN ===
(async function(){
const container = document.getElementById('post');
const slug = q('slug');
if (!slug) { container.textContent = 'Error: no slug in URL.'; return; }
// ✅ Delivery host + env + token in URL (no headers)
const url =
`https://cdn.contentful.com/spaces/${SPACE_ID}/environments/${ENV_ID}/entries` +
`?content_type=${CT_ID}&fields.slug=${encodeURIComponent(slug)}` +
`&include=10&limit=1&locale=*` +
`&access_token=${TOKEN}`;
console.log('[REQUEST]', url);
try {
const res = await fetch(url); // ✅ no headers
if (!res.ok) {
container.innerHTML = `Load failed (HTTP ${res.status}). Check token, env, and publish state.
`;
return;
}
const data = await res.json();
console.log('[RESPONSE]', data);
if (!data.items || data.items.length === 0) {
container.innerHTML = `Post not found. Check the slug in Contentful.
`;
return;
}
const post = data.items[0];
// resolve featured image from includes
let img = '';
const imgId = post.fields?.featuredImage?.sys?.id;
const asset = (data.includes?.Asset || []).find(a => a.sys.id === imgId);
if (asset?.fields?.file?.url) {
img = `
`;
}
const html = `
${img}
Last Updated: ${new Date(post.sys.updatedAt).toLocaleDateString()}
${post.fields.title || ''}
${renderRT(post.fields.content)}
`;
document.getElementById('page-title').textContent =
`${post.fields.title || 'Post'} - One Step Club`;
container.innerHTML = html;
} catch (err) {
console.error(err);
container.innerHTML = `Network/JS error. ${err.message}
`;
}
})();