Skip to content

Commit 98952a5

Browse files
gabrielmfernbukinoshita
authored andcommitted
feat(docs): More info on the email dev command (#1283)
1 parent 57f8808 commit 98952a5

File tree

1 file changed

+117
-3
lines changed

1 file changed

+117
-3
lines changed

β€Žapps/docs/cli.mdxβ€Ž

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,123 @@ icon: 'square-terminal'
88

99
## `email dev`
1010

11-
Starts a local development server that will watch your files and automatically rebuild your email when you make changes.
11+
Starts a local development server that will watch your files and automatically rebuild
12+
your email template when you make changes.
1213

13-
**Options**
14+
### Where can I place my static files for previewing?
15+
16+
Almost always you will need to have static files in your emails, and seeing
17+
them on the preview server without having to first host on a CDN is very helpful.
18+
19+
We do allow for this, and currently, you can place your files inside a `static`
20+
directory inside your `emails` directory.
21+
22+
This does adjust to your `--dir` option, so if your `emails` directory was inside
23+
`./src/emails`, you would place your static files inside `./src/emails/static`.
24+
25+
These static files are directly served from our preview server by looking into the
26+
requests made into it that end with `/static` (i.e. `http://localhost:3000/static/...`) and serving the files at that point,
27+
this also allows for you to have images inside your emails like so:
28+
29+
```jsx
30+
export default function Email(props) {
31+
return (
32+
<div>
33+
<img src="/static/email-logo.png" />
34+
</div>
35+
)
36+
}
37+
```
38+
39+
<Info>
40+
This does not mean your images are hosted for you to send the emails.
41+
42+
If you do send the rendered email, and you are trying to link to an image,
43+
or some other asset inside `emails/static`, they will not load on the email that was sent.
44+
45+
We recommend that you use a different source link to your files depending on whether you're
46+
running in production or not. Here's an example
47+
48+
```jsx
49+
const baseURL = process.env.NODE_ENV === 'production'
50+
? 'https://cdn.com'
51+
: ''
52+
53+
export default function Email(props) {
54+
return (
55+
<div>
56+
<img src={`${baseURL}/static/email-logo.png`} />
57+
</div>
58+
)
59+
}
60+
```
61+
You can refer to our [demo emails source code](https://demo.react.email/preview/vercel-invite-user.tsx?view=source)
62+
for an example of how we do this with our demo deploy on Vercel.
63+
</Info>
64+
65+
### How can I define props specific to the email's preview?
66+
67+
Considering that you are already default exporting the React component
68+
that will render as your email template, you can just define a `PreviewProps` with it as follows:
69+
70+
```jsx Email template
71+
export default function Email(props) {
72+
return (
73+
<div>
74+
<a src={props.source}>click here if you want candy πŸ‘€</a>
75+
</div>
76+
)
77+
}
78+
79+
Email.PreviewProps = {
80+
source: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
81+
}
82+
```
83+
84+
And then, when opening this email's preview, the `PreviewProps` will be used as props into Email.
85+
So, in a nutshell, it will render the same as if you were to do:
86+
87+
```jsx Another file
88+
import Email from './path-to-my-email';
89+
90+
<Email {...Email.PreviewProps} />
91+
```
92+
93+
### How to make the preview server ignore directories?
94+
95+
Once the preview server has started and is now open on `localhost`, the preview server
96+
reads recursively down into all of your files and directories. This can be disabled
97+
from a directory down by prefixing it with `_`, e.g. `components -> _components`.
98+
99+
So if you wanted to make components for your emails, you could have a file structure as follows:
100+
101+
```bash
102+
my-project
103+
β”œβ”€β”€ emails
104+
β”‚ β”œβ”€β”€ _components
105+
β”‚ β”‚ └── this-is-not-going-to-appear-in-the-sidebar.tsx
106+
β”‚ β”œβ”€β”€ email.tsx
107+
β”‚ └── static
108+
β”œβ”€β”€ package.json
109+
└── tsconfig.json
110+
```
111+
112+
Then the only file that will be shown on the preview server is going to be your `email.tsx`.
113+
114+
### The heuristics for files to be considered emails
115+
116+
To avoid uncanny files appearing in the sidebar of the preview server,
117+
we account for two heuristics to determine weather or not we should
118+
include it:
119+
120+
1. If a file has `.js, .jsx or .tsx` as their file extension
121+
2. If the file contains a `export default` expression by matching with the regex<br/>
122+
`/\bexport\s*default\b/gm`
123+
124+
These can certainly fail as they are only heuristics, so if you do find
125+
any issues with these, feel free to open an [issue](https://github.com/resend/react-email/issues).
126+
127+
### Options
14128

15129
<ResponseField name="--dir" type="string" default="emails">
16130
Change the directory of your email templates.
@@ -32,7 +146,7 @@ Copies the preview app for onto `.react-email` and builds it.
32146

33147
## `email start`
34148

35-
Runs the built preview app that is inside of `.react-email`.
149+
Runs the built preview app that is inside `.react-email`.
36150

37151
## `email export`
38152

0 commit comments

Comments
Β (0)