Skip to content

Commit

Permalink
feat: support prerendering binary files (#320)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
danielroe and pi0 authored Aug 24, 2022
1 parent cc8527a commit d1d1015
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ export async function prerender (nitro: Nitro) {

// Fetch the route
const res = await (localFetch(withBase(route, nitro.options.baseURL), { headers: { 'x-nitro-prerender': route } }) as ReturnType<typeof fetch>)
_route.contents = await res.text()
_route.data = await res.arrayBuffer()
Object.defineProperty(_route, 'contents', {
get: () => {
if (!(_route as any)._contents) {
(_route as any)._contents = new TextDecoder('utf-8').decode(new Uint8Array(_route.data))
}
return (_route as any)._contents
}
})
if (res.status !== 200) {
_route.error = new Error(`[${res.status}] ${res.statusText}`) as any
_route.error.statusCode = res.status
Expand All @@ -75,7 +83,7 @@ export async function prerender (nitro: Nitro) {
if (_route.skip) { return }

const filePath = join(nitro.options.output.publicDir, _route.fileName)
await writeFile(filePath, _route.contents)
await writeFile(filePath, Buffer.from(_route.data))

// Crawl route links
if (!_route.error && isImplicitHTML) {
Expand Down
1 change: 1 addition & 0 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Nitro {
export interface PrerenderRoute {
route: string
contents?: string
data?: ArrayBuffer
fileName?: string
error?: Error & { statusCode: number, statusMessage: string }
generateTimeMS?: number
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export function tryImport (dir: string, path: string) {
} catch (_err) { }
}

export async function writeFile (file: string, contents: string, log = false) {
export async function writeFile (file: string, contents: Buffer | string, log = false) {
await fse.mkdirp(dirname(file))
await fse.writeFile(file, contents, 'utf-8')
await fse.writeFile(file, contents, typeof contents === 'string' ? 'utf-8' : undefined)
if (log) {
consola.info('Generated', prettyPath(file))
}
Expand Down
1 change: 1 addition & 0 deletions test/fixture/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineNitroConfig({
],
routes: [
'/prerender',
'/icon.png',
'/404'
]
}
Expand Down
15 changes: 15 additions & 0 deletions test/fixture/routes/icon.png.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const LOGO_BASE64 = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAT9JREFUOE+t081KAlEUwPH/nRkn0pmgZWQgFLTpBYqQVkFUmgTRNnqKkFrZop7Blm4KQq02rVqYr1AILaKPZYIzGY46ExdRNMWP6u4ul/O759x7jggli0fgxQGd0ZYD4liEkh+VXwQ3r3Ik4DV3igBVQNXtnYomQB65rQjoALbndE7DAfZvbe5eq11KJmKSeqxwXnBaZ13ASThAyfHYTJd4sztTGQrYWxjjueQybSjsXFs4bcbQwO6NTSZqkn+vcZgvt9IdGthIW8xPqlxGTOK5MumnRs0jATIgNquTWPYTy1oUivXRAYkklvwsTmlEsxapNWPwL8hHlCU0l67CxbrJi+0yYyqkHgZ8409AQkFD4WprAsMniN9/9u+DXoBEVoI+zlYNDnJ9AE0Bn4Cveu9WHtcabV5r643/GKa/jfM3OT68lZwxK8oAAAAASUVORK5CYII='

export default eventHandler((event) => {
const buff = base64ToArray(LOGO_BASE64)
event.res.end(buff)
})

function base64ToArray (base64) {
const str = atob(base64)
const bytes = new Uint8Array(str.length)
for (let i = 0; i < str.length; i++) {
bytes[i] = str.charCodeAt(i)
}
return bytes
}

0 comments on commit d1d1015

Please sign in to comment.