From fd0be27f0c04a8891b15c9dbb217fafe36cb88e8 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 20 Jan 2021 16:00:14 +0100 Subject: [PATCH] fix: static asset handling with leading slash --- packages/nitro/src/rollup/plugins/static.ts | 2 +- packages/nitro/src/runtime/server/static.ts | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/nitro/src/rollup/plugins/static.ts b/packages/nitro/src/rollup/plugins/static.ts index 095416be40..a23487040c 100644 --- a/packages/nitro/src/rollup/plugins/static.ts +++ b/packages/nitro/src/rollup/plugins/static.ts @@ -19,7 +19,7 @@ export function staticAssets (context: SigmaContext) { const etag = createEtag(readFileSync(fullPath)) const stat = statSync(fullPath) - assets[id] = { + assets['/' + id] = { type, etag, mtime: stat.mtime.toJSON(), diff --git a/packages/nitro/src/runtime/server/static.ts b/packages/nitro/src/runtime/server/static.ts index cf639430c7..b802a0c6ab 100644 --- a/packages/nitro/src/runtime/server/static.ts +++ b/packages/nitro/src/runtime/server/static.ts @@ -1,4 +1,5 @@ import { createError } from 'h3' +import { withoutTrailingSlash, withLeadingSlash, parseURL } from 'ufo' // @ts-ignore import { getAsset, readAsset } from '~static' @@ -12,15 +13,18 @@ export default async function serveStatic(req, res) { return } - let id = req.url.split('?')[0] - if (id.startsWith('/')) { - id = id.substr(1) - } - if (id.endsWith('/')) { - id = id.substr(0, id.length - 1) - } + let id = withLeadingSlash(withoutTrailingSlash(parseURL(req.url).pathname)) + let asset = getAsset(id) - const asset = getAsset(id) || getAsset(id = id + '/index.html') + // Try index.html + if (!asset) { + const _id = id + '/index.html' + const _asset = getAsset(_id) + if (_asset) { + asset = _asset + id = _id + } + } if (!asset) { if (id.startsWith(PUBLIC_PATH)) {