fix(react-email): Absolute paths when trying to click emails on sub directories in the Sidebar #1356
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is the issue?
When running the Next app we do a few path transformations by hand, as they run in the browser, and
browsers doesn't provide us with something like a
path
module. This means that we do stuff like making a pathrelative to another by doing replacements.
Here's an example of making a path relative and of where the issue starts:
react-email/packages/react-email/src/components/sidebar/sidebar-directory.tsx
Lines 31 to 35 in 2e16a60
This path replacement would fail because this absolute path is determined based on the user's project location:
react-email/packages/react-email/src/utils/emails-directory-absolute-path.ts
Lines 30 to 34 in 2e16a60
And since this path is being passed in without modifications to the Next app, through environment variables,
what happens is that the
emailsDirectoryAbsolutePath
becomes something likeD:\email-server\src/emails
which does not work when replacing as the paths, that come from the server action to get all the email templates metadata,
will start with
D:\email-server\src\emails
.This is the same as #1354.
How does this fix the issue?
Before passing in the environment variable for the relative path to the user's emails directory, we run a
path.normalize
on it which ensures that the forward slashes are converted to backward slashes meaning the issues goes away.
This changes both on the
build
command and on thedev
command since them both can be affected by this:react-email/packages/react-email/src/cli/utils/preview/start-dev-server.ts
Lines 116 to 126 in 2e16a60
react-email/packages/react-email/src/cli/commands/build.ts
Lines 51 to 59 in 2e16a60
How can I make sure this is fixed?
You can, on Windows, do the following:
react-email/packages/render/src/index.ts
Lines 1 to 4 in 2e16a60
render
on NextJS 14 which can only be avoided locally by doing thisturbo build
inside./packages/react-email
to build all the dependencies it has./apps/demo/emails
to./apps/demo/src/emails
npx tsx ../../packages/react-email/src/cli/index.ts dev -d ./src/emails
http://localhost:3000
I've also tested this on a Windows machine of mine and can confirm this works.