Skip to content

Commit

Permalink
fix: static asset handling with leading slash
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 20, 2021
1 parent f7d1daf commit 2303c7d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/rollup/plugins/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
20 changes: 12 additions & 8 deletions src/runtime/server/static.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createError } from 'h3'
import { withoutTrailingSlash, withLeadingSlash, parseURL } from 'ufo'
// @ts-ignore
import { getAsset, readAsset } from '~static'

Expand All @@ -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)) {
Expand Down

0 comments on commit 2303c7d

Please sign in to comment.