Skip to content

Commit

Permalink
chore: wip serve static
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Oct 9, 2024
1 parent 0a65d51 commit d63e144
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/vike-node/src/runtime/handler-node-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function handleViteDevServer(req: IncomingMessage, res: ServerResponse): Promise
})
}

function resolveStaticConfig(static_: VikeOptions['static']): false | { root: string; cache: boolean } {
export 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
Expand Down
47 changes: 36 additions & 11 deletions packages/vike-node/src/runtime/vike-handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { parseHeaders } from './utils/header-utils.js'
import { renderPage as _renderPage } from 'vike/server'
import type { VikeHttpResponse, VikeOptions } from './types.js'
import type { ConnectMiddleware, VikeHttpResponse, VikeOptions } from './types.js'
import type { Get, UniversalHandler } from '@universal-middleware/core'
import { globalStore } from './globalStore.js'
import { assert } from '../utils/assert.js'
import type { IncomingMessage, ServerResponse } from 'http'
import { connectToWebFallback } from './adapters/connectToWeb.js'
import { isVercel } from '../utils/isVercel.js'

export { renderPage, renderPageWeb }

Expand Down Expand Up @@ -55,26 +56,50 @@ async function renderPageWeb<PlatformRequest>({
}

export const renderPageUniversal = ((options?) => async (request, context, runtime: any) => {
if (runtime.req || runtime.env?.incoming) {
globalStore.setupHMRProxy(runtime.req ?? runtime.env?.incoming);
const nodeReq: IncomingMessage | undefined = runtime.req ?? runtime.env?.incoming
let staticConfig: false | { root: string; cache: boolean } = false
let shouldCache = false
const compressionType = options?.compress ?? !isVercel()
let staticMiddleware: ConnectMiddleware | undefined

if (nodeReq) {
globalStore.setupHMRProxy(nodeReq)
const { resolveStaticConfig } = await import("./handler-node-only.js")
staticConfig = resolveStaticConfig(options?.static)
shouldCache = staticConfig && staticConfig.cache
}

if (globalStore.isPluginLoaded) {
const handled = await web(request)

// console.log({ url: request.url, handled: Boolean(handled) })
if (handled) return handled
} else {
// const isAsset = req.url?.startsWith('/assets/')
// const shouldCompressResponse = compressionType === true || (compressionType === 'static' && isAsset)
} else if (nodeReq) {
const isAsset = nodeReq.url?.startsWith('/assets/')
const shouldCompressResponse = compressionType === true || (compressionType === 'static' && isAsset)
// if (shouldCompressResponse) {
// await applyCompression(req, res, shouldCache)
// }
//
// if (staticConfig) {
// const handled = await serveStaticFiles(req, res, staticConfig)
// if (handled) return true
// }

if (staticConfig) {
const handled = await connectToWebFallback(serveStaticFiles)(request);
if (handled) return handled
}
}

async function serveStaticFiles(
req: IncomingMessage,
res: ServerResponse
): Promise<boolean> {
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))
staticMiddleware!(req, res, () => resolve(false))
})
}

const pageContextInit = { ...context, ...runtime, urlOriginal: request.url, headersOriginal: request.headers }
Expand Down
4 changes: 1 addition & 3 deletions packages/vike-node/src/utils/assert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pc from '@brillout/picocolors'

export { assert, assertUsage }

function assert(condition: unknown): asserts condition {
Expand All @@ -9,5 +7,5 @@ function assert(condition: unknown): asserts condition {

function assertUsage(condition: unknown, message: string): asserts condition {
if (condition) return
throw new Error(`${pc.cyan('[vike-node]')} wrong usage: ${message}`)
throw new Error(`[vike-node] wrong usage: ${message}`)
}
2 changes: 1 addition & 1 deletion packages/vike-node/tsup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default defineConfig([
esbuildOptions(opts) {
opts.outbase = "src";
},
external: ["stream", "http", "node:stream", "node:http"],
external: ["stream", "http", "node:stream", "node:http", "path", "url"],
dts: true,
outDir: 'dist',
bundle: true
Expand Down

0 comments on commit d63e144

Please sign in to comment.