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.

Bug: iframe `src` attribute set incorrectly in jsx

See original GitHub issue

My MWE is a very simple iframe:

<iframe src="https://content.com"></iframe>

This works just fine (full iframe code below).

If I wrap the src attribute’s value in curly braces (code below), I get the following:

<iframe src={"https://content.com"}></iframe>

image

image

The actual iframe content is an embedded Powerpoint presentation:

<iframe
src="https://capsida.sharepoint.com/sites/sitename/_layouts/15/Doc.aspx?sourcedoc={xxxxxx}&amp;action=embedview"
width="476px"
height="288px"
frameBorder="0">
</iframe>

React version:

Steps To Reproduce

  1. Open this Stackblitz. You should not be able to see the embedded Powerpoint content, but the iframe should load correctly (you should see a Microsoft login screen).
  2. Wrap the src="https://..." in curly braces: src={"https://..."} and save the component. Now the iframe does not load! You can see the screenshots below to observe this phenomenon:

image

image

The current behavior

My real example is something like src={url}, where url is a state variable. The issue is not that I want to wrap the attribute in curly braces, it’s that this iframe loads differently when the attribute is hard-coded into the jsx versus loaded from javascript via curly braces {url}.

I also recognize that Microsoft’s Powerpoint embedding logic may forbid cross-origin embedding, and I want to emphasize that this is clearly not the issue here despite the Chrome console message. If cross-origin embedding was the issue, the iframe should equally fail with or without curly braces.

The expected behavior

I expect the iframe to load (or not load) the same way regardless of whether its src attribute is passed as an HTML attribute in jsx (src="https://...") or as a jsx attribute (src={"https://..."}).

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:19

github_iconTop GitHub Comments

1reaction
zachsiegel-capsidacommented, Oct 27, 2022

SOLVED!

Per this StackOverflow comment, the string &amp; in Javascript strings works slightly differently from HTML. In iframe src attributes in HTML, it’s not necessary to escape ampersands in this way, so I don’t know why Microsoft’s Sharepoint “embed” snippets are made that way.

I am able to change src={url} to src={sanitizeUrl(url)} where sanitizeUrl is the following function:

function sanitizeUrl(url) {
	return url.replaceAll('&amp;', '&')
}

I would consider this issue closed.

1reaction
zachsiegel-capsidacommented, Sep 7, 2022

@Noah670 99.9% of the time I would assume I am doing something wrong and that the React developers would call my issue a mis-use rather than a bug, but I think my MWE is very compelling. I doubt many people would see the MWE and expect it to succeed/fail depending on those curly braces.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React change iframe src with user input - Stack Overflow
I want to be able to change an iframe src with the help of react but i don't know were to start as...
Read more >
React, Iframes, and a Back-Navigation Bug
Notice that the iframe has a dynamic src attribute, meaning its content changes as you navigate to a different page in the app....
Read more >
Best practices for React iframes - LogRocket Blog
Explore two use cases for React iframes, embedding external ... You can only use the src attribute to set the URL that we...
Read more >
Iframes with React: Best Practices | by Andrea Perera
To boost page loading speed, set the iframe src/url attribute with JavaScript after the main content has been loaded.
Read more >
A Deep Dive into Debugging Cypress, iframes, & Runtime ...
Being able to effectively discover, track down, and resolve bugs is one ... each managed/controlled input lives), setting the src attribute, ...
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