Skip to content
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

Return contentType in transformRequest #15865

Open
4 tasks done
nitedani opened this issue Feb 10, 2024 · 1 comment
Open
4 tasks done

Return contentType in transformRequest #15865

nitedani opened this issue Feb 10, 2024 · 1 comment

Comments

@nitedani
Copy link

Description

I would like to use transformRequest for retrieving assets through an rpc connection between a worker and the Vite dev server.
My issue is that transformRequest doesn't return the content-type of the result. I need to set the right content-type in the response sent to the browser.

An alternative would be to support passing a standard Request to the vite middlewares, returning a standard Response. #15389

This is not a blocker. I can always just proxy the requests through http between browser<->worker<->vite.

Suggested solution

Add contentType to the transformRequest return type.

Alternative

Support passing a standard Request to the vite middlewares, returning a standard Response.
#15389

Additional context

No response

Validations

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Feb 26, 2024

I think ViteDevServer.transformRequest is meant to return javascript for most of the cases?

// resolve, load and transform using the plugin container
const result = await transformRequest(url, server, {
html: req.headers.accept?.includes('text/html'),
})
if (result) {
const depsOptimizer = getDepsOptimizer(server.config, false) // non-ssr
const type = isDirectCSSRequest(url) ? 'css' : 'js'
const isDep =
DEP_VERSION_RE.test(url) || depsOptimizer?.isOptimizedDepUrl(url)
return send(req, res, result.code, type, {
etag: result.etag,
// allow browser to cache npm deps!
cacheControl: isDep ? 'max-age=31536000,immutable' : 'no-cache',
headers: server.config.server.headers,
map: result.map,
})
}

I remember there's .css?direct (which I think vike uses?) but this might be the only odd ball which returns text/css content type. If that's the case, then you could check ?direct on your runtime? If that's not the case, then I wonder what's the other possible content types.

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

No branches or pull requests

2 participants