Keeping Vite site content in JSON files
By Yash Garg · · 5 min read
You can keep your React website copy in local JSON files and import it directly into components without installing external plugins or loaders. This isolates editable text from your UI components while keeping your codebase lean.
This approach works cleanly for landing pages, marketing sites, and client handoffs where you want text separated from presentation components. You can use default imports to pull an entire file or named imports to grab specific top-level fields. When you import single fields, unused sibling keys are omitted from the production bundle. Starting in Vite 6, the `json.stringify` setting defaults to `'auto'`, as noted in the Vite migration guide. Starting in Vite 6, `json.stringify` defaults to `'auto'`, which will only stringify large JSON files, according to the Vite migration guide.
Folder organization: src versus public
Where you store JSON files dictates how Vite handles them during builds and page loads. Storing editable content in `src/content/` ties the data directly into your build graph. This gives you instant updates during development through Hot Module Replacement (HMR) and bundles your text together with your minified JavaScript.
```text src/ content/ home.json navigation.json pricing.json components/ Hero.tsx PricingList.tsx ```
In contrast, files placed inside the `public/` directory bypass bundling altogether. Vite serves those assets as raw static files, which means you cannot use standard import statements to pull them into your React tree. Storing content in `src/content/` keeps version history intact inside your Git repository alongside component code. Storing content files directly in your Git repository alongside code simplifies versioning, a structure documented in the Decap CMS overview. For larger multi-page projects that also handle long-form articles, teams often weigh structured data objects against prose documents; see our comparison of markdown vs JSON for site content to choose the right format.
| Location | Import Method | HMR Support | Bundled & Minified | Ideal Use Case |
|---|---|---|---|---|
| `src/content/` | `import` statement | Yes (instant) | Yes | Static page text, lists, navigation links |
| `public/` | `fetch()` network call | No (browser refresh) | No | Large dynamic datasets, runtime config payloads |
Configuring TypeScript for JSON imports
When you introduce local JSON files into a React and Vite TypeScript project, you might encounter a compile error stating `Cannot find module './content.json'`. This happens because TypeScript defaults to ignoring non-code files unless explicitly configured to parse them.
To enable native imports, open your `tsconfig.json` or `tsconfig.app.json` file. The compiler options include `resolveJsonModule`, as listed in the TypeScript compiler documentation.
```text { "compilerOptions": { "target": "ES2022", "module": "ESNext", "moduleResolution": "bundler", "resolveJsonModule": true, "strict": true } } ```
The TypeScript resolveJsonModule documentation explains that enabling this flag instructs TypeScript to inspect the contents of `.json` files and generate accurate static types automatically. You do not need to write manual type definitions or interfaces for simple site content.
If you want to enforce that a component's props match your JSON shape exactly, you can derive types directly using the `typeof` keyword.
```text import homeContent from '../content/home.json';
export type HomeContent = typeof homeContent; ```
This pattern keeps types strictly synchronized with what lives in your data files. If an editor changes a string to a boolean or alters an object key, your TypeScript build step catches the mismatch before deployment. Other tools like TinaCMS take a schema-first approach where you define collections inside a dedicated configuration file, as detailed in the TinaCMS documentation. In contrast, deriving types directly from local JSON files requires no additional configuration files or schemas.
Consuming JSON in React components
Once TypeScript is configured, you can consume your content files across your component tree. Vite allows you to cherry-pick named exports from your JSON root object to keep individual page bundles lean.
Keep in mind that Vite only supports named exports for top-level keys in a JSON document. If your JSON structure nests properties inside a child object, you must import the top-level object and read properties from it inside the component.
```text { "heading": "Modern Websites Delivered Fast", "subheading": "We build fast, editable websites for growing businesses.", "features": [ { "title": "Speed", "desc": "Sub-second load times." }, { "title": "Security", "desc": "Static output with no databases." } ] } ```
In your React component, import the values directly.
```text import { heading, subheading, features } from '../content/home.json';
export function Hero() { return (
{heading} {subheading}
{features.map((item) => (
{item.title} {item.desc}
))}
); } ```
Avoid creating a single global `content.json` file that contains the text for your entire site. For example, a five-page marketing site that imports one unified content file on every page prevents bundlers from code-splitting routes cleanly. Split content by route or section so each component only bundles the copy it displays.
Common pitfalls and how to fix them
Working with JSON imports in Vite is straightforward, but three common mistakes frequently cause build failures or client confusion.
1. Trying to import from the public folder
Placing `data.json` inside the `public/` directory and writing `import content from '/public/data.json'` breaks the Vite build process. Vite treats `public/` as an unbundled pass-through directory.
- The fix: Move the JSON file into `src/content/`. If the file must remain in `public/` because an external script modifies it at runtime, retrieve it in a component using standard browser `fetch('/data.json')`.
2. Expecting runtime edits without a redeploy
Clients often assume that modifying a bundled JSON file on the live web server updates the site immediately.
- The fix: Connect your project repository to a hosting platform with CI/CD automation. Every time the JSON file changes in Git, trigger an automated rebuild to push the new copy live.
3. Attempting deep named imports
Attempting to import a nested property directly will result in compile warnings and runtime `undefined` errors.
- The problem: `import { desc } from '../content/home.json/features[0]'` is invalid syntax.
- The fix: Import the top-level parent array or object (`import { features } from '../content/home.json'`) and access the nested values inside your component.
Handoff to clients and visual editing
Storing site content in structured JSON makes life easier for developers, but raw files are awkward for non-technical clients. A client asked to edit JSON on GitHub can break the site by accidentally deleting a quotation mark, adding a trailing comma, or mistyping a bracket.
Git workflows add friction as well. As outlined in GitHub's guide on editing files directly, users cannot edit or upload files on the GitHub web editor if a branch is protected. By default, branch protection rules prevent matching branches from being deleted and disable force pushes, as documented in GitHub's guide on managing protected branches. For agency setups that keep main locked down, editing requires branching and manual review. Pull requests let users discuss and review proposals before merging them, as explained in the GitHub pull request reference. To safeguard your repository structure before giving collaborators access, review our recommendations to protect the main branch on client sites.
```text Content Handoff Checklist:
- Extract all static copy from JSX components into src/content/.
- Run a production build (npm run build) to verify TypeScript compiles without errors.
- Confirm branch protection is configured to prevent accidental force-pushes.
- Set up an automated deployment hook on Git push.
- Provide a safe visual interface so clients never have to format raw syntax.
```
If your clients want an intuitive editing screen, you can connect your repository to TurnIntoCMS visual editor for React and Vite. TurnIntoCMS turns an existing GitHub repository into a visual editor without running migrations, rebuilding the application, or adding custom schemas. Clients log in, click text, links, and list items directly on a live preview of the site, and publish changes.
When a client hits publish, the platform writes the updates back directly to the source files on GitHub as a standard Git commit, triggering your existing deployment pipeline. TurnIntoCMS is not a full page builder and does not provide an editorial approval workflow or scheduled publishing, but for updating text, lists, and images in JSON-backed Vite sites, it prevents syntax mistakes entirely.
FAQ
Can I import a JSON file directly without installing a Vite plugin?
Yes.
Should I use fetch('/content.json') or import content from './content.json'?
Use `import` when you want your content baked directly into the static bundle with tree shaking and instant HMR updates. Use `fetch()` from the `public/` directory only if the content updates on a remote server without triggering a repository rebuild.
How do I fix the 'Cannot find module' error in TypeScript?
Enabling `resolveJsonModule` allows importing JSON files as described in the TypeScript resolveJsonModule documentation.
Does importing large JSON files slow down page load times in Vite?
No. Vite 6 introduces a new default value of `'auto'` for `json.stringify`, which only stringifies large JSON files, according to the Vite migration guide.
Sources
- Migration from v5 (v6.vite.dev, checked November 24, 2026)
- TypeScript: TSConfig Reference (typescriptlang.org, checked November 24, 2026)
- TypeScript: TSConfig Option: resolveJsonModule (typescriptlang.org, checked November 24, 2026)
- Decap CMS overview (decapcms.org, checked November 24, 2026)
- Get Started and learn about TinaCMS (tina.io, checked November 24, 2026)
- About protected branches (docs.github.com, checked November 24, 2026)
- Pull requests (docs.github.com, checked November 24, 2026)
- Editing files (docs.github.com, checked November 24, 2026)