FetchableDevEnvironment API #18191
Replies: 5 comments 48 replies
-
This looks great! Especially the Is the The purist in me wants to avoid making
I appreciate that such a restriction would make migrations from Vite 5 more awkward though, and possibly prevent other valid use cases. |
Beta Was this translation helpful? Give feedback.
-
How do I dispatch a |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I don't yet completely understand all of this, but I'm liking it. I'm currently building a plugin, which is a directory-based API router that should support multiple runtimes; and, since this is a fresh project, I'm using the v6 beta. The proposed helper functions, to convert requests and responses, would be great. As of now, I've stolen SvelteKit's versions of these, via their As for using a const environment = new FetchableEnvironment(name, config, {
async handleRequest(request) {
const entry = await environment.import(environment.config.entryPoint)
const response = await entry.default.fetch(request)
validateResponse(response)
return response
},
})
await server.environments.ssr.dispatchFetch(
new Request(convertToUrl('/entry-point.js'))
) |
Beta Was this translation helpful? Give feedback.
-
To add my 2 cents to the discussion, I think that this proposal would benefit from more standard middlewares. What I would like to be able to do: server.middlewares.use(async (request: Request) => {
// We can directly reuse `request` in `dispatchFetch`
return new Response(...);
}); I created The new syntax would internally be wrapped using |
Beta Was this translation helpful? Give feedback.
-
There is a growing need to standardise the API around environment communication. Even the default SSR environment currently doesn't expose any methods to run the code.
We have decided to implement
RunnableDevEnvironment
for the default SSR environment that exposes therunner
andimport
directly to decrease the boilerplate (no need to runcreateModuleRunner
manually):Taking this idea further, we think it would be good for the ecosystem to have a standard way to run the code via the Fetch API. This is already how most third-party tools operate, and we have seen more and more requests to support this out of the box. This is where the new
FetchableDevEnvironment
comes in.FetchableDevEnvironment
is aDevEnvironment
that has adispatchFetch
method:FetchableDevEnvironment
class is not exposed (only its type). The class is considered an implementation detail and instead, we will expose acreateFetchableDevEnvironment
function to create the environment like we already do withcreateServer
. You can call it inside theenvironments.dev.createEnvironment
config option:dispatchFetch
is not called anywhere by Vite itself. The expectation is that frameworks can use this to run the code in the adapter's environment:We are also thinking about providing utility functions to convert the
http.IncommingMessage
toRequest
andhttp.ServerResponse
toResponse
. We might also need a function to combineResponse
+http.ServerResponse
.Fetchable Module Runner API
To make things easier on the module runner side, we also want to introduce a
createFetchableModuleRunner
function to create a runner that uses globalfetch
to fetch the module information. If globalfetch
is not available, thecreateFetchableModuleRunner
will throw an error.To achieve this, Vite implements a middleware that intercepts all requests to
/@vite/import/{env}/{url}
(the naming is based on already established/@vite/client
and/@vite/env
) and responds with the result ofenvironment.fetchModule
back to the runner:Default SSR environment as
FetchableDevEnvironment
To reduce boilerplate even further, we could also expose the
dispatchFetch
method on the default SSR environment. At the minimum, this would require:entryPoint
option in the SSR section of the configexport default { fetch }
)The internal implementation might look something like this:
Currently, we don't have plans to do this, but any feedback regarding this is welcome.
Previous discussions:
Beta Was this translation helpful? Give feedback.
All reactions