@@ -26,9 +26,21 @@ are served and how ISG revalidation state is tracked.
2626
2727## Adapter Interface
2828
29- Each adapter exports two things:
29+ Each adapter exports three things:
3030
31- ### 1. Request handler factory
31+ ### 1. Adapter factory (for the Vite plugin)
32+
33+ ``` typescript
34+ // Example: Node adapter
35+ import { nodeAdapter } from " @viact/adapter-node" ;
36+
37+ viact ({ adapter: nodeAdapter () });
38+ ```
39+
40+ The factory returns a ` ViactAdapter ` object that the Vite plugin uses to
41+ generate the server entry module.
42+
43+ ### 2. Request handler factory
3244
3345``` typescript
3446// Example: Node adapter
@@ -37,14 +49,14 @@ export function createNodeRequestHandler<TContext>(
3749): (req : IncomingMessage , res : ServerResponse ) => Promise <void >;
3850```
3951
40- ### 2 . Entry module generator (for the Vite plugin )
52+ ### 3 . Entry module generator (for custom adapters )
4153
4254``` typescript
4355export function createNodeServerEntryModule(options ? : NodeServerEntryModuleOptions ): string ;
4456```
4557
46- The Vite plugin calls the entry module generator to create a virtual module
47- (` virtual:viact/node- server ` ) that bootstraps the server.
58+ The adapter factory calls the entry module generator internally to create a virtual module
59+ (` virtual:viact/server ` ) that bootstraps the server.
4860
4961---
5062
@@ -114,7 +126,7 @@ the executable production server entry.
114126- ** Default request context** : generated worker entries pass `{ env,
115127executionContext }` to viact so loaders, actions, and middleware can access
116128 bindings without extra wiring.
117- - ** Build output** : ` viact({ adapter: "cloudflare" }) ` makes ` viact build `
129+ - ** Build output** : ` viact({ adapter: cloudflareAdapter() }) ` makes ` viact build `
118130 emit a Worker bundle in ` dist/server/server.js ` . You own a ` wrangler.jsonc `
119131 in your project root that points at the output — this lets you add KV, D1,
120132 R2, cron, and any other Cloudflare bindings without losing them on rebuild.
@@ -163,7 +175,7 @@ export default {
163175
164176- ** Edge runtime handler** : generated server entries export a default ` fetch ` -style
165177 handler that Vercel bundles as an Edge Function.
166- - ** Build Output API v3** : ` viact({ adapter: "vercel" }) ` makes ` viact build `
178+ - ** Build Output API v3** : ` viact({ adapter: vercelAdapter() }) ` makes ` viact build `
167179 emit ` .vercel/output/config.json ` , ` .vercel/output/static/ ` , and
168180 ` .vercel/output/functions/render.func/ ` .
169181- ** Clean URL routing** : prerendered SSG pages are copied into
@@ -202,19 +214,51 @@ export default async function handle(request, context) {
202214
203215## Writing a Custom Adapter
204216
205- An adapter needs to:
217+ A custom adapter exports a factory function that returns a ` ViactAdapter` object:
218+
219+ ` ` ` typescript
220+ import type { ViactAdapter } from " @viact/vite-plugin" ;
221+
222+ export function myAdapter (options ?: MyOptions ): ViactAdapter {
223+ return {
224+ id: " my-platform" ,
225+ serverImports: ' import { handleViactRequest, resolveApp, resolveApiRoutes } from "viact";' ,
226+ createServerEntryModule () {
227+ // Return JavaScript source code that will be appended to the
228+ // generated virtual:viact/server module.
229+ return `
230+ export default async function handle(request) {
231+ return handleViactRequest({
232+ app: resolvedApp,
233+ registry,
234+ request,
235+ apiRoutes,
236+ clientEntryUrl: clientEntryUrl ?? undefined,
237+ cssManifest,
238+ jsManifest,
239+ });
240+ }
241+ ` ;
242+ },
243+ };
244+ }
245+ ` ` `
246+
247+ The generated server entry module has access to ` resolvedApp` , ` registry` ,
248+ ` apiRoutes` , ` clientEntryUrl` , ` cssManifest` , and ` jsManifest` -- your
249+ ` createServerEntryModule ()` code can reference these directly.
250+
251+ At the runtime level, an adapter also typically needs to:
206252
2072531. **Accept a platform request** and convert it to a Web ` Request` object
208- 2. **Check for static assets** — serve files from ` dist/ client/ ` with appropriate
254+ 2. **Check for static assets** -- serve files from ` dist/ client/ ` with appropriate
209255 headers (content-type, cache-control with immutable for hashed assets)
210- 3. **Check for prerendered pages** — SSG and ISG routes have HTML files on disk.
256+ 3. **Check for prerendered pages** -- SSG and ISG routes have HTML files on disk.
211257 For ISG, implement staleness checking.
212- 4. **Delegate dynamic requests** to ` handleViactRequest ()` from ` @ viact/ framework `
258+ 4. **Delegate dynamic requests** to ` handleViactRequest ()` from ` viact`
2132595. **Convert the Web ` Response` ** back to the platform's response format
214- 6. **Provide a context factory** — create app-level context from platform-specific
260+ 6. **Provide a context factory** -- create app-level context from platform-specific
215261 inputs (env bindings, headers, etc.)
216- 7. **Export an entry module generator** — a function that returns JavaScript source
217- code for the Vite plugin to use as a virtual module
218262
219263### Context factory pattern
220264
0 commit comments