Skip to content

Commit

Permalink
chore: use @universal-middleware/compress
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Oct 15, 2024
1 parent 7505294 commit 2013d2b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/vike-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
"dependencies": {
"@brillout/picocolors": "^1.0.14",
"@nitedani/shrink-ray-current": "^4.3.0",
"@universal-middleware/core": "^0.2.13",
"@universal-middleware/compress": "^0.2.0",
"@vercel/nft": "^0.26.5",
"esbuild": "^0.24.0",
"resolve-from": "^5.0.0",
Expand All @@ -89,9 +91,7 @@
},
"devDependencies": {
"@brillout/release-me": "^0.4.0",
"@major-tanya/itty-compression": "^0.2.2",
"@types/node": "^20.14.15",
"@universal-middleware/core": "^0.2.13 ",
"elysia": "^1.1.6",
"fastify": "^4.28.1",
"h3": "^1.12.0",
Expand Down
15 changes: 3 additions & 12 deletions packages/vike-node/src/runtime/vike-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parseHeaders } from './utils/header-utils.js'
import { renderPage as _renderPage } from 'vike/server'
import type { ConnectMiddleware, VikeHttpResponse, VikeOptions } from './types.js'
import { type Get, pipe, type UniversalHandler, type UniversalMiddleware } from '@universal-middleware/core'
import compressMiddlewareFactory from '@universal-middleware/compress'
import { globalStore } from './globalStore.js'
import { assert } from '../utils/assert.js'
import type { IncomingMessage, ServerResponse } from 'http'
Expand Down Expand Up @@ -58,24 +59,14 @@ async function renderPageWeb<PlatformRequest>({
export const renderPageCompress = ((options?) => async (request, context, runtime: any) => {
const nodeReq: IncomingMessage | undefined = runtime.req
const compressionType = options?.compress ?? !isVercel()
const compressMiddleware = compressMiddlewareFactory()(request);

return async (response) => {
if (!globalStore.isPluginLoaded && nodeReq) {
const isAsset = nodeReq.url?.startsWith('/assets/')
const shouldCompressResponse = compressionType === true || (compressionType === 'static' && isAsset)
if (shouldCompressResponse) {
// FIXME convert to universal-middleware! Wrong usage of getReader().read()
// Could use either CompressionStream or node:zlib
// const { negotiatedCompression } = await import('@major-tanya/itty-compression')
// TODO caching
// const newRes = await negotiatedCompression(response, request)
response.headers.delete('content-length')
response.headers.set('content-encoding', 'gzip')
response.headers.set('vary', 'Accept-Encoding')
return new Response(response.body?.pipeThrough(new CompressionStream('gzip')), response)
// FIXME should be part of the compression lib
// newRes.headers.delete('content-length')
// return newRes
return compressMiddleware(response)
}
}
return response
Expand Down
48 changes: 25 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2013d2b

Please sign in to comment.