question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apps never render in hidden iframes in development mode

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:46:32 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T6000
Binaries:
  Node: 14.19.1
  npm: 6.14.16
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 12.2.3
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome 103.0.5060.134

How are you deploying your application? (if relevant)

Contentful app SDK (development)

Describe the Bug

When running NextJS in development mode (e.g. next dev), apps that are rendered into hidden cross-origin iframes can fail to render.

I first observed this issue when using NextJS to load an application via the Contentful App Framework in development mode. When Contentful loads the application, it does so by requesting a page URL via a hidden iframe. In some instances (e.g. Contentful’s App Configuration screen), the app is not considered “loaded” (and thus remains hidden) until a specific Contentful SDK callback is called. The callback is typically (and specifically in my case) consumed through a React Provider, and thus is is not called until my apps first render cycle is completed.

In development mode, NextJS relies on scheduling and running an animation frame to handle flashes of unstyled content with style-loader. The problem is that hidden iframes have no guarantees about running animation frames, so while NextJS assumes requestAnimationFrame is safe to use if it’s available, when used within an iframe, it’s not a reliable way to schedule future work.

I believe the bug can be fixed by only relying on requestAnimationFrame if the window object is not contained within an iframe (can detect size of window too to be extra safe).

I was able to work around this bug by running this snippet of code before rendering the app:

if (typeof window !== 'undefined' && window.self !== window.top) {
  window.requestAnimationFrame = window.setTimeout;
}

Expected Behavior

Although the iframe is hidden, we should still render the app.

Link to reproduction

about:blank

To Reproduce

Apologies for no reproduction link. I couldn’t easily reproduce this in other environments.

Hoping the bug description and related source code link + article is enough to illustrate the issue

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
timothympacecommented, Aug 5, 2022

You might have missed some important details in my bug description. I linked that line of code too, but the problem is that requestAnimationFrame is available, but just not trustworthy. In the window.requestAnimationFrame || safeSetTimeout selection, requestAnimationFrame gets selected, but the callback never fires.

You can check out this article as to why it’s not firing the callback, even though requestAnimationFrame is defined and available.

1reaction
timothympacecommented, Aug 5, 2022

In fact, given that this is such a niche issue… I’d love to use it as an opportunity to open a PR and contribute. Doesn’t look like a high priority bug for the vercel team to fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create-React-App creates this <iframe> that prevents me from ...
I have to delete this iframe to edit the elements directly from the browser but can't think why this is rendering each time...
Read more >
It's time to lazy-load offscreen iframes! - web.dev
If an iframe meets any of these conditions, Chrome considers it hidden and won't lazy-load it in most cases. iframes that aren't hidden...
Read more >
Iframes with React: Best Practices | by Andrea Perera
Embedding an Iframe inside a React app comes with some risks where Iframe will load content outside your control unless you take the...
Read more >
The ultimate guide to iframes - LogRocket Blog
Not a fan of iframes? This post provides an overview of the tag's best features, shows you how to use them, and how...
Read more >
<iframe>: The Inline Frame element - HTML - MDN Web Docs
Set to true if the <iframe> can activate fullscreen mode by calling the requestFullscreen() method. Note: This attribute is considered a legacy ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found