Skip to content

Fix npm package worker resolution in Vite v6 / 2025.1.3+#2840

Closed
juanpprieto wants to merge 23 commits intomainfrom
juanpprieto/fix-vite-6-ssr-worker-resolution
Closed

Fix npm package worker resolution in Vite v6 / 2025.1.3+#2840
juanpprieto wants to merge 23 commits intomainfrom
juanpprieto/fix-vite-6-ssr-worker-resolution

Conversation

@juanpprieto
Copy link
Copy Markdown
Contributor

@juanpprieto juanpprieto commented Apr 4, 2025

Context:
https://shopifypartners.slack.com/archives/C02F94JC3QC/p1743413107901139
sanity-io/hydrogen-sanity#116
sanity-io/visual-editing#1085

Vite 6 (2025.1.3+) seems to have a different more strict NPM module resolution logic for worker exports. This PR aims to fix this by adding an additional resolve conditions for workers in the SSR option.

Looking for feedback

For those having this issue with 2025.1.3+

Could you try this draft mini-oxygen version?

`"@shopify/mini-oxygen": "0.0.0-snapshot-20250408225709"`

P.S - when tophating a codebase with hydrogen-sanity you might need to npm i -force because the sanity does not allow snapshot versions of hydrogen.

Important

make sure you comment out @nkgentile previous vite.config solution if you implemented it to make sure the test results are not skewed

ssr: 
  // ...existing SSR configuration
   
  resolve: {
    conditions: ['worker'],
  }
}

🎩 Top** hat instructions:

  1. Clone hydrogen-sanity-example
git clone https://github.com/juanpprieto/hydrogen-sanity-example
  1. Follow README tophat instructions in the repo

@shopify
Copy link
Copy Markdown
Contributor

shopify bot commented Apr 4, 2025

Oxygen deployed a preview of your juanpprieto/fix-vite-6-ssr-worker-resolution branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment April 14, 2025 7:46 PM
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment April 14, 2025 7:46 PM
classic-remix ✅ Successful (Logs) Preview deployment Inspect deployment April 14, 2025 7:46 PM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment April 14, 2025 7:46 PM
metaobjects ✅ Successful (Logs) Preview deployment Inspect deployment April 14, 2025 7:46 PM

Learn more about Hydrogen's GitHub integration.

@juanpprieto
Copy link
Copy Markdown
Contributor Author

/snapit

@Shopify Shopify deleted a comment from github-actions bot Apr 4, 2025
@juanpprieto
Copy link
Copy Markdown
Contributor Author

/snapit

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 4, 2025

🫰✨ Thanks @juanpprieto! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

"@shopify/cli-hydrogen": "0.0.0-snapshot-20250404162914",
"@shopify/hydrogen": "0.0.0-snapshot-20250404162914",
"@shopify/mini-oxygen": "0.0.0-snapshot-20250404162914"

Create a new project with all the released packages running npm create @shopify/hydrogen@<snapshot_version>
To try a new CLI plugin version, add @shopify/cli-hydrogen as a dependency to your project using the snapshot version.

@juanpprieto
Copy link
Copy Markdown
Contributor Author

/snapit

@juanpprieto juanpprieto marked this pull request as ready for review April 8, 2025 19:03
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2025

🫰✨ Thanks @juanpprieto! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

"@shopify/cli-hydrogen": "0.0.0-snapshot-20250408190305",
"@shopify/hydrogen": "0.0.0-snapshot-20250408190305",
"@shopify/mini-oxygen": "0.0.0-snapshot-20250408190305"

Create a new project with all the released packages running npm create @shopify/hydrogen@<snapshot_version>
To try a new CLI plugin version, add @shopify/cli-hydrogen as a dependency to your project using the snapshot version.

@juanpprieto juanpprieto changed the title Fix ssr worker resolve for vite 6 Fix npm package worker resolution in Vite v6 / 2025.1.3+ Apr 8, 2025
@juanpprieto
Copy link
Copy Markdown
Contributor Author

/snapit

@juanpprieto juanpprieto requested review from Copilot and frandiox April 8, 2025 21:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

packages/hydrogen/src/vite/plugin.ts:64

  • The condition 'workerd' may be a typo unless it is intentionally targeting a specific non-standard environment. Please confirm if 'workerd' is required or if it should be removed.
conditions: ['workerd', 'worker'], // Recommended earlier, ideally combined with these settings

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2025

🫰✨ Thanks @juanpprieto! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

"@shopify/cli-hydrogen": "0.0.0-snapshot-20250408212515",
"@shopify/hydrogen": "0.0.0-snapshot-20250408212515"

Create a new project with all the released packages running npm create @shopify/hydrogen@<snapshot_version>
To try a new CLI plugin version, add @shopify/cli-hydrogen as a dependency to your project using the snapshot version.

@juanpprieto
Copy link
Copy Markdown
Contributor Author

/snapit

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2025

🫰✨ Thanks @juanpprieto! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

"@shopify/cli-hydrogen": "0.0.0-snapshot-20250408225709",
"@shopify/hydrogen": "0.0.0-snapshot-20250408225709",
"@shopify/mini-oxygen": "0.0.0-snapshot-20250408225709"

Create a new project with all the released packages running npm create @shopify/hydrogen@<snapshot_version>
To try a new CLI plugin version, add @shopify/cli-hydrogen as a dependency to your project using the snapshot version.

ssr: {
noExternal: true,
target: 'webworker',
resolve: {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actual fix

@balazsbajorics
Copy link
Copy Markdown
Contributor

Seems like this breaks the skeleton store:

image
│                                                                              │
│  In Hydrogen's `createStorefrontClient`:                                     │
│                                                                              │
│  `storeDomain` is required when creating a new Storefront client in          │
│  production.                                                                 │
│                                                                              │
│  To investigate the issue, examine this stack trace:                         │
│    at createAppLoadContext (app/lib/context.ts:27)                           │
│      const hydrogenContext = createHydrogenContext({                         │
│    at fetch (server.ts:20)                                                   │
│      const appLoadContext = await createAppLoadContext(                      │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯

Steps to reproduce:
First method:
npx shopify@latest hydrogen init
then change the @shopify/mini-oxygen dependency to 0.0.0-snapshot-20250408225709
and run npm run dev in a store which defaults to mock.shop instead of having a .env file
If I try to open localhost:3000 it throws this error

Second method:
I checked out your branch, ran npx turbo daemon stop; git clean -fdX && npm install && npm run build && npm run dev and then went to /templates/skeleton and npm run dev there throws the same error

@juanpprieto
Copy link
Copy Markdown
Contributor Author

/snapit

@github-actions
Copy link
Copy Markdown
Contributor

🫰✨ Thanks @juanpprieto! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

"@shopify/cli-hydrogen": "0.0.0-snapshot-20250414204700",
"@shopify/hydrogen": "0.0.0-snapshot-20250414204700",
"@shopify/mini-oxygen": "0.0.0-snapshot-20250414204700"

Create a new project with all the released packages running npm create @shopify/hydrogen@<snapshot_version>
To try a new CLI plugin version, add @shopify/cli-hydrogen as a dependency to your project using the snapshot version.

Copy link
Copy Markdown
Contributor

@frandiox frandiox left a comment

Choose a reason for hiding this comment

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

Hey 👋 I mentioned this to Bret already but here are some thoughts:
Vite 6 changed how resolve.conditions work. Here's the migration guide: https://vite.dev/guide/migration.html#default-value-for-resolve-conditions

To keep the same behavior, I think you would need to add resolve.conditions: ['worker', 'workerd', ...defaultClientConditions] and ssr.resolve.conditions: ['worker', 'workerd', ...defaultClientConditions], doing import {defaultClientConditions} from 'vite'.

workerd is likely not required (it was added just in case), but worker should still be added in front of other conditions. This will make sure you end up importing the "worker" entry point for libraries that specify it, like here (otherwise it would likely get browser.import, which is a different entry file).


For the record, Vite versions before 6 were using resolve.conditions also as ssr.resolve.conditions and adding the default values automatically (module, browser, prod/dev, etc.). However, Vite 6 requires you to do all of this manually as mentioned above.

@juanpprieto
Copy link
Copy Markdown
Contributor Author

Replaced by #2863

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants