diff --git a/deno_dist/helper/ssg/index.ts b/deno_dist/helper/ssg/index.ts index 4dad2424a..c46601a18 100644 --- a/deno_dist/helper/ssg/index.ts +++ b/deno_dist/helper/ssg/index.ts @@ -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 @@ -123,7 +124,7 @@ 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 } @@ -131,7 +132,6 @@ export const fetchRoutesContent = async < 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) { @@ -150,10 +150,9 @@ 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) { @@ -161,6 +160,9 @@ export const fetchRoutesContent = async < } 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, { @@ -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') } /** @@ -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() }