---
title: "Framer Help: How to fix custom code optimization errors like “Cannot Set Property of Undefined”"
description: Optimization issues often occur when custom code components or overrides depend on browser-specific APIs. This guide explains why these errors arise and how to resolve them.
published: "Nov 6, 2026, 11:03 AM UTC"
---

# How to fix custom code optimization errors like “Cannot Set Property of Undefined”

Optimization issues often occur when custom code components or overrides depend on browser-specific APIs. This guide explains why these errors arise and how to resolve them.

### [Why optimization issues occur](#why-optimization-issues-occur)

Framer pre-renders pages on the server to improve performance and ensure compatibility across all devices. However, during pre-rendering:
- Browser-specific APIs like `window`, `document`, and `navigator` are **not available** because the server lacks browser context (e.g., browser settings, window size, cookies).
- [Custom code](../../../dictionary/custom-code) that relies on these APIs will cause errors during pre-rendering.

### [Avoid browser-specific APIs](#avoid-browser-specific-apis)

Write [JavaScript](../../../dictionary/javascript) that doesn’t rely on browser APIs for rendering.
- **Instead of**: Using `window.innerWidth`.
- **Use**: CSS media queries to handle layout changes based on screen size.

For example:

```
// ❌️ Bad: will cause optimization issues
export function Component() {
  const windowWidth = window.innerWidth

  return windowWidth < 768 ? <MobileVideo /> : <DesktopVideo />
}

// ✅ Good: won’t cause optimization issues
import { withCSS } from "framer"

function Component() {
  return <>
    <style>{`
      .my-component-video-mobile { display: block }
      .my-component-video-desktop { display: none }
        
      @media (min-width: 768px) {
        .my-component-video-mobile { display: none }
        .my-component-video-desktop { display: block }
      }
    `}</style>
    <div className="my-component-video-mobile"><MobileVideo /></div>
    <div className="my-component-video-desktop"><DesktopVideo /></div>
  </>
}
```

### [Put browser-specific API calls into `useEffect`](#put-browser-specific-api-calls-into-useeffect)

If you can’t avoid using browser-specific APIs, try moving them [inside](https://react.dev/reference/react/useEffect)`[useEffect](https://react.dev/reference/react/useEffect)` instead. `useEffect` executes only in the browser, when the page has fully loaded, so it won’t cause optimization issues. For example:

```
// ❌️ Bad: will cause optimization issues
function Component() {
  window.gtag("event", "component_rendered")

  return <div>Hello!</div>
}

// ✅ Good: won’t cause optimization issues
function Component() {
  useEffect(() => {
    window.gtag("event", "component_rendered")
  }, [])

  return <div>Hello!</div>
}
```

### [Use placeholders for browser-dependent data](#use-placeholders-for-browser-dependent-data)

If your component needs information like `document.cookie` (e.g., user login status) or `navigator.language`:
1. Write a component that shows a **placeholder** during pre-rendering.
2. After the page loads, update the component with the correct data.

**Example:** [framer-optim-1.jsx](https://gist.github.com/heypiotr/602ca88b9991ac9df2211d74598638ef#file-framer-optim-1-jsx)

### [Opt out of optimization](#opt-out-of-optimization)

If necessary, you can opt your component out of optimization entirely:
1. Skip pre-rendering for the component using server-side logic.
2. Note:
   - The component’s content won’t be accessible to search engines.
   - It will only load after the page has fully loaded.
   - Use a placeholder to provide a fallback experience for better usability.

**Example:** [framer-optim-2.jsx](https://gist.github.com/heypiotr/602ca88b9991ac9df2211d74598638ef#file-framer-optim-2-jsx)

### [Apply the same approach to overrides](#apply-the-same-approach-to-overrides)

For custom overrides:
- Use placeholders or opt out of optimization in a similar way to components.

**Example:** [framer-optim-3.jsx](https://gist.github.com/heypiotr/602ca88b9991ac9df2211d74598638ef#file-framer-optim-3-jsx)\
\
If you're still experiencing issues, please reach out to us through our [contact](../../../) page for further help or check out the [Developer Space](https://www.framer.community/c/developers/).

\
\


**Example:** [framer-optim-3.jsx](https://gist.github.com/heypiotr/602ca88b9991ac9df2211d74598638ef#file-framer-optim-3-jsx)

If you're still experiencing issues, please reach out to us through our [contact](../../../) page for further help or check out the [Developer Space](https://www.framer.community/c/developers/).

#### FAQ

### Why do optimization errors like 'Cannot set property of undefined' occur in Framer when using custom code?

Framer pre-renders pages on the server to improve performance and ensure compatibility across all devices. During pre-rendering, browser-specific APIs like window, document, and navigator are not available because the server lacks browser context. Custom code that relies on these APIs will cause errors during pre-rendering, leading to issues such as 'Cannot set property of undefined.'

### How can I safely use browser-specific APIs like window or document in my Framer components?

If you can’t avoid using browser-specific APIs, move them inside useEffect instead. useEffect executes only in the browser, when the page has fully loaded, so it won’t cause optimization issues. For example: // ✅ Good: won’t cause optimization issues function Component() { useEffect(() => { window.gtag("event", "component\_rendered") }, \[\]) return \<div>Hello!\</div> }

### What is the recommended way to handle responsive layouts in Framer custom code without causing optimization errors?

Write JavaScript that doesn’t rely on browser APIs for rendering. Instead of using window.innerWidth, use CSS media queries to handle layout changes based on screen size. For example, use CSS classes and media queries to show or hide components for different screen sizes, rather than relying on JavaScript to detect window size during rendering.

Updated

2026年9月15日

## Your next idea starts here

Create personal portfolio, Build startup site, `Launch landing page`, Start company blog

[Start without AI](https://framer.com/signup)

---

## Navigation

- [Enterprise](../../../enterprise/)
- [Pricing](../../../pricing)
- [Log in](https://framer.com/login)
- [Sign up](https://www.framer.com/r/signup/)
- [Brand guidelines](../../../brand)
- [Help center](../../)
- [Troubleshooting](../../troubleshooting/)
- [AI](../../../ai/)
- [Agents](../../../agents/)
- [MCP New](../../../mcp/)
- [Design](../../../design/)
- [Collaborate](../../../collaborate/)
- [CMS](../../../cms/)
- [Hosting](../../../hosting/)
- [Performance](../../../performance/)
- [Analytics](../../../analytics)
- [SEO](../../../seo/)
- [Skills New](../../../skills)
- [Convert](../../../convert/)
- [Publish](../../../publish/)
- [Updates](../../../updates)
- [Templates](https://www.framer.com/community/marketplace/templates/)
- [Components](https://www.framer.com/community/marketplace/components/)
- [Vectors](https://www.framer.com/community/marketplace/vectors/)
- [Plugins](https://www.framer.com/community/marketplace/plugins/)
- [Gallery](https://www.framer.com/community/gallery/)
- [Hire Experts](https://www.framer.com/community/members/)
- [Switch New](../../../switch/)
- [Agencies](../../../agencies/)
- [Startups](../../../startups/)
- [Academy](../../../academy/)
- [Desktop App](../../../downloads/)
- [Developers](../../../developers/)
- [Affiliates](../../../creators)
- [Experts New](../../../expert/apply/)
- [Guides](../../../guides)
- [Blog](../../../blog/)
- [Newsletter](../../../newsletter)
- [Stories](../../../stories/)
- [Skills Index New](../../../skills/index)
- [Students](../../../education/students/)
- [Ambassadors](../../../)
- [Help Articles](../../)
- [Meetups](../../../meetups/)
- [Contact](../../../contact/)
- [Designers](../../../solutions/designers/)
- [Agencies](../../../solutions/agencies/)
- [Marketers](../../../solutions/marketers/)
- [Growth](../../../solutions/growth/)
- [Builders](../../../solutions/builders/)
- [Engineers](../../../solutions/engineers/)
- [Site Teams](../../../solutions/site-teams/)
- [Founders](../../../solutions/founders/)
- [AI website builder](../../../solutions/ai-website-builder/)
- [AI design agent](../../../solutions/ai-designer/)
- [Pro sites](../../../solutions/website-builder/)
- [Landing pages](../../../solutions/landing-pages/)
- [Portfolio maker](../../../solutions/portfolio-website/)
- [UI/UX design](../../../solutions/ui-ux-design/)
- [Figma to HTML](../../../solutions/figma-to-html/)
- [No-code](../../../solutions/no-code-website-builder/)
- [Compare](../../../compare)
- [Webflow](../../../compare/framer-vs-webflow)
- [Figma](../../../compare/framer-vs-figma)
- [Wix](../../../compare/framer-vs-wix)
- [Squarespace](../../../compare/framer-vs-squarespace)
- [WordPress](../../../compare/framer-vs-wordpress)
- [Readymag](../../../compare/framer-vs-readymag)
- [Ceros](../../../compare/framer-vs-ceros)
- [Unbounce](../../../compare/framer-vs-unbounce)
- [Lovable](../../../compare/framer-vs-lovable)
- [Claude Code](../../../compare/framer-vs-claude-code)
- [ChatGPT](../../../compare/framer-vs-chatgpt)
- [Codex](../../../compare/framer-vs-codex)
- [Contentful](../../../compare/framer-vs-contentful)
- [Sanity](../../../compare/framer-vs-sanity)
- [AEM](../../../compare/framer-vs-adobe-experience-manager)
- [Replit](../../../compare/framer-vs-replit)
- [Astro](../../../compare/framer-vs-astro)
- [v0](../../../compare/framer-vs-v0)
- [About New](../../../about)
- [Careers](../../../careers/)
- [Security](../../../legal/security)
- [Brand](../../../brand)
- [Abuse](mailto:abuse@framer.com)
- [Legal](../../../legal/community-terms)
- [Trust](https://app.eu.vanta.com/framer.com/trust/ow67ujg7iav0t6qtd1o6r2)
- [Merch](../../../store)
- [Keyboard](../../../f1)
- [Stack Score](../../../stack-score/)
- [Free domains](../../../domains/)
- [CanvasBench](../../../canvasbench/)
- [State of Sites](https://www.framer.com/state-of-sites-2026/)
- [AEO scanner](../../../aeo)
- [Link Preview](https://www.linkpreview.app/)
- [Checking status](https://www.framerstatus.com/)
