u
This commit is contained in:
@@ -24,6 +24,23 @@ function httpsOptions() {
|
||||
};
|
||||
}
|
||||
|
||||
function truncate(text, max = 500) {
|
||||
if (text == null || text === '') return text;
|
||||
const s = typeof text === 'string' ? text : String(text);
|
||||
if (s.length <= max) return s;
|
||||
return `${s.slice(0, max)}… (${s.length} chars)`;
|
||||
}
|
||||
|
||||
function formatLoggedBody(statusCode, body) {
|
||||
if (statusCode === 304) return '(304 no body)';
|
||||
if (statusCode === 204 || body === '') return '(empty)';
|
||||
return truncate(body);
|
||||
}
|
||||
|
||||
function hasRequestBody(method) {
|
||||
return method === 'POST' || method === 'PUT' || method === 'PATCH';
|
||||
}
|
||||
|
||||
const sharedServer = {
|
||||
// HTTP :4209 — HTTPS :4308 (self-signed; see npm run cert / dev:https)
|
||||
port: useHttps ? 4308 : 4209,
|
||||
@@ -35,24 +52,43 @@ const sharedServer = {
|
||||
changeOrigin: true,
|
||||
cookieDomainRewrite: '',
|
||||
configure(proxy) {
|
||||
proxy.on('proxyReq', (_proxyReq, req) => {
|
||||
if (!hasRequestBody(req.method)) return;
|
||||
const chunks = [];
|
||||
req.on('data', (chunk) => chunks.push(chunk));
|
||||
req.on('end', () => {
|
||||
req._proxyRequestBody = chunks.length
|
||||
? Buffer.concat(chunks).toString('utf8')
|
||||
: '';
|
||||
});
|
||||
});
|
||||
|
||||
proxy.on('proxyRes', (proxyRes, req) => {
|
||||
const raw = proxyRes.headers['set-cookie'];
|
||||
if (!raw) {
|
||||
console.log('[vite-proxy]', req.method, req.url, {
|
||||
status: proxyRes.statusCode,
|
||||
setCookie: '(none)',
|
||||
});
|
||||
return;
|
||||
}
|
||||
let cookies = Array.isArray(raw) ? raw : [raw];
|
||||
let cookies = raw ? (Array.isArray(raw) ? raw : [raw]) : null;
|
||||
// Backend sets Secure; browsers/WebViews on plain HTTP refuse it.
|
||||
if (!useHttps) {
|
||||
if (cookies && !useHttps) {
|
||||
cookies = cookies.map((c) => c.replace(/;\s*Secure/gi, ''));
|
||||
proxyRes.headers['set-cookie'] = cookies;
|
||||
}
|
||||
console.log('[vite-proxy]', req.method, req.url, {
|
||||
status: proxyRes.statusCode,
|
||||
setCookie: cookies,
|
||||
|
||||
const responseChunks = [];
|
||||
proxyRes.on('data', (chunk) => responseChunks.push(chunk));
|
||||
proxyRes.on('end', () => {
|
||||
const responseBody = responseChunks.length
|
||||
? Buffer.concat(responseChunks).toString('utf8')
|
||||
: '';
|
||||
const details = {
|
||||
status: proxyRes.statusCode,
|
||||
setCookie: cookies ?? '(none)',
|
||||
response: formatLoggedBody(proxyRes.statusCode, responseBody),
|
||||
};
|
||||
if (hasRequestBody(req.method)) {
|
||||
details.body = req._proxyRequestBody
|
||||
? truncate(req._proxyRequestBody)
|
||||
: '(empty)';
|
||||
}
|
||||
console.log('[vite-proxy]', req.method, req.url, details);
|
||||
});
|
||||
});
|
||||
},
|
||||
@@ -61,6 +97,24 @@ const sharedServer = {
|
||||
};
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
rolldownOptions: {
|
||||
output: {
|
||||
codeSplitting: {
|
||||
groups: [
|
||||
{
|
||||
name: 'react-vendor',
|
||||
test: /node_modules\/(react|react-dom|react-router|react-router-dom|scheduler)\//,
|
||||
},
|
||||
{
|
||||
name: 'mui-vendor',
|
||||
test: /node_modules\/(@mui|@emotion)\//,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
react(),
|
||||
VitePWA({
|
||||
|
||||
Reference in New Issue
Block a user