Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-email): Missing TextDecoder, TextEncoder and stream variants of these on the global for email components being rendered #1259

Merged

Conversation

gabrielmfern
Copy link
Collaborator

What does this fix?

This adds a few things that our render utility requires on the global scope
to be ran properly. Missing stuff like TextDecoder was causing someone that used it
inside their email to have issues on this.

Closes #1258

How can I make sure its fixed?

  1. Copy the code for this git diff which has the changes necessary to test
    diff --git a/apps/demo/emails/airbnb-review.tsx b/apps/demo/emails/airbnb-review.tsx
    index 07386ff..0658ccf 100644
    --- a/apps/demo/emails/airbnb-review.tsx
    +++ b/apps/demo/emails/airbnb-review.tsx
    @@ -11,6 +11,7 @@ import {
       Row,
       Section,
       Text,
    +  render,
     } from "@react-email/components";
     import * as React from "react";
     
    @@ -32,82 +33,99 @@ export const AirbnbReviewEmail = ({
       const previewText = `Read ${authorName}'s review`;
     
       return (
    -    <Html>
    -      <Head />
    -      <Preview>{previewText}</Preview>
    -
    -      <Body style={main}>
    -        <Container style={container}>
    -          <Section>
    -            <Img
    -              src={`${baseUrl}/static/airbnb-logo.png`}
    -              width="96"
    -              height="30"
    -              alt="Airbnb"
    -            />
    -          </Section>
    -          <Section>
    -            <Img
    -              src={authorImage}
    -              width="96"
    -              height="96"
    -              alt={authorName}
    -              style={userImage}
    -            />
    -          </Section>
    -          <Section style={{ paddingBottom: "20px" }}>
    -            <Row>
    -              <Text style={heading}>Here's what {authorName} wrote</Text>
    -              <Text style={review}>{reviewText}</Text>
    -              <Text style={paragraph}>
    -                Now that the review period is over, we’ve posted {authorName}
    -                ’s review to your Airbnb profile.
    -              </Text>
    -              <Text style={{ ...paragraph, paddingBottom: "16px" }}>
    -                While it’s too late to write a review of your own, you can send
    -                your feedback to {authorName} using your Airbnb message thread.
    -              </Text>
    -
    -              <Button style={button} href="https://airbnb.com/">
    -                Send My Feedback
    -              </Button>
    -            </Row>
    -          </Section>
    -
    -          <Hr style={hr} />
    -
    -          <Section>
    -            <Row>
    -              <Text style={{ ...paragraph, fontWeight: "700" }}>
    -                Common questions
    -              </Text>
    -              <Text>
    -                <Link href="https://airbnb.com/help/article/13" style={link}>
    -                  How do reviews work?
    -                </Link>
    -              </Text>
    -              <Text>
    -                <Link href="https://airbnb.com/help/article/1257" style={link}>
    -                  How do star ratings work?
    -                </Link>
    -              </Text>
    -              <Text>
    -                <Link href="https://airbnb.com/help/article/995" style={link}>
    -                  Can I leave a review after 14 days?
    -                </Link>
    -              </Text>
    -              <Hr style={hr} />
    -              <Text style={footer}>
    -                Airbnb, Inc., 888 Brannan St, San Francisco, CA 94103
    -              </Text>
    -              <Link href="https://airbnb.com" style={reportLink}>
    -                Report unsafe behavior
    -              </Link>
    -            </Row>
    -          </Section>
    -        </Container>
    -      </Body>
    -    </Html>
    +    <div
    +      dangerouslySetInnerHTML={{
    +        __html: render(
    +          <Html>
    +            <Head />
    +            <Preview>{previewText}</Preview>
    +
    +            <Body style={main}>
    +              <Container style={container}>
    +                <Section>
    +                  <Img
    +                    src={`${baseUrl}/static/airbnb-logo.png`}
    +                    width="96"
    +                    height="30"
    +                    alt="Airbnb"
    +                  />
    +                </Section>
    +                <Section>
    +                  <Img
    +                    src={authorImage}
    +                    width="96"
    +                    height="96"
    +                    alt={authorName}
    +                    style={userImage}
    +                  />
    +                </Section>
    +                <Section style={{ paddingBottom: "20px" }}>
    +                  <Row>
    +                    <Text style={heading}>Here's what {authorName} wrote</Text>
    +                    <Text style={review}>{reviewText}</Text>
    +                    <Text style={paragraph}>
    +                      Now that the review period is over, we’ve posted{" "}
    +                      {authorName}
    +                      ’s review to your Airbnb profile.
    +                    </Text>
    +                    <Text style={{ ...paragraph, paddingBottom: "16px" }}>
    +                      While it’s too late to write a review of your own, you can
    +                      send your feedback to {authorName} using your Airbnb
    +                      message thread.
    +                    </Text>
    +
    +                    <Button style={button} href="https://airbnb.com/">
    +                      Send My Feedback
    +                    </Button>
    +                  </Row>
    +                </Section>
    +
    +                <Hr style={hr} />
    +
    +                <Section>
    +                  <Row>
    +                    <Text style={{ ...paragraph, fontWeight: "700" }}>
    +                      Common questions
    +                    </Text>
    +                    <Text>
    +                      <Link
    +                        href="https://airbnb.com/help/article/13"
    +                        style={link}
    +                      >
    +                        How do reviews work?
    +                      </Link>
    +                    </Text>
    +                    <Text>
    +                      <Link
    +                        href="https://airbnb.com/help/article/1257"
    +                        style={link}
    +                      >
    +                        How do star ratings work?
    +                      </Link>
    +                    </Text>
    +                    <Text>
    +                      <Link
    +                        href="https://airbnb.com/help/article/995"
    +                        style={link}
    +                      >
    +                        Can I leave a review after 14 days?
    +                      </Link>
    +                    </Text>
    +                    <Hr style={hr} />
    +                    <Text style={footer}>
    +                      Airbnb, Inc., 888 Brannan St, San Francisco, CA 94103
    +                    </Text>
    +                    <Link href="https://airbnb.com" style={reportLink}>
    +                      Report unsafe behavior
    +                    </Link>
    +                  </Row>
    +                </Section>
    +              </Container>
    +            </Body>
    +          </Html>,
    +        ),
    +      }}
    +    ></div>
       );
     };
     
    diff --git a/packages/render/src/index.ts b/packages/render/src/index.ts
    index bffe0c6..60f05bf 100644
    --- a/packages/render/src/index.ts
    +++ b/packages/render/src/index.ts
    @@ -1,4 +1,4 @@
    -export * from "./render";
    +// export * from "./render";
     export * from "./render-async";
     
     export * from "./options";
  2. Create a new temp.patch and paste the contents of the diff

  3. Run pnpm install and pnpm build at the root of the project

  4. Run npx tsx ../../packages/react-email/src/cli/index.ts on ./apps/demo

  5. Open http://localhost:3000/preview/airbnb-review.tsx

  6. Verify that there are no errors similar to what TextDecoder is not defined when running email dev #1258 points out

The patch I show above does the following things for clarification:

  1. Calls our render() function on the usual content for the email
  2. Returns a div with its innerHTML set to be the result of render()
  3. Comments out the export for render() on @react-email/render to avoid errors when running the preview server locally

Copy link

vercel bot commented Jan 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-email ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 4, 2024 4:37pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
react-email-demo ⬜️ Ignored (Inspect) Visit Preview Mar 4, 2024 4:37pm

@gabrielmfern gabrielmfern changed the title fix(react-email): Not being able to call render from inside an email fix(react-email): Missing TextDecoder on the global for email components being rendered Mar 4, 2024
@gabrielmfern gabrielmfern changed the title fix(react-email): Missing TextDecoder on the global for email components being rendered fix(react-email): Missing TextDecoder, TextEncoder and stream variants of these on the global for email components being rendered Mar 4, 2024
bukinoshita
bukinoshita previously approved these changes Mar 4, 2024
vcapretz
vcapretz previously approved these changes Mar 4, 2024
@gabrielmfern gabrielmfern merged commit 20f560b into canary Mar 4, 2024
9 checks passed
@gabrielmfern gabrielmfern deleted the fix/missing-global-text-decoder-on-preview-server branch March 4, 2024 16:48
gabrielmfern added a commit that referenced this pull request Mar 6, 2024
…s of these on the `global` for email components being rendered (#1259)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TextDecoder is not defined when running email dev
4 participants