Replies: 1 comment 2 replies
-
Now that Remix has moved to Vite, you can do a similar thing with import { serverOnly$ } form 'vite-env-only'
export const handle = {
getSiteMapEntries: serverOnly$(async () => {})
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Proposal: Introducing
export const serverHandle
in RemixBackground
In Remix, exporting a
handle
allows developers to create application conventions that can be utilized with theuseMatches
hook, providing a flexible way to attach metadata or methods to routes. However, this capability currently does not differentiate between server-side and client-side code, potentially leading to unintended exposure of server-only logic in the client bundle. This poses a problem, especially when handles are used for server-side logic such as accessing databases or APIs that should not be part of the client-side codebase.A notable example can be found in Kent C. Dodds's epic-stack, where
export const handle
is used for generating sitemap entries. This server-side logic, ideally, should not be included in the client bundle to prevent unnecessary code exposure and potential errors.To address this limitation, this proposal suggests introducing a new
export const serverHandle
that follows the same removal principle as other server-only exports likeloader
andaction
, ensuring that any server-side logic defined withinserverHandle
is stripped from the client bundle.Use Case
The primary use case for
export const serverHandle
is to allow developers to define server-side logic associated with routes without worrying about this logic leaking into the client-side bundle. This is particularly useful for:By ensuring that
serverHandle
is removed from the client bundle, we can maintain a clear separation of concerns between client and server, optimize bundle size, and enhance application security.Proposal Details
Introduction of
serverHandle
For implementation details on how the handle is used in this particular case please see @balavishnuvj/remix-seo
This
serverHandle
would be automatically stripped from the client bundle, similar to howexport const loader
andexport const action
are currently handled. This ensures that server-side code is not exposed or shipped to the client, aligning with best practices for security and performance.Integration with
SERVER_ONLY_ROUTE_EXPORTS
To implement this feature,
serverHandle
would be added to the list ofSERVER_ONLY_ROUTE_EXPORTS
within Remix's build process. This would automatically excludeserverHandle
from the client bundle during the build phase, ensuring that server-side logic remains server-side.Conclusion
The introduction of
export const serverHandle
in Remix offers a robust solution for defining server-side logic associated with routes, enhancing code organization, security, and performance by ensuring server-side code is not included in the client bundle. This proposal aims to address the current limitations and provide a more flexible and secure way of handling server-side logic in Remix applications.Beta Was this translation helpful? Give feedback.
All reactions