Skip to content

Fix custom SSR build input with serverBundles #13107

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

Merged
merged 10 commits into from
Feb 26, 2025

Conversation

markdalgleish
Copy link
Member

Fixes #13104.

This fixes two issues:

  • Since moving to the Vite Environment API, if a custom build.rollupOptions.input value was provided in the SSR build, this was not honoured when serverBundles were configured. This PR ensures that all SSR builds honour this value.
  • Since the custom server entry imports virtual:react-router/server-build, this fails to account for the query string used to support serverBundles (e.g. virtual:react-router/server-build?route-ids=foo,bar,baz. This PR alters the logic for getting the route IDs of a server bundle build so they're no longer encoded in the query string. Instead, the server bundle ID is extracted from the environment name, and this is then used to look up its routes in the React Router build manifest.

Copy link

changeset-bot bot commented Feb 25, 2025

🦋 Changeset detected

Latest commit: 7fea63d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@react-router/dev Patch
@react-router/fs-routes Patch
@react-router/remix-routes-option-adapter Patch
create-react-router Patch
react-router Patch
react-router-dom Patch
@react-router/architect Patch
@react-router/cloudflare Patch
@react-router/express Patch
@react-router/node Patch
@react-router/serve Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

"bundle-a._index.tsx",
"bundle-a.route-a.tsx",
"bundle-a.route-b.tsx",
"bundle_a.tsx",
Copy link
Member Author

Choose a reason for hiding this comment

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

As noted in the changeset, we needed to replace hyphens with underscores in our server bundle IDs since we're also testing against the Vite Environment API. This is technically a breaking change but unlikely to break anyone in practice, and only those who have opted into an unstable API.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this something the Vercel preset will need to do as well? i.e. currently it generates a bundle ID such as nodejs-eyJydW50aW1lIjoibm9kZWpzIn0. Does that need to be nodejs_eyJydW50aW1lIjoibm9kZWpzIn0 instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah nice catch, I didn't realise this. Yep, swapping for an underscore now is a good idea.

Comment on lines +3326 to +3330
input:
(ctx.reactRouterConfig.future.unstable_viteEnvironmentApi
? viteUserConfig.environments?.ssr?.build?.rollupOptions?.input
: viteUserConfig.build?.rollupOptions?.input) ??
virtual.serverBuild.id,
Copy link
Member Author

Choose a reason for hiding this comment

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

Since the route-ids query string is no longer used, this config is now common across all SSR builds including server bundles.

@@ -184,15 +183,18 @@ async function viteBuild(
optimizeDeps: { force },
clearScreen,
logLevel,
...{ __reactRouterEnvironmentBuildContext: environmentBuildContext },
...{
__reactRouterPluginContext: ctx,
Copy link
Member Author

Choose a reason for hiding this comment

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

We now pass the resolved plugin context object into each build so we can re-use the build manifest rather than re-evaluating the serverBundles function whenever our Vite plugin is instantiated.

@markdalgleish markdalgleish merged commit 58439e3 into dev Feb 26, 2025
8 checks passed
@markdalgleish markdalgleish deleted the markdalgleish/server-bundles-custom-ssr-build-input branch February 26, 2025 22:46
github-merge-queue bot pushed a commit to vercel/vercel that referenced this pull request Feb 27, 2025
…ith Vite Environments API (#13101)

See this upcoming change in React Router:
remix-run/react-router#13107

From the changeset:

> Note that for consumers using the `future.unstable_viteEnvironmentApi`
and `serverBundles` options together, hyphens are no longer supported in
server bundle IDs since they also need to be valid Vite environment
names.
wilcoxmd added a commit to wilcoxmd/react-router that referenced this pull request Mar 5, 2025
…d-route-typegen

* upstream/dev: (65 commits)
  Generate types for `virtual:react-router/server-build` (remix-run#13152)
  Add support for client context and middleware (unstable) (remix-run#12941)
  Add playground for `vite-plugin-cloudflare` (remix-run#13151)
  do not typegen params for layout routes with a corresponding index (remix-run#13131) (remix-run#13140)
  Fix types for `loaderData` and `actionData` that contain `Record`s (remix-run#13139)
  chore: format
  chore(dev): remove unused dependencies (remix-run#13134)
  Remove unused Vite file system watcher (remix-run#13133)
  Remove stale changesets cherry-picked into release-next for 7.2.0
  Fix custom SSR build input with `serverBundles` (remix-run#13107)
  Skip resource route flow in dev mode when SPA  mode is enabled (remix-run#13113)
  chore: format
  Add integration test for `vite-plugin-cloudflare` (remix-run#13099)
  Fix custom client `build.rollupOptions.output.entryFileNames` (remix-run#13098)
  Detect lazy route discovery manifest version mismatches and trigger reloads (remix-run#13061)
  Fix critical CSS with custom `Vite.DevEnvironment` (remix-run#13066)
  Fix usage of `prerender` option with `serverBundles` (remix-run#13082)
  Fix support for custom `build.assetsDir` (remix-run#13077)
  Add changeset for remix-run#13064
  Only import the root route when SSRing SPA mode's index.html (remix-run#13023)
  ...
Copy link
Contributor

github-actions bot commented Mar 6, 2025

🤖 Hello there,

We just published version 7.3.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants