Skip to content

Commit 0d03f1c

Browse files
fix(react-email): Hot reloading not working with custom emails directory (#1231)
Co-authored-by: Bu Kinoshita <[email protected]>
1 parent a289cf7 commit 0d03f1c

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/react-email/src/contexts/emails.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
} from '../actions/get-emails-directory-metadata';
77
import { useHotreload } from '../hooks/use-hot-reload';
88
import {
9+
emailsDirRelativePath,
910
emailsDirectoryAbsolutePath,
11+
normalizePath,
1012
pathSeparator,
1113
} from '../utils/emails-directory-absolute-path';
1214
import {
@@ -70,11 +72,16 @@ export const EmailsProvider = (props: {
7072
});
7173

7274
for (const change of changes) {
75+
const normalizedEmailsDirRelativePath = normalizePath(
76+
emailsDirRelativePath,
77+
);
7378
const slugForChangedEmail =
7479
// filename ex: emails/apple-receipt.tsx
7580
// so we need to remove the "emails/" because it isn't used
7681
// on the slug parameter for the preview page
77-
change.filename.split(pathSeparator).slice(1).join('/');
82+
change.filename
83+
.replace(`${normalizedEmailsDirRelativePath}${pathSeparator}`, '')
84+
.replace(normalizedEmailsDirRelativePath, '');
7885

7986
const lastResult = renderingResultPerEmailSlug[slugForChangedEmail];
8087

packages/react-email/src/utils/emails-directory-absolute-path.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2-
const emailsDirRelativePath =
2+
export const emailsDirRelativePath =
33
process.env.NEXT_PUBLIC_EMAILS_DIR_RELATIVE_PATH ?? 'emails';
44

55
export const userProjectLocation =
@@ -11,7 +11,7 @@ export const pathSeparator = process.env.NEXT_PUBLIC_OS_PATH_SEPARATOR! as
1111
| '/'
1212
| '\\';
1313

14-
const normalizePath = (path: string) => {
14+
export const normalizePath = (path: string) => {
1515
let newPath = path;
1616

1717
while (newPath.startsWith('./')) {

0 commit comments

Comments
 (0)