Skip to content

Commit 605f8aa

Browse files
authored
Merge pull request #9122 from marmelab/fix-newsletter-script-loading-master
[Website] Fix newsletter script loading in doc pages - backport to `master`
2 parents 1d45298 + f79bb0b commit 605f8aa

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

docs/_layouts/default.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@
164164

165165
var AUTOHIDE = Boolean(0);
166166
</script>
167-
<script defer src="https://sibforms.com/forms/end-form/build/main.js"></script>
168167

169168
<script async defer src="https://unpkg.com/[email protected]/lib/typescript.js"></script>
170169
<script async defer src="https://unpkg.com/[email protected]/standalone.js"></script>

docs/documentation.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,6 @@ <h2>Recipes</h2>
296296
});
297297
</script>
298298

299-
<!-- Newsletter form -->
300-
<script>
301-
window.REQUIRED_CODE_ERROR_MESSAGE = 'Please choose a country code';
302-
window.LOCALE = 'en';
303-
window.GENERIC_INVALID_MESSAGE = window.EMAIL_INVALID_MESSAGE = window.SMS_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
304-
window.REQUIRED_ERROR_MESSAGE = "This field is required.";
305-
306-
var AUTOHIDE = Boolean(0);
307-
</script>
308-
309299
<!--
310300
React-admin protects your privacy!
311301
We use our own self-hosted tracking solution to collect some raw metrics,
@@ -319,6 +309,16 @@ <h2>Recipes</h2>
319309
src="https://analytics.marmelab.com/umami.js"
320310
></script>
321311

312+
<!-- Newsletter form -->
313+
<script>
314+
window.REQUIRED_CODE_ERROR_MESSAGE = 'Please choose a country code';
315+
window.LOCALE = 'en';
316+
window.GENERIC_INVALID_MESSAGE = window.EMAIL_INVALID_MESSAGE = window.SMS_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
317+
window.REQUIRED_ERROR_MESSAGE = "This field is required.";
318+
319+
var AUTOHIDE = Boolean(0);
320+
</script>
321+
322322
<script async defer src="https://unpkg.com/[email protected]/lib/typescript.js"></script>
323323
<script async defer src="https://unpkg.com/[email protected]/standalone.js"></script>
324324
<script async defer src="https://unpkg.com/[email protected]/parser-babel.js"></script>

docs/js/ra-doc-exec.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,19 @@ function toggleDockBlocks(status) {
274274
}
275275
}
276276

277+
function loadNewsletterScript() {
278+
/* Load the script only of the form is in the DOM */
279+
if (document.querySelector('#sib-form') != null) {
280+
const script = document.createElement('script');
281+
script.src = 'https://sibforms.com/forms/end-form/build/main.js';
282+
script.type = 'text/javascript';
283+
script.id = 'newsletter_script';
284+
document.head.appendChild(script);
285+
} else {
286+
document.getElementById('newsletter_script')?.remove();
287+
}
288+
}
289+
277290
// Replace full page reloads by a fill of the content area
278291
// so that the side navigation keeps its state
279292
// use a global event listener to also catch links inside the content area
@@ -307,7 +320,8 @@ document.addEventListener('click', event => {
307320
fetch(href)
308321
.then(res => res.text())
309322
.then(replaceContent)
310-
.then(buildJSCodeBlocksFromTS);
323+
.then(buildJSCodeBlocksFromTS)
324+
.then(loadNewsletterScript);
311325
// change the URL
312326
window.history.pushState(null, null, href);
313327
changeSelectedMenu();
@@ -327,7 +341,8 @@ window.addEventListener('popstate', () => {
327341
fetch(window.location.pathname)
328342
.then(res => res.text())
329343
.then(replaceContent)
330-
.then(buildJSCodeBlocksFromTS);
344+
.then(buildJSCodeBlocksFromTS)
345+
.then(loadNewsletterScript);
331346
}
332347
changeSelectedMenu();
333348
});
@@ -343,4 +358,5 @@ window.addEventListener('DOMContentLoaded', () => {
343358

344359
navigationFitScroll();
345360
buildJSCodeBlocksFromTS();
361+
loadNewsletterScript();
346362
});

0 commit comments

Comments
 (0)