File tree Expand file tree Collapse file tree 5 files changed +55
-7
lines changed
cli/utils/preview/hot-reloading Expand file tree Collapse file tree 5 files changed +55
-7
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " react-email " : patch
3
+ ---
4
+
5
+ Fix non-email files being rendered during hot reloading
Original file line number Diff line number Diff line change @@ -28,7 +28,15 @@ export const setupHotreloading = async (
28
28
const reload = debounce ( ( ) => {
29
29
// we detect these using the useHotreload hook on the Next app
30
30
clients . forEach ( ( client ) => {
31
- client . emit ( 'reload' , changes ) ;
31
+ client . emit (
32
+ 'reload' ,
33
+ changes . filter ( ( change ) =>
34
+ // Ensures only changes inside the emails directory are emitted
35
+ path
36
+ . resolve ( absolutePathToEmailsDirectory , change . filename )
37
+ . startsWith ( absolutePathToEmailsDirectory ) ,
38
+ ) ,
39
+ ) ;
32
40
} ) ;
33
41
34
42
changes = [ ] ;
Original file line number Diff line number Diff line change @@ -23,20 +23,24 @@ export const useEmailRenderingResult = (
23
23
// eslint-disable-next-line react-hooks/rules-of-hooks
24
24
useHotreload ( async ( changes ) => {
25
25
for await ( const change of changes ) {
26
- const slugForChangedFile =
26
+ const relativePathForChangedFile =
27
27
// ex: apple-receipt.tsx
28
28
// it will be the path relative to the emails directory, so it is already
29
29
// going to be equivalent to the slug
30
30
change . filename ;
31
31
32
32
if (
33
- containsEmailTemplate ( slugForChangedFile , emailsDirectoryMetadata )
33
+ ! containsEmailTemplate (
34
+ relativePathForChangedFile ,
35
+ emailsDirectoryMetadata ,
36
+ )
34
37
) {
35
38
continue ;
36
39
}
37
40
38
- const pathForChangedEmail =
39
- await getEmailPathFromSlug ( slugForChangedFile ) ;
41
+ const pathForChangedEmail = await getEmailPathFromSlug (
42
+ relativePathForChangedFile ,
43
+ ) ;
40
44
41
45
const newRenderingResult = await renderEmailByPath (
42
46
pathForChangedEmail ,
Original file line number Diff line number Diff line change 1
1
import path from 'node:path' ;
2
- import { containsEmailTemplate } from './contains-email-template' ;
2
+ import {
3
+ containsEmailTemplate ,
4
+ removeFilenameExtension ,
5
+ } from './contains-email-template' ;
6
+
7
+ describe ( 'removeFilenameExtension()' , async ( ) => {
8
+ it ( 'should work with a single .' , ( ) => {
9
+ expect ( removeFilenameExtension ( 'email-template.tsx' ) ) . toBe (
10
+ 'email-template' ,
11
+ ) ;
12
+ } ) ;
13
+
14
+ it ( 'should work with an example test file' , ( ) => {
15
+ expect ( removeFilenameExtension ( 'email-template.spec.tsx' ) ) . toBe (
16
+ 'email-template.spec' ,
17
+ ) ;
18
+ } ) ;
19
+
20
+ it ( 'should do nothing when there is no extension' , ( ) => {
21
+ expect ( removeFilenameExtension ( 'email-template' ) ) . toBe ( 'email-template' ) ;
22
+ } ) ;
23
+ } ) ;
3
24
4
25
test ( 'containsEmailTemplate()' , async ( ) => {
5
26
const emailsDirectoryPath = path . resolve (
Original file line number Diff line number Diff line change 1
1
import type { EmailsDirectory } from './get-emails-directory-metadata' ;
2
2
3
+ export const removeFilenameExtension = ( filename : string ) : string => {
4
+ const parts = filename . split ( '.' ) ;
5
+
6
+ if ( parts . length > 1 ) {
7
+ return parts . slice ( 0 , - 1 ) . join ( '.' ) ;
8
+ }
9
+
10
+ return filename ;
11
+ } ;
12
+
3
13
export const containsEmailTemplate = (
4
14
relativeEmailPath : string ,
5
15
directory : EmailsDirectory ,
@@ -9,7 +19,7 @@ export const containsEmailTemplate = (
9
19
. split ( '/' )
10
20
. filter ( Boolean ) ;
11
21
if ( remainingSegments . length === 1 ) {
12
- const emailFilename = remainingSegments [ 0 ] ! ;
22
+ const emailFilename = removeFilenameExtension ( remainingSegments [ 0 ] ! ) ;
13
23
return directory . emailFilenames . includes ( emailFilename ) ;
14
24
}
15
25
const subDirectory = directory . subDirectories . find (
You can’t perform that action at this time.
0 commit comments