Skip to content

Commit

Permalink
fix: use array buffers to avoid coercing to text
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 29, 2022
1 parent 8f40676 commit d2f7166
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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()
if (res.status !== 200) {
_route.error = new Error(`[${res.status}] ${res.statusText}`) as any
_route.error.statusCode = res.status
Expand All @@ -65,15 +65,15 @@ export async function prerender (nitro: Nitro) {
const routeWithIndex = route.endsWith('/') ? route + 'index' : route
_route.fileName = isImplicitHTML ? route + '/index.html' : routeWithIndex
const filePath = join(nitro.options.output.publicDir, _route.fileName)
const isBinary = isBinaryContentType(res.headers.get('content-type'))
await writeFile(filePath, _route.contents, false, isBinary ? 'binary' : 'utf-8')
await writeFile(filePath, Buffer.from(_route.data))

// Crawl route links
if (
!_route.error &&
nitro.options.prerender.crawlLinks &&
isImplicitHTML
) {
_route.contents = new TextDecoder('utf-8').decode(new Uint8Array(_route.data))
const crawledRoutes = extractLinks(_route.contents, route, res)
for (const crawledRoute of crawledRoutes) {
if (canPrerender(crawledRoute)) {
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
8 changes: 2 additions & 6 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, format = 'utf-8') {
export async function writeFile (file: string, contents: Buffer | string, log = false) {
await fse.mkdirp(dirname(file))
await fse.writeFile(file, contents, format)
await fse.writeFile(file, contents, typeof contents === 'string' ? 'utf-8' : undefined)
if (log) {
consola.info('Generated', prettyPath(file))
}
Expand Down Expand Up @@ -145,7 +145,3 @@ export function resolveAliases (aliases: Record<string, string>) {
}
return aliases
}

const BINARY_CONTENT_TYPE_RE = /^(audio|font|video|image\/(?!svg)|application\/(?!json))/
export const isBinaryContentType = (contentType = '') =>
BINARY_CONTENT_TYPE_RE.test(contentType)

0 comments on commit d2f7166

Please sign in to comment.