diff --git a/CHANGELOG.md b/CHANGELOG.md index 3472853177..5f79572e23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,23 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## v2.9.3 + +[compare changes](https://github.com/unjs/nitro/compare/v2.9.2...v2.9.3) + +### 🩹 Fixes + +- **raw:** Use mime to chck binary types and exclude `.json` ([#2239](https://github.com/unjs/nitro/pull/2239)) + +### 📖 Documentation + +- Fix typo ([a445fae6](https://github.com/unjs/nitro/commit/a445fae6)) + +### ❤️ Contributors + +- Pooya Parsa +- Keigo Nakao ([@kspace-trk](http://github.com/kspace-trk)) + ## v2.9.2 [compare changes](https://github.com/unjs/nitro/compare/v2.9.1...v2.9.2) diff --git a/docs/1.guide/9.plugins.md b/docs/1.guide/9.plugins.md index 67f073d695..fe5462ff26 100644 --- a/docs/1.guide/9.plugins.md +++ b/docs/1.guide/9.plugins.md @@ -40,7 +40,7 @@ export default defineNuxtConfig({ ## Nitro runtime hooks -You can use Nitro [hooks]("https://github.com/unjs/hookable) to extend the default runtime behaviour of Nitro by registering custom (async or asnc) functions to the lifecycle events within plugins. +You can use Nitro [hooks](https://github.com/unjs/hookable) to extend the default runtime behaviour of Nitro by registering custom (async or asnc) functions to the lifecycle events within plugins. **Example:** diff --git a/package.json b/package.json index 673d484d5d..ae55a83548 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nitropack", - "version": "2.9.2", + "version": "2.9.3", "description": "Build and Deploy Universal JavaScript Servers", "repository": "unjs/nitro", "license": "MIT", diff --git a/src/rollup/plugins/raw.ts b/src/rollup/plugins/raw.ts index e32c90ef01..778f4f9015 100644 --- a/src/rollup/plugins/raw.ts +++ b/src/rollup/plugins/raw.ts @@ -1,5 +1,6 @@ import { promises as fsp } from "node:fs"; import { extname } from "pathe"; +import mime from "mime"; import type { Plugin } from "rollup"; export interface RawOptions { @@ -18,15 +19,9 @@ export function raw(opts: RawOptions = {}): Plugin { ".css", ".htm", ".html", - ".json", - ".json5", - ".csv", ...(opts.extensions || []), ]); - // TODO: use ext=>mime - const isBinary = (id) => !extensions.has(extname(id)); - return { name: "raw", resolveId(id) { @@ -79,6 +74,17 @@ export function raw(opts: RawOptions = {}): Plugin { }; } +function isBinary(id: string) { + const idMime = mime.getType(id) || ""; + if (idMime.startsWith("text/")) { + return false; + } + if (/application\/(json|xml|yaml)/.test(idMime)) { + return false; + } + return true; +} + function getHelpers() { const js = String.raw; return js` diff --git a/test/fixture/assets/test.json b/test/fixture/assets/test.json new file mode 100644 index 0000000000..c8c4105eb5 --- /dev/null +++ b/test/fixture/assets/test.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} diff --git a/test/fixture/routes/assets/all.ts b/test/fixture/routes/assets/all.ts new file mode 100644 index 0000000000..58b16b9843 --- /dev/null +++ b/test/fixture/routes/assets/all.ts @@ -0,0 +1,25 @@ +export default eventHandler(async (event) => { + const serverAssets = useStorage("assets/server"); + + const keys = await serverAssets.getKeys(); + const items = await Promise.all( + keys.map(async (key) => { + return { + key, + meta: await serverAssets.getMeta(key), + data: await serverAssets.getItem(key).then((r) => + // prettier-ignore + typeof r === "string" ? r.slice(0, 32) : (isPureObject(r) ? r : ``) + ), + }; + }) + ); + + return items; +}); + +function isPureObject(value) { + return ( + value !== null && typeof value === "object" && value.constructor === Object + ); +}