Implementing locking mechanism for SSR
In case you want to implement AB Tasty in a server-side rendering (SSR) environment, you might want to use our locking mechanism to prevent our tag from executing and modify the DOM before it fully loads, thus avoiding any reconciliation issues.
You will find here basic implementation examples for the three main JS frameworks and vanilla JS.
Next JS
import { Html, Head, Main, NextScript } from 'next/document';
export default function Document() {
return (
<Html>
<Head>
{/* Lock AB Tasty before tag loads */}
<script>{`window.lockABTastyTag = true;`}</script>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}import { useEffect } from 'react';
function MyApp({ Component, pageProps }) {
useEffect(() => {
const tryUnlockABTasty = () => {
if (typeof window?.unlockABTasty === 'function') {
window.unlockABTasty();
} else {
setTimeout(tryUnlockABTasty, 100);
}
};
tryUnlockABTasty();
}, []);
return <Component {...pageProps} />;
}
export default MyApp;Angular
<head>
<script>
window.lockABTastyTag = true;
</script>
</head>import { AfterViewInit, Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<router-outlet></router-outlet>`
})
export class AppComponent implements AfterViewInit {
ngAfterViewInit(): void {
const tryUnlockABTasty = () => {
if (typeof window.unlockABTasty === 'function') {
window.unlockABTasty();
} else {
setTimeout(tryUnlockABTasty, 100);
}
};
tryUnlockABTasty();
}
}VueJS
export default {
app: {
head: {
script: [
{ children: 'window.lockABTastyTag = true;' }
]
}
}
}export default {
mounted() {
if (typeof window !== 'undefined' && window.unlockABTasty) {
window.unlockABTasty();
}
}
}Vanilla JS
<head>
<!-- Lock AB Tasty before the tag loads -->
<script>
window.lockABTastyTag = true;
</script>
<!-- AB Tasty tag here -->
<script src="https://try.abtasty.com/.../abtasty.js" async></script>
</head>window.addEventListener("DOMContentLoaded", function () {
if (typeof window.unlockABTasty === "function") {
window.unlockABTasty();
}
});
Last updated
Was this helpful?

