From 41c3b1e3df5d6e086a273d6df2a524a43fa22ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mime=20=C4=8Cuvalo?= Date: Wed, 26 Jun 2024 12:36:57 +0100 Subject: [PATCH] bookmarks: account for relative urls more robustly (#4022) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes up url's that don't have `/` in front or `http` ### Change Type - [ ] `feature` — New feature - [x] `improvement` — Product improvement - [ ] `api` — API change - [ ] `bugfix` — Bug fix - [ ] `other` — Changes that don't affect SDK users, e.g. internal or .com changes ### Release Notes - Bookmark extractor: account for relative urls more robustly --- apps/docs/app/layout.tsx | 12 ++++++------ apps/dotcom-bookmark-extractor/lib/unfurl.ts | 4 ++-- apps/dotcom/src/utils/createAssetFromUrl.ts | 4 ++-- apps/vscode/editor/src/utils/bookmarks.ts | 4 ++-- .../tldraw/src/lib/defaultExternalContentHandlers.ts | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/apps/docs/app/layout.tsx b/apps/docs/app/layout.tsx index 8c6f7e874..3846dfff8 100644 --- a/apps/docs/app/layout.tsx +++ b/apps/docs/app/layout.tsx @@ -46,12 +46,12 @@ export const metadata: Metadata = { }, icons: [ { rel: 'shortcut icon', url: '/favicon.svg' }, - { rel: 'icon', url: 'favicon-32x32.svg', sizes: '32x32' }, - { rel: 'icon', url: 'favicon-16x16.svg', sizes: '16x16' }, - { rel: 'apple-touch-icon', url: 'apple-touch-icon.png' }, - { rel: 'apple-touch-icon', url: 'apple-touch-icon-152x152.svg', sizes: '152x152' }, - { rel: 'apple-touch-icon', url: 'apple-touch-icon-180x180.svg', sizes: '180x180' }, - { rel: 'apple-touch-icon', url: 'apple-touch-icon-167x167.svg', sizes: '167x167' }, + { rel: 'icon', url: '/favicon-32x32.svg', sizes: '32x32' }, + { rel: 'icon', url: '/favicon-16x16.svg', sizes: '16x16' }, + { rel: 'apple-touch-icon', url: '/apple-touch-icon.png' }, + { rel: 'apple-touch-icon', url: '/apple-touch-icon-152x152.svg', sizes: '152x152' }, + { rel: 'apple-touch-icon', url: '/apple-touch-icon-180x180.svg', sizes: '180x180' }, + { rel: 'apple-touch-icon', url: '/apple-touch-icon-167x167.svg', sizes: '167x167' }, ], } diff --git a/apps/dotcom-bookmark-extractor/lib/unfurl.ts b/apps/dotcom-bookmark-extractor/lib/unfurl.ts index f363f8677..25c835c5c 100644 --- a/apps/dotcom-bookmark-extractor/lib/unfurl.ts +++ b/apps/dotcom-bookmark-extractor/lib/unfurl.ts @@ -31,10 +31,10 @@ export async function unfurl(url: string) { $('link[rel="icon"]').attr('href') ?? undefined - if (image?.startsWith('/')) { + if (image && !image?.startsWith('http')) { image = new URL(image, url).href } - if (favicon?.startsWith('/')) { + if (favicon && !favicon?.startsWith('http')) { favicon = new URL(favicon, url).href } diff --git a/apps/dotcom/src/utils/createAssetFromUrl.ts b/apps/dotcom/src/utils/createAssetFromUrl.ts index e9c28d9bb..b40003581 100644 --- a/apps/dotcom/src/utils/createAssetFromUrl.ts +++ b/apps/dotcom/src/utils/createAssetFromUrl.ts @@ -58,10 +58,10 @@ export async function createAssetFromUrl({ url }: { type: 'url'; url: string }): description: doc.head.querySelector('meta[property="og:description"]')?.getAttribute('content') ?? '', } - if (meta.image.startsWith('/')) { + if (!meta.image.startsWith('http')) { meta.image = new URL(meta.image, url).href } - if (meta.favicon.startsWith('/')) { + if (!meta.favicon.startsWith('http')) { meta.favicon = new URL(meta.favicon, url).href } } catch (error) { diff --git a/apps/vscode/editor/src/utils/bookmarks.ts b/apps/vscode/editor/src/utils/bookmarks.ts index 4760e9a70..377eecaf6 100644 --- a/apps/vscode/editor/src/utils/bookmarks.ts +++ b/apps/vscode/editor/src/utils/bookmarks.ts @@ -43,10 +43,10 @@ export async function onCreateAssetFromUrl({ description: doc.head.querySelector('meta[property="og:description"]')?.getAttribute('content') ?? '', } - if (meta.image.startsWith('/')) { + if (!meta.image.startsWith('http')) { meta.image = new URL(meta.image, url).href } - if (meta.favicon.startsWith('/')) { + if (!meta.favicon.startsWith('http')) { meta.favicon = new URL(meta.favicon, url).href } } catch (error) { diff --git a/packages/tldraw/src/lib/defaultExternalContentHandlers.ts b/packages/tldraw/src/lib/defaultExternalContentHandlers.ts index 856511e3d..9f3f6e37c 100644 --- a/packages/tldraw/src/lib/defaultExternalContentHandlers.ts +++ b/packages/tldraw/src/lib/defaultExternalContentHandlers.ts @@ -148,10 +148,10 @@ export function registerDefaultExternalContentHandlers( description: doc.head.querySelector('meta[property="og:description"]')?.getAttribute('content') ?? '', } - if (meta.image.startsWith('/')) { + if (!meta.image.startsWith('http')) { meta.image = new URL(meta.image, url).href } - if (meta.favicon.startsWith('/')) { + if (!meta.favicon.startsWith('http')) { meta.favicon = new URL(meta.favicon, url).href } } catch (error) {