:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(workflows): open email preview links in a new tab (#97046)
ruby childs3fa06f0003e2
diff --git a/frontend/src/scenes/hog-functions/email-templater/EmailTemplater.tsx b/frontend/src/scenes/hog-functions/email-templater/EmailTemplater.tsxindex 076c5009b016..37cbb13d6970 100644--- a/frontend/src/scenes/hog-functions/email-templater/EmailTemplater.tsx+++ b/frontend/src/scenes/hog-functions/email-templater/EmailTemplater.tsx@@ -33,6 +33,7 @@ import 'products/workflows/frontend/TemplateLibrary/MessageTemplatesGrid.scss'import { MessageTemplateCard } from 'products/workflows/frontend/TemplateLibrary/MessageTemplateCard'import { collapseToolsPanelCustomJs } from './custom-tools/collapseToolsPanel'+import { previewLinkTargetCustomJs } from './custom-tools/previewLinkTarget'import { unsubscribeLinkToolCustomJs } from './custom-tools/unsubscribeLinkTool'import { EMAIL_TYPE_SUPPORTED_FIELDS, EmailTemplaterLogicProps, emailTemplaterLogic } from './emailTemplaterLogic'import { EmailFieldErrors, EmailTemplateFrom, MAX_WORKFLOW_EMAIL_SENDERS } from './types'@@ -207,6 +208,7 @@ function DestinationEmailTemplaterForm({// paid credits from our Unlayer workspace, so keep them all off.ai: false,},+ customJS: [previewLinkTargetCustomJs],}}/></div>@@ -702,8 +704,12 @@ function NativeEmailTemplaterForm({},projectId: unlayerEditorProjectId,customJS: sceneIntegrationEnabled- ? [unsubscribeLinkToolCustomJs, collapseToolsPanelCustomJs]- : [unsubscribeLinkToolCustomJs],+ ? [+ unsubscribeLinkToolCustomJs,+ collapseToolsPanelCustomJs,+ previewLinkTargetCustomJs,+ ]+ : [unsubscribeLinkToolCustomJs, previewLinkTargetCustomJs],fonts: unlayerEditorProjectId? {showDefaultFonts: true,diff --git a/frontend/src/scenes/hog-functions/email-templater/custom-tools/previewLinkTarget.test.ts b/frontend/src/scenes/hog-functions/email-templater/custom-tools/previewLinkTarget.test.tsnew file mode 100644index 000000000000..c40cfa75d1b7--- /dev/null+++ b/frontend/src/scenes/hog-functions/email-templater/custom-tools/previewLinkTarget.test.ts@@ -0,0 +1,37 @@+import { previewLinkTargetCustomJs } from './previewLinkTarget'++describe('previewLinkTargetCustomJs', () => {+ // The customJS runs as a string inside Unlayer's iframe, so typecheck cannot see it. Run it+ // against a stub of the parts of the Unlayer API it touches.+ function runPreviewHtml(html?: string): string {+ let previewHtml: ((params: { html?: string }, done: (result: { html: string }) => void) => void) | undefined+ const unlayer = {+ registerCallback: (name: string, callback: typeof previewHtml): void => {+ if (name === 'previewHtml') {+ previewHtml = callback+ }+ },+ }+ new Function('unlayer', previewLinkTargetCustomJs)(unlayer)+ if (!previewHtml) {+ throw new Error('previewHtml callback was not registered')+ }+ let result = ''+ previewHtml({ html }, (params) => {+ result = params.html+ })+ return result+ }++ it.each([+ [+ 'a full document',+ '<!DOCTYPE html><html><head><title>t</title></head><body>hi</body></html>',+ '<!DOCTYPE html><html><head><base target="_blank"><title>t</title></head><body>hi</body></html>',+ ],+ ['markup with no head', '<div>hi</div>', '<base target="_blank"><div>hi</div>'],+ ['empty markup', undefined, '<base target="_blank">'],+ ])('gives %s a default link target', (_name, html, expected) => {+ expect(runPreviewHtml(html)).toEqual(expected)+ })+})diff --git a/frontend/src/scenes/hog-functions/email-templater/custom-tools/previewLinkTarget.ts b/frontend/src/scenes/hog-functions/email-templater/custom-tools/previewLinkTarget.tsnew file mode 100644index 000000000000..c1ba841d5e2a--- /dev/null+++ b/frontend/src/scenes/hog-functions/email-templater/custom-tools/previewLinkTarget.ts@@ -0,0 +1,23 @@+/**+ * Unlayer shows the preview in a nested iframe, so a link that has no target navigates that+ * iframe. Most sites refuse to be framed, so an author who clicks a link to test it gets a+ * broken page. `previewHtml` is Unlayer's documented hook for the markup that preview mode+ * shows: give that markup a default target and the clicks open a new tab. The base tag has+ * no href, so relative URLs resolve as before, and the exported email does not change.+ */+export const previewLinkTargetCustomJs = `+if (typeof unlayer !== 'undefined' && unlayer.registerCallback) {+ unlayer.registerCallback('previewHtml', function (params, done) {+ var html = params && params.html ? params.html : ''+ var baseTag = '<base target="_blank">'+ var headStart = html.indexOf('<head')+ var headTagEnd = headStart === -1 ? -1 : html.indexOf('>', headStart)+ done({+ html:+ headTagEnd === -1+ ? baseTag + html+ : html.slice(0, headTagEnd + 1) + baseTag + html.slice(headTagEnd + 1),+ })+ })+}+`