Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@netlify/remix-edge-adapter): support Hydrogen Vite sites #441

Conversation

serhalp
Copy link
Contributor

@serhalp serhalp commented Aug 30, 2024

Description

It appears to be (nearly?) impossible to provide an out-of-the-box Netlify experience for Hydrogen sites that use Remix Vite. This is in part because our Remix Vite (edge) adapter builds an SSR entrypoint that is not compatible with Hydrogen Vite. It seems impossible (or at least very difficult) to provide an alternative Hydrogen SSR entrypoint due to the directionality of configuration (I won't get into that rabbit hole here, but user-land code, framework-land code, meta-framework-land code, and platform-land that all need to interact in complex ways and our Remix adapter approach just doesn't play nicely with this flow at all.)

We may be able to solve for this at some point, but for now our only option is to expect these sites to contain a server.ts (or similar) file at the root and use that as a custom SSR entrypoint. Hydrogen templates include such a file and so will the Netlify Hydrogen template, so this seems ok, though a bit on the magical side. I did make sure to fail the build with an actionable error for misconfigured Hydrogen sites.

This also seems to be in line with Remix's philosophy: https://remix.run/docs/en/main/guides/deployment. 🤷🏼‍♂️

(There may be value in supporting this for non-Hydrogen Remix sites as well, but I haven't heard of a use case so I didn't bother supporting it here.)

Refactors

I also refactored a bit in a separate commit. I mostly improved naming and inline documentation. This space is very complicated.

Future improvements

As a next step, we could probably trim down our template's server.ts by about half or so by providing helper functions from this package (or a new Hydrogen package) that abstract out the platform-specific parts. Ended up doing this in this PR

Related Tickets & Documents

See netlify/hydrogen-template#4.

QA Instructions, Screenshots, Recordings

I added e2e tests for a working Hydrogen Vite site (from the updated template in netlify/hydrogen-template#4, with a bit of fat trimmed) and a misconfigured one without a server.ts. I've also done extensive testing. This repo also already has existing Remix e2e tests.

"Entrypoint" is a loaded term here with multiple possible meanings, as
is "server". Rename for clarity and consistency with Remix naming.
@serhalp serhalp added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Aug 30, 2024
@serhalp serhalp requested a review from a team as a code owner August 30, 2024 20:47
Copy link

netlify bot commented Aug 30, 2024

Deploy Preview for remix-serverless ready!

Name Link
🔨 Latest commit aaa0903
🔍 Latest deploy log https://app.netlify.com/sites/remix-serverless/deploys/66d9f155cc116b00082aae1b
😎 Deploy Preview https://deploy-preview-441--remix-serverless.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Aug 30, 2024

Deploy Preview for remix-edge ready!

Name Link
🔨 Latest commit aaa0903
🔍 Latest deploy log https://app.netlify.com/sites/remix-edge/deploys/66d9f155ca2baf00089528e3
😎 Deploy Preview https://deploy-preview-441--remix-edge.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@serhalp serhalp force-pushed the serhalp/frp-1264-hydrogen-sites-cannot-work-on-netlify-when-using-remix-vite branch 2 times, most recently from 45ac950 to 3863ddd Compare August 30, 2024 20:59
It appears to be (nearly?) imposssible to provide an out-of-the-box Netlify
experience for Hydrogen sites that use Remix Vite. We may be able to
solve for this at some point, but for now our only option is to expect
these sites to contain a `server.ts` (or similar) file at the root. This
is what Hydrogen templates do and what the Netlify Hydrogen template
will do.

There may be value in supporting this for Remix sites as well, but I
haven't heard of a use case so I didn't bother supporting it here.
@serhalp serhalp force-pushed the serhalp/frp-1264-hydrogen-sites-cannot-work-on-netlify-when-using-remix-vite branch from 3863ddd to e1ffb39 Compare August 30, 2024 21:04
@pieh
Copy link
Contributor

pieh commented Sep 2, 2024

Just checking about Hydrogen and non-edge variant - this part still doesn't work, correct? Should we maybe add checks to non-edge variant to fail with actionable message to use edge variant? (and if so, this doesn't need to be part of this PR and could be another item for "Future improvements")

@serhalp
Copy link
Contributor Author

serhalp commented Sep 3, 2024

Just checking about Hydrogen and non-edge variant - this part still doesn't work, correct? Should we maybe add checks to non-edge variant to fail with actionable message to use edge variant? (and if so, this doesn't need to be part of this PR and could be another item for "Future improvements")

Yep, great idea. Once we've reached a good mergeable point with all this, I'd love to look at this again from the fresh perspective of someone trying to deploy a Hydrogen site to Netlify from the Hydrogen docs and make sure we're giving actionable pointers wherever we can.

@serhalp serhalp force-pushed the serhalp/frp-1264-hydrogen-sites-cannot-work-on-netlify-when-using-remix-vite branch from 6aeaf11 to f3fa359 Compare September 5, 2024 13:49
Comment on lines +236 to +238
return () => {
if (isHydrogenSite && !viteDevServer.config.server.middlewareMode) {
viteDevServer.middlewares.use(async (nodeReq, nodeRes, next) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some Hydrogen utils expect crypto to be available on global. Because for dev we run in Node this will be causing errors on node@18 (Node only added crypto to global in Node@19 without --experimental-global-webcrypto toggle - see https://nodejs.org/api/globals.html#crypto_1 -> expand "History")

If we want to "polyfill" this we could maybe do this:

Suggested change
return () => {
if (isHydrogenSite && !viteDevServer.config.server.middlewareMode) {
viteDevServer.middlewares.use(async (nodeReq, nodeRes, next) => {
return async () => {
if (isHydrogenSite && !viteDevServer.config.server.middlewareMode) {
if (!globalThis.crypto) {
globalThis.crypto = (await import('node:crypto')).webcrypto
}
viteDevServer.middlewares.use(async (nodeReq, nodeRes, next) => {

Otherwise we would require either using handling that OR say to use at least node@19 instead of 18.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could include a note about this in the Troubleshooting section I added to the README in netlify/hydrogen-template#4?

@serhalp serhalp merged commit 81e9bfa into main Sep 5, 2024
15 checks passed
@serhalp serhalp deleted the serhalp/frp-1264-hydrogen-sites-cannot-work-on-netlify-when-using-remix-vite branch September 5, 2024 18:07
@token-generator-app token-generator-app bot mentioned this pull request Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants