Gatsby: How to use Iframe to introduce HTML files in Gatsbyjs?

Created on 20 Sep 2019  ·  14Comments  ·  Source: gatsbyjs/gatsby

I have an HTML file that I need to import and display in the form of an Iframe
IAK45BAFRTPP 8%A{I%TUWI
. But... I haven't found a way to achieve it. Can you tell me how to achieve it? It is an HTML file and cannot be converted into a react component.

awaiting author response question or discussion

Most helpful comment

@PiccoloYu i have a solution for you, i'm going to describe the steps i took to triage below:

  • Created a new gatsby site based on the hello world starter.
  • Based on the guidelines and best practices in gatsby, i created a new file called test.html inside the static folder with some random content.
  • Modified /src/pages/index.js to the following:
import React from "react"

export default () => (
  <div
    style={{
      display: "flex",
      alignContent: "center",
      margin: "3rem auto",
      justifyContent: "space-around",

    }}
  >
    <div>
      <iframe src="/test.html" title="dummy" style={{width:'800px',height:'600px'}}></iframe>
    </div>
    <div>this is content for the page</div>
  </div>
)

  • Issued gatsby develop and opened up http://localhost:8000 and i'm presented with the folllowing, also on the right side i tried to access the page directly and as you can see by the image the issue persists:
    picollo_1_dev
  • Tried gatsby build && gatsby serve and i opened up http://localhost:9000 on one window and http://localhost:9000/test.html and i can now see the content as you can see below:
    picollo_2_build

It seems that on build you can see the actual html without any issues.

  • Did a little search and it seems your issue is related to #13072 . Went through the issue and pieh's comment offered a workaround and a explanation on what is happening. With that in mind i changed gatsby-node.js to contain the following:
const express= require('express');

exports.onCreateDevServer=({app})=>{
    app.use(express.static('public'))
}
  • Issued gatsby develop and opened up browser windows to bothhttp://localhost:8000 and http://localhost:8000/test.html and i'm presented with the following:
    picollo_3_dev

If you want i can hoist up the code to a repo and you can go over it at your own pace.

Feel free to provide feedback so that we can close this issue or continue to work on it until we find a suitable solution.

All 14 comments

IEC%)TYB)JRKBD0JL5S8~LK
H~ 12~Q}IC{<a href="OE@45XHUL">OE@45XHUL</a>{TV

Have you tried using the static folder?
https://www.gatsbyjs.org/docs/static-folder/

So placing the HTML file there and pointing the src there instead of using GraphQL.

您是否尝试过使用静态文件夹?
https://www.gatsbyjs.org/docs/static-folder/

因此,将HTML文件放在此处并将src指向那里,而不是使用GraphQL。

4$CA}9}Y02I{<code>H%C10N}(</code>H
Yes, there is no problem in introducing pictures. But Iframe will make a mistake.

Create a React.js component in your site directory at src/pages/impress/impress.html.js and this page will automatically refresh to show the new page component you created. If you were trying to reach another page, perhaps you can find it below.
IV00)6O%9$LF{ABPO1C RAJ

@PiccoloYu if you want i can make a small reproduction and describe how you can do it. Sounds good?

@jonniebigodes Of course, this is the best. Thank you very much.

No need to thank, i'm going make the reproduction and report back.

@PiccoloYu i have a solution for you, i'm going to describe the steps i took to triage below:

  • Created a new gatsby site based on the hello world starter.
  • Based on the guidelines and best practices in gatsby, i created a new file called test.html inside the static folder with some random content.
  • Modified /src/pages/index.js to the following:
import React from "react"

export default () => (
  <div
    style={{
      display: "flex",
      alignContent: "center",
      margin: "3rem auto",
      justifyContent: "space-around",

    }}
  >
    <div>
      <iframe src="/test.html" title="dummy" style={{width:'800px',height:'600px'}}></iframe>
    </div>
    <div>this is content for the page</div>
  </div>
)

  • Issued gatsby develop and opened up http://localhost:8000 and i'm presented with the folllowing, also on the right side i tried to access the page directly and as you can see by the image the issue persists:
    picollo_1_dev
  • Tried gatsby build && gatsby serve and i opened up http://localhost:9000 on one window and http://localhost:9000/test.html and i can now see the content as you can see below:
    picollo_2_build

It seems that on build you can see the actual html without any issues.

  • Did a little search and it seems your issue is related to #13072 . Went through the issue and pieh's comment offered a workaround and a explanation on what is happening. With that in mind i changed gatsby-node.js to contain the following:
const express= require('express');

exports.onCreateDevServer=({app})=>{
    app.use(express.static('public'))
}
  • Issued gatsby develop and opened up browser windows to bothhttp://localhost:8000 and http://localhost:8000/test.html and i'm presented with the following:
    picollo_3_dev

If you want i can hoist up the code to a repo and you can go over it at your own pace.

Feel free to provide feedback so that we can close this issue or continue to work on it until we find a suitable solution.

@jonniebigodes (∩_∩)It would certainly be very helpful to provide an example.

@PiccoloYu here you go, i've set up a repo with the code used for this issue in here

@jonniebigodes Yes, it works!!

@PiccoloYu glad that i was able to help you and you managed to get it to work.

@jonniebigodes Just a comment to say thank you, your well detailed explanatory comment was super helpful, thanks a lot.

@LukyVj no need to thank, glad that i was able to help you aswell.

you just need to add this code in gatsby-node.js

const express = require('express')
exports.onCreateDevServer=({app})=>{
    app.use(express.static('public'))
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dustinhorton picture dustinhorton  ·  3Comments

rossPatton picture rossPatton  ·  3Comments

magicly picture magicly  ·  3Comments

timbrandin picture timbrandin  ·  3Comments

Oppenheimer1 picture Oppenheimer1  ·  3Comments