Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NOJIRA-123): Prevent loading live embed multiple times #668

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/demo-html/public/live-embed.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</head>
<body>
<div class="element">I have z-index 10k</div>
<div id="wrapper" data-tf-live="embed-id" data-tf-hidden="[email protected]"></div>
<div id="wrapper" data-tf-live="01J5X1H8WM5CSXVDN89WXNX5EB" data-tf-hidden="[email protected]"></div>
<script src="./lib/embed.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion packages/embed/e2e/spec/functional/live-embed.cy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('Single Embed Code', () => {
describe(`Should load`, () => {
before(() => {
cy.intercept('https://api.typeform.com/single-embed/embed-id', {
cy.intercept('https://api.typeform.com/single-embed/01J5X1H8WM5CSXVDN89WXNX5EB', {
statusCode: 200,
body: {
html: `<div
Expand Down
8 changes: 5 additions & 3 deletions packages/embed/src/initializers/initialize-live-embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ export const initializeLiveEmbeds = ({

for (let index = 0; index < embedTypeElements.length; index += 1) {
const element = embedTypeElements.item(index)
if (forceReload || element.dataset.tfLoaded !== 'true') {
const canBeLoaded = element.dataset.tfLoading !== 'true' && element.dataset.tfLoaded !== 'true'
if (canBeLoaded || forceReload) {
const embedId = element.getAttribute(LIVE_EMBED_ATTRIBUTE)
if (!embedId) {
throw new Error(`Invalid ${LIVE_EMBED_ATTRIBUTE}=${embedId} for embed #${index}`)
}
element.dataset.tfLoading = 'true'

fetchLiveEmbed(embedId).then(({ html }) => {
element.innerHTML = html
element.dataset.tfLoaded = 'true'

onLiveEmbedLoad(element)
delete element.dataset.tfLoading
element.dataset.tfLoaded = 'true'
})
}
}
Expand Down
Loading