Skip to content

Take the CDN host and settings from the script's own tag - #31

Open
yellowby wants to merge 4 commits into
devfrom
fix/script-tag-lookup
Open

yellowby wants to merge 4 commits into
devfrom
fix/script-tag-lookup

Conversation

@yellowby

Copy link
Copy Markdown
Member

Problem

At start, embed.js read every <script> named embed.js or iframely.js on the page. It took the CDN host from
any of them served from the root of a host, and its settings from the first one with a query string.

Instagram and TikTok embed codes load their own embed.js from the root of their hosts. On a page with such an
embed:

  • plain links could unfurl to //www.tiktok.com/api/iframe?... and stay empty;
  • another widget's embed.js?... could replace the page's settings, the key included.

A //cdn.iframe.ly/embed.js?key=... tag was not affected. A self-hosted script in a folder, a pre-set config with a
tag that has no query string, a bundled script and the ES module were. The bug dates from 1.x.

Fix

The script finds the tag that loaded it with document.currentScript.

  • CDN host: only from that tag, when the file is at the root of its host (//YOUR_CDN/embed.js), or from
    ?cdn=, the pre-set config, configure({ cdn }) or iframely.CDN.
  • Settings: from that tag. Other embed.js / iframely.js tags are read only when it has none, so a page where
    the cloud copy from API embed codes starts first and another tag carries the key keeps working.

Behaviour change

The ES module and a bundled script have no tag of their own, so they keep the default CDN whatever tags are on the
page. A site on its own CDN that relied on a //YOUR_CDN/embed.js tag to tell them must set cdn explicitly. The
same goes for a self-hosted script in a folder (?cdn=YOUR_CDN).

Tests

  • 11 new unit tests; 8 of them fail without the fix.
  • A browser test with a new fixture page, demo/other-embed-js.html, in Chromium, Firefox and WebKit.

At init the library scanned every <script> named embed.js or iframely.js
and took the CDN host from any of them at the root of its host, and the
settings from the first one with a query string. Instagram, TikTok and
Threads serve their own embed.js from the root of their hosts, so on a
page with their embeds plain links could unfurl to
//www.tiktok.com/api/iframe, and another provider's query string could
replace the page's settings.

The running tag is now found with document.currentScript. The CDN host
comes only from that tag or from explicit configuration. Settings are
read from that tag; other tags are scanned only when it has none (or the
library runs without a tag of its own, as the ES module does), which
keeps pages working where the query-less cloud copy from API embed codes
boots first and another tag carries the key.
demo/other-embed-js.html loads a TikTok and an Instagram embed.js, and a
made-up widget's embed.js with a query string, around our own tag. The
test checks in three browsers that the running tag is found, its settings
are the ones applied and plain links unfurl on the Iframely CDN.

dist/ is rebuilt with the fix.
README and USAGE.md say that the script reads the tag that loaded it and
that other files named embed.js on the page do not change its settings
or CDN, and drop the advice to rename ours to iframely.js. They also say
that the ES module and a bundled script need the CDN set in code.

The changelog gets an Unreleased entry with the fix, the workaround for
1.x and the one behaviour change for sites on their own CDN.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant