Skip to content

Commit

Permalink
chore: denoify
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma committed Feb 9, 2024
1 parent 013c76c commit 4718bd9
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions deno_dist/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { replaceUrlParam } from '../../client/utils.ts'
import type { Context } from '../../context.ts'
import { inspectRoutes } from '../../helper/dev/index.ts'
import type { Hono } from '../../hono.ts'
import { METHOD_NAME_ALL } from '../../router.ts'
import type { Env, MiddlewareHandler, Schema } from '../../types.ts'
import { bufferToString } from '../../utils/buffer.ts'
import { getExtension } from '../../utils/mime.ts'
import { joinPaths, dirname } from './utils.ts'

export const X_HONO_SSG_HEADER_KEY = 'x-hono-ssg'
export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg'
const SSG_CONTEXT = 'HONO_SSG_CONTEXT'
export const SSG_DISABLED_RESPONSE = new Response('SSG is disabled', { status: 404 })

/**
* @experimental
Expand Down Expand Up @@ -123,15 +124,14 @@ export const fetchRoutesContent = async <
const baseURL = 'http://localhost'

for (const route of inspectRoutes(app)) {
if (route.isMiddleware) {
if (route.isMiddleware || (route.method !== 'GET' && route.method !== METHOD_NAME_ALL)) {
continue
}

// GET Route Info
const thisRouteBaseURL = new URL(route.path, baseURL).toString()

let forGetInfoURLRequest = new Request(thisRouteBaseURL) as AddedSSGDataRequest
forGetInfoURLRequest.headers.set(X_HONO_SSG_HEADER_KEY, 'true')
if (beforeRequestHook) {
const maybeRequest = beforeRequestHook(forGetInfoURLRequest)
if (!maybeRequest) {
Expand All @@ -150,17 +150,19 @@ export const fetchRoutesContent = async <

for (const param of forGetInfoURLRequest.ssgParams) {
const replacedUrlParam = replaceUrlParam(route.path, param)
let response = await app.request(replacedUrlParam, forGetInfoURLRequest)
if (response.headers.get(X_HONO_DISABLE_SSG_HEADER_KEY)) {
continue
}
let response = await app.request(replacedUrlParam, forGetInfoURLRequest, {
[SSG_CONTEXT]: true,
})
if (afterResponseHook) {
const maybeResponse = afterResponseHook(response)
if (!maybeResponse) {
continue
}
response = maybeResponse
}
if (response === SSG_DISABLED_RESPONSE) {
continue
}
const mimeType = response.headers.get('Content-Type')?.split(';')[0] || 'text/plain'
const content = await parseResponseContent(response)
htmlMap.set(replacedUrlParam, {
Expand Down Expand Up @@ -261,14 +263,22 @@ export const toSSG: ToSSGInterface = async (app, fs, options) => {

/**
* @experimental
* `disableSSG` is an experimental feature.
* `isSSGContext` is an experimental feature.
* The API might be changed.
*/
export const isSSGContext = (c: Context): boolean => !!c.env?.[SSG_CONTEXT]

/**
* @experimental
* `disableSSG` is an experimental feature.
* The API might be changed.
*/
export const disableSSG = (): MiddlewareHandler =>
async function disableSSG(c, next) {
if (isSSGContext(c)) {
return SSG_DISABLED_RESPONSE
}
await next()
c.header(X_HONO_DISABLE_SSG_HEADER_KEY, 'true')
}

/**
Expand All @@ -278,9 +288,8 @@ export const disableSSG = (): MiddlewareHandler =>
*/
export const onlySSG = (): MiddlewareHandler =>
async function onlySSG(c, next) {
const headerValue = c.req.raw.headers.get(X_HONO_SSG_HEADER_KEY)
if (headerValue) {
await next()
if (!isSSGContext(c)) {
return c.notFound()
}
return c.notFound()
await next()
}

0 comments on commit 4718bd9

Please sign in to comment.