Skip to content

feat: shared services in core - #5023

Merged
pi0 merged 2 commits into
mainfrom
feat/core-services
Nov 3, 2026
Merged

pi0 merged 2 commits into
mainfrom
feat/core-services

Conversation

@pi0x

@pi0x pi0x commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Implements the core part of #5015 (additive, no breaking change for nitro/vite).

Config

  • New top-level services option ({ [name]: { entry } }), resolved in core (src/config/resolvers/services.ts). vite.services is a deprecated alias (top-level wins), merged by the Vite plugin after modules ran (a module can still change it, as on main).
  • Path entries are resolved from rootDir / scanDirs; bare specifiers are left to the builder (environment conditions apply). The Vite plugin resolves again after modules ran. Inline vite.services override services of the config file, with or without a provided instance (nitro build). A service without entry throws an explicit error.
  • entry-server detection, the server entry vs ssr entry check and the default ssr renderer move to core (src/build/services.ts). Vite calls them and registers auto-detected server environments back into nitro.options.services.

Integration contract

  • Builder integrations set nitro._serviceLoader(name) (internal) to report how each service output is loaded: { import } (lazy import, bundled in production) or { expression } (evaluated per fetch, e.g. the Vite dev worker global).
  • Core generates one #nitro/virtual/services module (replaces #nitro/virtual/vite-services). Without a loader, it is empty, and the build warns at start that the services are ignored (not again for the prerenderer).
  • The renderer template renders <!--ssr-outlet--> with the ssr service and adds fetchService to the template context when services are loaded (_pageTemplate.fetchService is removed). The prerenderer gets services and the loader.

Runtime

  • fetchService(name, input, init?) from nitro/services.
  • fetchViteEnv() from nitro/vite/runtime is now a deprecated alias (ViteServiceConfig type too). The default SSR handler moves to internal/routes/ssr-renderer.

Notes

  • Docs: services config and nitro/services in the utils reference.
  • nitro/rsbuild (feat: experimental nitro/rsbuild plugin #5011) can map services to its environments and set _serviceLoader instead of adding fetchRsbuildEnv().
  • Lazy service error message is now Service "x" entry ... (was Vite service "x" ...).
  • Deprecations are JSDoc only (no runtime warning, not in docs); both keep working.

Tests

  • test/unit/services.test.ts (from vite-services.test.ts): import + expression loaders, empty without a loader.
  • test/unit/services-config.test.ts: path vs bare entries, missing entry error, alias not merged in core.
  • asset-url-fixture uses top-level services + fetchService; ssr-outlet checks fetchService in the template context (dev, build, prerender); plugin-options checks top-level services on a provided instance, services added or overridden by modules (also vite.services) and inline vs config precedence.
  • test/vite, test/unit and pnpm test:rolldown pass locally.

🤖 Generated with AI assistant

@pi0x
pi0x requested a review from pi0 as a code owner November 3, 2026 17:09
@vercel

vercel Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro.build Ready Ready Preview Nov 3, 2026 5:41pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

CodeRabbit ASCII loading animation

Currently processing new changes in this PR. This may take a few minutes, please wait...

Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1d5758f5-61eb-4dff-8493-2fc77682b101
Commits

Reviewing files that changed from the base of the PR and between e02c7cf and 65f68d3.

Files selected for processing: 40
  • .agents/vite-dev.md
  • .agents/vite.md
  • docs/1.docs/61.vite.md
  • docs/1.docs/62.utils.md
  • docs/3.config/8.build.md
  • package.json
  • src/build/build.ts
  • src/build/services.ts
  • src/build/virtual/_all.ts
  • src/build/virtual/renderer-template.ts
  • src/build/virtual/services.ts
  • src/build/vite/_page-template-dev.ts
  • src/build/vite/env.ts
  • src/build/vite/plugin.ts
  • src/build/vite/prod.ts
  • src/build/vite/services.ts
  • src/build/vite/types.ts
  • src/config/defaults.ts
  • src/config/loader.ts
  • src/config/resolvers/services.ts
  • src/prerender/prerender.ts
  • src/runtime/internal/routes/ssr-renderer.ts
  • src/runtime/internal/services.ts
  • src/runtime/internal/vite/ssr-renderer.mjs
  • src/runtime/services.ts
  • src/runtime/virtual/services.ts
  • src/runtime/virtual/vite-services.ts
  • src/runtime/vite.ts
  • src/types/config.ts
  • src/types/index.ts
  • src/types/nitro.ts
  • test/unit/renderer-template.test.ts
  • test/unit/services-config.test.ts
  • test/unit/services.test.ts
  • test/unit/vite-services.test.ts
  • test/vite/asset-url-fixture/nitro.config.ts
  • test/vite/asset-url-fixture/routes/service.ts
  • test/vite/plugin-options.test.ts
  • test/vite/ssr-outlet-fixture/index.html
  • test/vite/ssr-outlet.test.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@5023

commit: 65f68d3

@pi0x
pi0x force-pushed the feat/core-services branch from 903bc3d to c38667f Compare November 3, 2026 17:13
@pi0x
pi0x force-pushed the feat/core-services branch from c38667f to bafb14e Compare November 3, 2026 17:17
@pi0 pi0 changed the title feat: shared services in core with fetchService from nitro/services feat: shared services in core Nov 3, 2026
…ces`

Top-level `services` option (`vite.services` alias), core entry-server detection,
server entry check, default ssr renderer, one services virtual module and
`fetchService()` (`fetchViteEnv()` is an alias).

This branch was successfully deployed

1 active deployment
Preview — 152b2515 Deployed Nov 3, 2026 by vercel[bot]
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.

2 participants