Provision local HTTPS certificates automatically when running Vite with localHttps - #3953
Conversation
localHttps
frandiox
left a comment
There was a problem hiding this comment.
Nice. I had seen other Vite plugins doing something similar but since we have a core feature like customer accounts, it makes sense we export one 🤔
I'm seeing this but I think it was fixed already in MiniOxygen?
Request must contain a host header.
at toWeb (file:///...hydrogen/node_modules/.pnpm/@shopify+mini-oxygen@4.2.0_vite@8.0.10_@types+node@22.19.17_esbuild@0.28.1_jiti@2.6.1_t_33b61828dcbe09e87499152c791ecdbb/node_modules/@shopify/mini-oxygen/dist/vite/utils.js:13:11)
| try { | ||
| await provisionCertificates(settings); |
There was a problem hiding this comment.
This is a big side effect when running the Vite plugin, and I'm not sure users would notice (it modifies their system to add a certificate).
Would it make sense if we use node:readline/promises or similar to block the process until the user accepts a terminal prompt? (or exit with a nice message otherwise)
That or require a manual setup https command?
Not sure really 🤔
There was a problem hiding this comment.
i designed the API to support a setup step separately (useful for CI, as we skip interactive prompts)
but the DX obsessive in me wants to make it just work
the experience is pretty clear, there are logs all throughout the process, users know what's going on, so i'd say keep it?
- we can add jsdoc to the httpOnly plugin saying that by using this in vite, if enabled it will create a certificate on your machine
- or a prompt to get acceptance first
open to either way
There was a problem hiding this comment.
I love the "it just works" but at the same time, this is modifying the system to install a certificate... I feel this is something we should ask confirmation for. Or at least have more than 1 single step to run it: force them to enable it manually and read a comment on top of it, etc.
Alternatively... is there a command we could print to uninstall the cert we just added? Perhaps that would be good enough if someone can just undo, and give them the command when they run dev:https?
It seems there's mkcert -uninstall, and we could wire it up with extra cleaning via npx hydrogen setup https --uninstall or similar? We might need a different CAROOT for mkcert so that it doesn't uninstall different certs though 🤔
There was a problem hiding this comment.
very interesting
i think i will do both: one confirmation dialog following the "no magic" principle we've been following, and also a way to uninstall, probably npx hydrogen certs uninstall alongside npx hydrogen certs install which just runs the script to install them
There was a problem hiding this comment.
yep, agreed - did both.
vite dev now explains the mkcert download, system trust-store change, and certificate creation before asking for confirmation. Declining makes no system changes and points to the explicit install command.
The commands are now:
npx hydrogen certs installnpx hydrogen certs uninstallremoves Hydrogen's certificate files and cached binary, but leaves mkcert's shared CA trustednpx hydrogen certs uninstall --remove-cawarns about other projects and then uninstalls the exact binary we installed before, not the user's mainmkcert
I kept mkcert's default CAROOT so we reuse an existing mkcert CA rather than installing another root. Removing that shared CA therefore requires the explicit flag.
yeah i fixed it but it's not released yet edit: now fixed in preview, using the correct minioxygen version |
a5581ff to
c9897c6
Compare
Assisted-By: devx/07abca81-b031-4579-88c8-bc07dfd3c5aa
Assisted-By: devx/07abca81-b031-4579-88c8-bc07dfd3c5aa
Assisted-By: devx/07abca81-b031-4579-88c8-bc07dfd3c5aa
Assisted-By: devx/05def498-8bdd-48d0-bd5e-35e4667a2f3d
c9897c6 to
b5b5d11
Compare
TL;DR: Local HTTPS for Vite-based setups required a manual, macOS-specific mkcert setup before Customer Account flows worked. This PR (stacked on #3936) makes provisioning portable across macOS, Linux, and Windows while keeping the system trust change explicit and reversible.
Before
After
What this changes
localHttps()detects missing certificates duringvite devand explains that Hydrogen will download mkcert, modify the system trust store, and create a certificate before asking for confirmation. Declining or running non-interactively leaves HTTPS disabled and prints the explicit installation command.~/.shopify/hydrogen/mkcert/.hydrogen certs installandhydrogen certs uninstallcommands. Uninstall removes Hydrogen's certificate, key, and cached mkcert binary by default.--remove-caadditionally warns and runsmkcert -uninstallbecause that CA may be shared with unrelated projects.provisionLocalHttps()for frameworks that need certificate paths before the Vite plugin runs, including Nuxt and SolidStart.https:setupscript cross-platform and updates the packaged local HTTPS skill and repository guidance.Developer impact
Extends the existing minor changeset for
@shopify/hydrogen. It adds theprovisionLocalHttps()/ProvisionLocalHttpsOptionsexports under@shopify/hydrogen/viteand the paired certificate CLI commands. ExistinglocalHttps()call sites need no changes.Risk
hydrogen certs installinstalls mkcert's local CA into system trust stores and may prompt for a password.certs uninstalltherefore leaves it trusted; removing it requires the explicit--remove-caflag and can make certificates from other projects untrusted.How to Test
mv ~/.shopify/hydrogen ~/.shopify/hydrogen.bak.pnpm install && pnpm build:pkgs.pnpm --filter @shopify/hydrogen-example-astro https:dev.npx hydrogen certs install.https://local.tryhydrogen.dev:5173with a trusted certificate.node packages/hydrogen/bin/hydrogen.mjs certs uninstalland confirm the Hydrogen certificate files are removed while the output says the shared CA remains trusted.rm -rf ~/.shopify/hydrogen && mv ~/.shopify/hydrogen.bak ~/.shopify/hydrogen.