fix: static asset handling with leading slash

This commit is contained in:
Pooya Parsa 2021-01-20 16:00:14 +01:00
parent cc4a32c024
commit fd0be27f0c
2 changed files with 13 additions and 9 deletions

View File

@ -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(),

View File

@ -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)) {