-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for proxying font URLs (#16796)
- Loading branch information
1 parent
70062eb
commit 8449fa5
Showing
6 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
frontend/src/scenes/session-recordings/player/rrweb/__snapshots__/index.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`CorsPlugin should replace font urls in links 1`] = `"https://replay.ph-proxy.com/proxy?url=https://app.posthog.com/fonts/my-font.woff2?t=1234"`; | ||
|
||
exports[`CorsPlugin should replace font urls in links 2`] = `"https://replay.ph-proxy.com/proxy?url=https://app.posthog.com/fonts/my-font.ttf"`; | ||
|
||
exports[`CorsPlugin should replace font urls in stylesheets 1`] = `"@font-face { font-display: fallback; font-family: "Roboto Condensed"; font-weight: 400; font-style: normal; src: url("https://replay.ph-proxy.com/proxy?url=https://posthog.com/assets/fonts/roboto/roboto_condensed_reg-webfont.woff2?11012022") format("woff2"), url("https://replay.ph-proxy.com/proxy?url=https://posthog.com/assets/fonts/roboto/roboto_condensed_reg-webfont.woff?11012022")"`; | ||
exports[`CorsPlugin should replace font urls in stylesheets 2`] = `"url("https://replay.ph-proxy.com/proxy?url=https://app.posthog.com/fonts/my-font.woff2")"`; |
24 changes: 24 additions & 0 deletions
24
frontend/src/scenes/session-recordings/player/rrweb/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { CorsPlugin } from '.' | ||
|
||
describe('CorsPlugin', () => { | ||
it.each([ | ||
`@font-face { font-display: fallback; font-family: "Roboto Condensed"; font-weight: 400; font-style: normal; src: url("https://posthog.com/assets/fonts/roboto/roboto_condensed_reg-webfont.woff2?11012022") format("woff2"), url("https://posthog.com/assets/fonts/roboto/roboto_condensed_reg-webfont.woff?11012022")`, | ||
`url("https://app.posthog.com/fonts/my-font.woff2")`, | ||
])('should replace font urls in stylesheets', (content: string) => { | ||
expect(CorsPlugin._replaceFontCssUrls(content)).toMatchSnapshot() | ||
}) | ||
|
||
it.each(['https://app.posthog.com/fonts/my-font.woff2?t=1234', 'https://app.posthog.com/fonts/my-font.ttf'])( | ||
'should replace font urls in links', | ||
(content: string) => { | ||
expect(CorsPlugin._replaceFontUrl(content)).toMatchSnapshot() | ||
} | ||
) | ||
|
||
it.each(['https://app.posthog.com/my-image.jpeg'])( | ||
'should not replace non-font urls in links', | ||
(content: string) => { | ||
expect(CorsPlugin._replaceFontUrl(content)).toEqual(content) | ||
} | ||
) | ||
}) |
38 changes: 38 additions & 0 deletions
38
frontend/src/scenes/session-recordings/player/rrweb/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ReplayPlugin, playerConfig } from 'rrweb/typings/types' | ||
|
||
const PROXY_URL = 'https://replay.ph-proxy.com' as const | ||
|
||
export const CorsPlugin: ReplayPlugin & { | ||
_replaceFontCssUrls: (value: string) => string | ||
_replaceFontUrl: (value: string) => string | ||
} = { | ||
_replaceFontCssUrls: (value: string): string => { | ||
return value.replace( | ||
/url\("(https:\/\/\S*(?:.eot|.woff2|.ttf|.woff)\S*)"\)/gi, | ||
`url("${PROXY_URL}/proxy?url=$1")` | ||
) | ||
}, | ||
|
||
_replaceFontUrl: (value: string): string => { | ||
return value.replace(/^(https:\/\/\S*(?:.eot|.woff2|.ttf|.woff)\S*)$/i, `${PROXY_URL}/proxy?url=$1`) | ||
}, | ||
|
||
onBuild: (node) => { | ||
if (node.nodeName === 'STYLE') { | ||
const styleElement = node as HTMLStyleElement | ||
styleElement.innerText = CorsPlugin._replaceFontCssUrls(styleElement.innerText) | ||
} | ||
|
||
if (node.nodeName === 'LINK') { | ||
const linkElement = node as HTMLLinkElement | ||
linkElement.href = CorsPlugin._replaceFontUrl(linkElement.href) | ||
} | ||
}, | ||
} | ||
|
||
export const COMMON_REPLAYER_CONFIG: Partial<playerConfig> = { | ||
triggerFocus: false, | ||
insertStyleRules: [ | ||
`.ph-no-capture { background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHZpZXdCb3g9IjAgMCAxNiAxNiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2IiBmaWxsPSJibGFjayIvPgo8cGF0aCBkPSJNOCAwSDE2TDAgMTZWOEw4IDBaIiBmaWxsPSIjMkQyRDJEIi8+CjxwYXRoIGQ9Ik0xNiA4VjE2SDhMMTYgOFoiIGZpbGw9IiMyRDJEMkQiLz4KPC9zdmc+Cg=="); }`, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+31.1 KB
(2700%)
...-snapshots/Navigation-App-Page-With-Side-Bar-Hidden-Mobile-1-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.