-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react-email): Preview server crashing without React 19 (#1861)
- Loading branch information
1 parent
3b8aada
commit c6fcd94
Showing
12 changed files
with
153 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"react-email": patch | ||
--- | ||
|
||
Fix preview server crashing without React 19 |
19 changes: 19 additions & 0 deletions
19
packages/react-email/src/actions/get-emails-directory-metadata-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use server'; | ||
|
||
import type { EmailsDirectory } from '../utils/get-emails-directory-metadata'; | ||
import { getEmailsDirectoryMetadata } from '../utils/get-emails-directory-metadata'; | ||
|
||
export const getEmailsDirectoryMetadataAction = async ( | ||
absolutePathToEmailsDirectory: string, | ||
keepFileExtensions = false, | ||
isSubDirectory = false, | ||
|
||
baseDirectoryPath = absolutePathToEmailsDirectory, | ||
): Promise<EmailsDirectory | undefined> => { | ||
return getEmailsDirectoryMetadata( | ||
absolutePathToEmailsDirectory, | ||
keepFileExtensions, | ||
isSubDirectory, | ||
baseDirectoryPath, | ||
); | ||
}; |
123 changes: 0 additions & 123 deletions
123
packages/react-email/src/actions/get-emails-directory-metadata.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/react-email/src/utils/get-emails-directory-metadata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
const isFileAnEmail = (fullPath: string): boolean => { | ||
const stat = fs.statSync(fullPath); | ||
|
||
if (stat.isDirectory()) return false; | ||
|
||
const { ext } = path.parse(fullPath); | ||
|
||
if (!['.js', '.tsx', '.jsx'].includes(ext)) return false; | ||
|
||
// This is to avoid a possible race condition where the file doesn't exist anymore | ||
// once we are checking if it is an actual email, this couuld cause issues that | ||
// would be very hard to debug and find out the why of it happening. | ||
if (!fs.existsSync(fullPath)) { | ||
return false; | ||
} | ||
|
||
// check with a heuristic to see if the file has at least | ||
// a default export | ||
const fileContents = fs.readFileSync(fullPath, 'utf8'); | ||
|
||
return /\bexport\s+default\b/gm.test(fileContents); | ||
}; | ||
|
||
export interface EmailsDirectory { | ||
absolutePath: string; | ||
relativePath: string; | ||
directoryName: string; | ||
emailFilenames: string[]; | ||
subDirectories: EmailsDirectory[]; | ||
} | ||
|
||
const mergeDirectoriesWithSubDirectories = ( | ||
emailsDirectoryMetadata: EmailsDirectory, | ||
): EmailsDirectory => { | ||
let currentResultingMergedDirectory: EmailsDirectory = | ||
emailsDirectoryMetadata; | ||
|
||
while ( | ||
currentResultingMergedDirectory.emailFilenames.length === 0 && | ||
currentResultingMergedDirectory.subDirectories.length === 1 | ||
) { | ||
const onlySubDirectory = currentResultingMergedDirectory.subDirectories[0]!; | ||
currentResultingMergedDirectory = { | ||
...onlySubDirectory, | ||
directoryName: path.join( | ||
currentResultingMergedDirectory.directoryName, | ||
onlySubDirectory.directoryName, | ||
), | ||
}; | ||
} | ||
|
||
return currentResultingMergedDirectory; | ||
}; | ||
|
||
export const getEmailsDirectoryMetadata = async ( | ||
absolutePathToEmailsDirectory: string, | ||
keepFileExtensions = false, | ||
isSubDirectory = false, | ||
|
||
baseDirectoryPath = absolutePathToEmailsDirectory, | ||
): Promise<EmailsDirectory | undefined> => { | ||
if (!fs.existsSync(absolutePathToEmailsDirectory)) return; | ||
|
||
const dirents = await fs.promises.readdir(absolutePathToEmailsDirectory, { | ||
withFileTypes: true, | ||
}); | ||
|
||
const emailFilenames = dirents | ||
.filter((dirent) => | ||
isFileAnEmail(path.join(absolutePathToEmailsDirectory, dirent.name)), | ||
) | ||
.map((dirent) => | ||
keepFileExtensions | ||
? dirent.name | ||
: dirent.name.replace(path.extname(dirent.name), ''), | ||
); | ||
|
||
const subDirectories = await Promise.all( | ||
dirents | ||
.filter( | ||
(dirent) => | ||
dirent.isDirectory() && | ||
!dirent.name.startsWith('_') && | ||
dirent.name !== 'static', | ||
) | ||
.map((dirent) => { | ||
const direntAbsolutePath = path.join( | ||
absolutePathToEmailsDirectory, | ||
dirent.name, | ||
); | ||
|
||
return getEmailsDirectoryMetadata( | ||
direntAbsolutePath, | ||
keepFileExtensions, | ||
true, | ||
baseDirectoryPath, | ||
) as Promise<EmailsDirectory>; | ||
}), | ||
); | ||
|
||
const emailsMetadata = { | ||
absolutePath: absolutePathToEmailsDirectory, | ||
relativePath: path.relative( | ||
baseDirectoryPath, | ||
absolutePathToEmailsDirectory, | ||
), | ||
directoryName: absolutePathToEmailsDirectory.split(path.sep).pop()!, | ||
emailFilenames, | ||
subDirectories, | ||
} satisfies EmailsDirectory; | ||
|
||
return isSubDirectory | ||
? mergeDirectoriesWithSubDirectories(emailsMetadata) | ||
: emailsMetadata; | ||
}; |
c6fcd94
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-email-demo – ./apps/demo
react-email-demo.vercel.app
react-email-demo-resend.vercel.app
react-email-demo-git-main-resend.vercel.app
demo.react.email