
Why AI-Generated HTML Pages Break Outside ChatGPT and Claude: A Developer's Debugging Guide
A hands-on troubleshooting guide covering the 7 most common reasons HTML, CSS, and JavaScript generated by ChatGPT, Claude, or Gemini render fine in chat previews but break when saved or hosted.
You ask ChatGPT, Claude, or Gemini to build an interactive calculator, a data dashboard, or a landing page. Inside the AI chat preview pane, it looks polished and works smoothly. Then you copy the code into a .html file, open it in Chrome or send it to a colleague, and it turns into a blank white screen—or half the buttons stop responding.
This happens because AI chat preview sandboxes inject runtimes, preloaded libraries, and permissive styling that a standard browser tab does not provide. Below are the seven most frequent failure modes and how to fix each one in under two minutes.
1. The Output Is Actually JSX/React, Not Standalone HTML
Claude Artifacts and ChatGPT Canvas frequently generate React components using import React from 'react', lucide-react, or recharts. Browsers cannot execute JSX or bare module specifiers natively.
Symptom: Blank page, plus a DevTools console error like:
Uncaught SyntaxError: Unexpected token '<' or Failed to resolve module specifier "react".
How to fix it:
- Best option (zero dependencies): Prompt the model explicitly: "Rewrite this as a single self-contained HTML5 file using vanilla JavaScript, inline CSS, and inline SVG icons—no React, no JSX, and no external build step."
- If you must keep a library: Use ESM-compatible CDN URLs with pinned versions via
<script type="module">(for example,https://esm.sh/chart.js@4.4.6), or bundle the project with Vite before hosting.
2. Missing Tailwind CSS Runtime
Models love writing utility classes like class="flex items-center justify-between p-6 bg-slate-900 text-white rounded-xl" without including any stylesheet—because chat preview panes pre-load Tailwind automatically.
Symptom: All text appears stacked vertically in Times New Roman with zero spacing, borders, or colors.
How to fix it:
- For quick prototypes, add the Tailwind CDN script in
<head>(or better yet, ask the model to replace Tailwind classes with a clean<style>block of plain CSS so the page loads instantly and works offline):
<!-- Quick prototype fix (requires internet) -->
<script src="https://cdn.tailwindcss.com"></script>- For production or mobile sharing, plain inline CSS with CSS variables (
:root { --accent: #2563eb; }) eliminates the ~300 ms runtime compilation delay and prevents flash of unstyled content.
3. Broken or Hallucinated CDN Script URLs
LLMs occasionally invent library paths, point to deprecated cdn.jsdelivr.net versions, or mix UMD and ESM builds (import Chart from 'https://cdnjs.cloudflare.com/.../chart.min.js', which fails because the UMD bundle has no ES default export).
Symptom: ReferenceError: Chart is not defined or HTTP 404 / MIME type errors in the Network tab.
How to fix it:
- Open DevTools → Network and filter by
JSto spot red404requests. - Match the script tag type to the bundle format:
- UMD / global build (attaches
window.Chart): load with a regular<script src="..."></script>before your inline script. - ESM build (uses
export): load inside<script type="module">usingimport ... from 'https://esm.sh/...'.
- UMD / global build (attaches
4. Script Runs Before the DOM Exists
When an AI places <script> in the <head> without defer or DOMContentLoaded, document.getElementById('submit-btn') returns null because the HTML body has not been parsed yet.
Symptom: Uncaught TypeError: Cannot read properties of null (reading 'addEventListener').
How to fix it: Move <script> to the bottom of <body> right before </body>, or wrap initialization in DOMContentLoaded:
document.addEventListener('DOMContentLoaded', () => {
const btn = document.getElementById('submit-btn');
if (!btn) return;
btn.addEventListener('click', handleSubmit);
});5. localStorage or fetch() Crashes Inside Sandboxed Viewers
Many AI-generated apps call localStorage.setItem(...) unconditionally on startup to save theme preferences or form drafts. When hosted inside a sandboxed iframe (or opened in Safari Private Browsing / strict cookie mode), accessing window.localStorage throws a SecurityError that aborts the entire script.
Symptom: Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window': The document is sandboxed.
How to fix it: Always wrap storage access in a tiny defensive helper with an in-memory fallback:
const memoryStore = new Map();
function safeGet(key, fallback = null) {
try {
return window.localStorage.getItem(key) ?? fallback;
} catch {
return memoryStore.get(key) ?? fallback;
}
}
function safeSet(key, value) {
try {
window.localStorage.setItem(key, value);
} catch {
memoryStore.set(key, value);
}
}Similarly, if the page calls fetch('https://api.example.com/...'), remember that third-party APIs without Access-Control-Allow-Origin: * will be blocked by browser CORS when served from a real domain.
6. Missing Mobile Viewport Tag and Fixed-Width Layouts
Chat preview panes are usually desktop-sized cards. Models often hardcode width: 900px on containers or omit the viewport meta tag, causing horizontal overflow on phones.
Symptom: Tiny zoomed-out text on iOS/Android, or charts clipped past the right edge of the screen.
How to fix it:
- Add
<meta name="viewport" content="width=device-width, initial-scale=1">inside<head>. - Replace fixed pixel widths (
width: 900px) withwidth: 100%; max-width: 900px;. - Wrap wide tables and
<canvas>elements in a scrollable container (overflow-x: auto).
7. Truncated Output at the Model's Token Limit
Long dashboards (800+ lines) frequently get cut off mid-tag or mid-function when a model hits its single-response output cap.
Symptom: Unclosed <script> tag, half-rendered styles, or Uncaught SyntaxError: Unexpected end of input.
How to fix it:
- Check the last 10 lines of the file: it must close cleanly with
</script>,</body>, and</html>. - Keep CSS concise (use CSS Grid and variables instead of repeating rules for every card) and ask the model to store repetitive data in a compact JSON array rendered by a short loop rather than repeating 500 lines of static HTML.
A 60-Second Pre-Share Debugging Checklist
Before sharing any AI-generated HTML page with clients, students, or teammates:
- Open DevTools Console and reload with cache disabled—verify 0 red errors.
- Check the Network tab—verify no
404CDN scripts or blocked fonts. - Toggle Device Toolbar (
375×812mobile viewport)—verify no horizontal scrollbar. - Test in an Incognito window—catches
localStoragecrashes and logged-in cookie assumptions. - View Source / End of File—confirm
</html>is present and no JSXimportstatements remain.
FAQ
Why does my Claude Artifact work in Claude.ai but not when saved as .html?
Most interactive Claude Artifacts are React/JSX components (application/vnd.ant.react) rendered inside Claude's built-in React + Tailwind runtime. Saving JSX directly as .html fails in a normal browser unless you ask Claude to output plain standalone HTML/CSS/JS or compile the component with a bundler like Vite.
Why do my icons disappear when I open an AI-generated HTML file?
AI models frequently import icons from lucide-react or reference Font Awesome classes without loading the icon stylesheet. Switching to inline <svg> icons fixes the issue permanently and works offline.
How can I test an HTML file on my phone without deploying a full server?
Opening raw .html attachments directly inside iOS Mail, WhatsApp, or WeChat often strips JavaScript or shows raw source code. Hosting the single HTML file on a static link host (like dochost, GitHub Pages, or Cloudflare Pages) gives you a real HTTPS URL that renders identically in Safari and Chrome on mobile.
Is it safe to run JavaScript generated by an LLM?
Always skim generated <script> blocks for unexpected fetch() endpoints, external <script src> domains, or eval() calls before sharing the page—especially if you pasted proprietary data into the prompt.
Author
Founder of dochost (Xhorizon Labs LLC). Software engineer focused on web publishing, HTML and Markdown rendering pipelines, sandboxed content delivery, and AI developer workflows.
Categories
localStorage or fetch() Crashes Inside Sandboxed Viewers6. Missing Mobile Viewport Tag and Fixed-Width Layouts7. Truncated Output at the Model's Token LimitA 60-Second Pre-Share Debugging ChecklistFAQWhy does my Claude Artifact work in Claude.ai but not when saved as .html?Why do my icons disappear when I open an AI-generated HTML file?How can I test an HTML file on my phone without deploying a full server?Is it safe to run JavaScript generated by an LLM?More Posts

How to Share an HTML File as a Link (5 Ways, 2026)
Five practical ways to share an HTML file as a link in 2026 — paste HTML get link, host a single file, Netlify Drop, GitHub Pages, and email workarounds — with real dochost free limits.

How to Publish a Claude Artifact as a Standalone Webpage
Two ways to put a Claude artifact on a real URL: Claude's own Share link (Publish, for legacy chat artifacts), or pasting the code into dochost for a plain standalone link.

Tiiny.host vs Netlify Drop: The Fastest Way to Put One HTML File Online
A head-to-head look at Tiiny.host and Netlify Drop for getting a single HTML file online — setup, accounts, free-tier limits, and where dochost fits.
Newsletter
Join the community
Subscribe to our newsletter for the latest news and updates