-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move middlewares to a dedicated folder
- Loading branch information
Showing
10 changed files
with
186 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import compressMiddlewareFactory from '@universal-middleware/compress' | ||
import { isVercel } from '../utils/isVercel.js' | ||
import { globalStore } from '../runtime/globalStore.js' | ||
import type { VikeOptions } from '../runtime/types.js' | ||
import type { Get, UniversalMiddleware } from '@universal-middleware/core' | ||
|
||
export const compressMiddleware = ((options?) => async (request, _context) => { | ||
const compressionType = options?.compress ?? !isVercel() | ||
const compressMiddlewareInternal = compressMiddlewareFactory()(request) | ||
|
||
return async (response) => { | ||
if (!globalStore.isDev) { | ||
const isAsset = new URL(request.url).pathname.startsWith('/assets/') | ||
const shouldCompressResponse = compressionType === true || (compressionType === 'static' && isAsset) | ||
if (shouldCompressResponse) { | ||
return compressMiddlewareInternal(response) | ||
} | ||
} | ||
return response | ||
} | ||
}) satisfies Get<[options: VikeOptions], UniversalMiddleware> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { Get, UniversalMiddleware } from '@universal-middleware/core' | ||
import { connectToWeb } from '../runtime/adapters/connectToWeb.js' | ||
import { handleViteDevServer } from '../runtime/adapters/handleViteDevServer.js' | ||
import type { IncomingMessage } from 'node:http' | ||
import { globalStore } from '../runtime/globalStore.js' | ||
|
||
export const devServerMiddleware = (() => async (request, _context, runtime) => { | ||
const nodeReq: IncomingMessage | undefined = 'req' in runtime ? runtime.req : undefined | ||
|
||
if (nodeReq) { | ||
const needsUpgrade = globalStore.setupHMRProxy(nodeReq) | ||
|
||
if (needsUpgrade) { | ||
// Early response for HTTP connection upgrade | ||
return new Response(null) | ||
} | ||
} | ||
|
||
const handled = await connectToWeb(handleViteDevServer)(request) | ||
|
||
if (handled) return handled | ||
}) satisfies Get<[], UniversalMiddleware> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { Get, UniversalMiddleware } from '@universal-middleware/core' | ||
import { connectToWeb } from '../runtime/adapters/connectToWeb.js' | ||
import type { IncomingMessage, ServerResponse } from 'node:http' | ||
import { getGlobalContextAsync } from 'vike/server' | ||
import { globalStore } from '../runtime/globalStore.js' | ||
import { assert } from '../utils/assert.js' | ||
import type { ConnectMiddleware, VikeOptions } from '../runtime/types.js' | ||
import { isVercel } from '../utils/isVercel.js' | ||
import { dirname, isAbsolute, join } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
|
||
async function removeBaseUrl(req: IncomingMessage) { | ||
if (!req.url) return | ||
const globalContext = await getGlobalContextAsync(!globalStore.isDev) | ||
const baseAssets = globalContext.baseAssets as string | ||
// Don't choke on older Vike versions | ||
if (baseAssets === undefined) return | ||
const { url } = req | ||
assert(url.startsWith('/')) | ||
let urlWithoutBase = url.slice(baseAssets.length) | ||
if (!urlWithoutBase.startsWith('/')) urlWithoutBase = `/${urlWithoutBase}` | ||
req.url = urlWithoutBase | ||
} | ||
|
||
function resolveStaticConfig(static_: VikeOptions['static']): false | { root: string; cache: boolean } { | ||
// Disable static file serving for Vercel | ||
// Vercel will serve static files on its own | ||
// See vercel.json > outputDirectory | ||
if (isVercel()) return false | ||
if (static_ === false) return false | ||
|
||
const argv1 = process.argv[1] | ||
const entrypointDirAbs = argv1 | ||
? dirname(isAbsolute(argv1) ? argv1 : join(process.cwd(), argv1)) | ||
: dirname(fileURLToPath(import.meta.url)) | ||
const defaultStaticDir = join(entrypointDirAbs, '..', 'client') | ||
|
||
if (static_ === true || static_ === undefined) { | ||
return { root: defaultStaticDir, cache: true } | ||
} | ||
if (typeof static_ === 'string') { | ||
return { root: static_, cache: true } | ||
} | ||
return { | ||
root: static_.root ?? defaultStaticDir, | ||
cache: static_.cache ?? true | ||
} | ||
} | ||
|
||
export const serveStaticMiddleware = ((options?) => async (request, _context) => { | ||
const staticConfig = resolveStaticConfig(options?.static) | ||
let staticMiddleware: ConnectMiddleware | undefined | ||
|
||
// FIXME port sirv to universal-middleware | ||
async function serveStaticFiles(req: IncomingMessage, res: ServerResponse): Promise<boolean> { | ||
await removeBaseUrl(req) | ||
|
||
if (!staticMiddleware) { | ||
const { default: sirv } = await import('sirv') | ||
staticMiddleware = sirv((staticConfig as { root: string; cache: boolean }).root, { etag: true }) | ||
} | ||
|
||
return new Promise<boolean>((resolve) => { | ||
res.once('close', () => resolve(true)) | ||
// biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
staticMiddleware!(req, res, () => resolve(false)) | ||
}) | ||
} | ||
|
||
const handled = await connectToWeb(serveStaticFiles)(request) | ||
if (handled) return handled | ||
}) satisfies Get<[options: VikeOptions], UniversalMiddleware> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
packages/vike-server/src/runtime/utils/resolve-static-config.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.