From 1d46d330851daf914a5751606dc7155ad320ce9a Mon Sep 17 00:00:00 2001 From: Chance Strickland Date: Fri, 10 Mar 2023 11:00:47 -0800 Subject: [PATCH] Add support for creating non-meta tags with `v2_meta` (#5746) --- .changeset/meta-v2-enhancements.md | 6 ++ integration/meta-test.ts | 91 ++++++++++++++++++- packages/remix-react/components.tsx | 51 +++++++++-- packages/remix-react/routeModules.ts | 11 ++- packages/remix-server-runtime/routeModules.ts | 11 ++- 5 files changed, 157 insertions(+), 13 deletions(-) create mode 100644 .changeset/meta-v2-enhancements.md diff --git a/.changeset/meta-v2-enhancements.md b/.changeset/meta-v2-enhancements.md new file mode 100644 index 00000000000..08bc9d5e875 --- /dev/null +++ b/.changeset/meta-v2-enhancements.md @@ -0,0 +1,6 @@ +--- +"@remix-run/react": minor +"@remix-run/server-runtime": patch +--- + +Add support for generating `", "") + .trim(); + + expect(JSON.parse(scriptContents)).toEqual({ + "@context": "http://schema.org", + "@type": "Person", + name: "Sonny Day", + address: { + "@type": "PostalAddress", + streetAddress: "123 Sunset Cliffs Blvd", + addressLocality: "San Diego", + addressRegion: "CA", + postalCode: "92107", + }, + email: ["sonnyday@fancymail.com", "surfergal@veryprofessional.org"], + }); + }); + + test("{ tagName: 'link' } adds a ", async ({ page }) => { + let app = new PlaywrightFixture(appFixture, page); + await app.goto("/authors/1"); + expect(await app.getHtml('link[rel="canonical"]')).toBeTruthy(); + }); }); diff --git a/packages/remix-react/components.tsx b/packages/remix-react/components.tsx index 9c484975f08..52fe9d257e1 100644 --- a/packages/remix-react/components.tsx +++ b/packages/remix-react/components.tsx @@ -602,7 +602,7 @@ function V1Meta() { } if (["charset", "charSet"].includes(name)) { - return ; + return ; } if (name === "title") { @@ -719,18 +719,49 @@ function V2Meta() { return null; } + if ("tagName" in metaProps) { + let tagName = metaProps.tagName; + delete metaProps.tagName; + if (!isValidMetaTag(tagName)) { + console.warn( + `A meta object uses an invalid tagName: ${tagName}. Expected either 'link' or 'meta'` + ); + return null; + } + let Comp = tagName; + return ; + } + if ("title" in metaProps) { return {String(metaProps.title)}; } - if ("charSet" in metaProps || "charset" in metaProps) { - // TODO: We normalize this for the user in v1, but should we continue - // to do that? Seems like a nice convenience IMO. + if ("charset" in metaProps) { + metaProps.charSet ??= metaProps.charset; + delete metaProps.charset; + } + + if ("charSet" in metaProps && metaProps.charSet != null) { + return typeof metaProps.charSet === "string" ? ( + + ) : null; + } + + if ("script:ld+json" in metaProps) { + let json: string | null = null; + try { + json = JSON.stringify(metaProps["script:ld+json"]); + } catch (err) {} return ( - + json != null && ( +