iframe and BASE_URL #2977
|
Hello, I need to embed iframes in my myst website. It works great locally, but it breaks when the website is deployed to github pages. Checking the console it seems that the URL in the iframe directive does not get prepended with BASE_URL (which is set correctly in the CI action). This is the directive I'm using: Note that if I use the devtools to make the iframe point at the correct location (i.e. I prepend the BASE_URL to its URL) then the page displays correctly. Is this a bug or I'm doing something wrong? Thanks in advance to anyone who can provide some guidance. |
Replies: 2 comments
|
Hi @lorenzo-rovigatti! You're not doing anything wrong. This is how the iframe renderer currently behaves. The Here are a few workarounds, from simplest to most automatic: 1. Use the full URL of the deployed file ```{iframe} https://<user>.github.io/<repo>/simulations/eulero.html
:width: 100%
```This works everywhere. The catch is that 2. Avoid relative paths like They look like they should work, but MyST pages are served both as 3. Patch the build output in CI (keeps Keep the absolute path in your Markdown and rewrite it after the build, before uploading the Pages artifact. The - name: Build HTML
run: myst build --html
env:
BASE_URL: /${{ github.event.repository.name }}
- name: Prefix iframe src with BASE_URL
run: |
grep -rlZ --include='*.html' --include='*.json' '/simulations/' _build/html \
| xargs -0 -r sed -i \
-e "s#src=\"/simulations/#src=\"${BASE_URL}/simulations/#g" \
-e "s#\"src\":\"/simulations/#\"src\":\"${BASE_URL}/simulations/#g"
env:
BASE_URL: /${{ github.event.repository.name }}Running this sed against a local build, both the HTML and the JSON end up with By the way, if you're not already doing it, declaring the folder in project:
static_files:
- simulationsI think it's worth opening an issue (or commenting on #2911) asking for Hope this helps! If it does, please mark it as the answer so the next person who embeds a simulation can find it 🙂 |
|
Thanks for the detailed answer! I did a couple of tests and it turns out that using a relative path such as |
Hi @lorenzo-rovigatti! You're not doing anything wrong. This is how the iframe renderer currently behaves.
The
{iframe}directive stores the URL exactly as you wrote it, and the theme renders it as-is (src={node.src}inmyst-to-react/src/iframe.tsx).BASE_URLgets prepended to internal page links and theme assets, but not to an iframesrc. It reproduces with mystmd 1.11.0 andBASE_URL=/repo myst build --html: the output still contains<iframe src="/simulations/eulero.html">, while the file itself is copied to_build/html/simulations/eulero.html, i.e. served at/repo/simulations/eulero.htmlon GitHub Pages. It's the same root cause as #2911, which is about site action buttons pointing at s…