From e55783f4edf505a8e9c6febf26b83914ead8e8de Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Mon, 8 Apr 2024 21:28:34 +0900 Subject: [PATCH 1/3] fix(ssg): use response header to mark as disabled routes for SSG --- src/helper/ssg/index.ts | 8 +++++++- src/helper/ssg/middleware.ts | 9 +++++++-- src/helper/ssg/ssg.test.tsx | 12 ++++++++++-- src/helper/ssg/ssg.ts | 4 ++-- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/helper/ssg/index.ts b/src/helper/ssg/index.ts index 11b061084..19d7182bb 100644 --- a/src/helper/ssg/index.ts +++ b/src/helper/ssg/index.ts @@ -1,2 +1,8 @@ export * from './ssg' -export { SSG_DISABLED_RESPONSE, ssgParams, isSSGContext, disableSSG, onlySSG } from './middleware' +export { + X_HONO_DISABLE_SSG_HEADER_KEY, + ssgParams, + isSSGContext, + disableSSG, + onlySSG, +} from './middleware' diff --git a/src/helper/ssg/middleware.ts b/src/helper/ssg/middleware.ts index 99919d201..1925d3af2 100644 --- a/src/helper/ssg/middleware.ts +++ b/src/helper/ssg/middleware.ts @@ -2,7 +2,11 @@ import type { Context } from '../../context' import type { Env, MiddlewareHandler } from '../../types' export const SSG_CONTEXT = 'HONO_SSG_CONTEXT' -export const SSG_DISABLED_RESPONSE = new Response('SSG is disabled', { status: 404 }) +export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg' +export const SSG_DISABLED_RESPONSE = new Response('SSG is disabled', { + status: 404, + headers: { [X_HONO_DISABLE_SSG_HEADER_KEY]: 'true' }, +}) interface SSGParam { [key: string]: string @@ -43,7 +47,8 @@ export const isSSGContext = (c: Context): boolean => !!c.env?.[SSG_CONTEXT] export const disableSSG = (): MiddlewareHandler => async function disableSSG(c, next) { if (isSSGContext(c)) { - return SSG_DISABLED_RESPONSE + c.header(X_HONO_DISABLE_SSG_HEADER_KEY, 'true') + return c.notFound() } await next() } diff --git a/src/helper/ssg/ssg.test.tsx b/src/helper/ssg/ssg.test.tsx index e55cbd4a4..315103605 100644 --- a/src/helper/ssg/ssg.test.tsx +++ b/src/helper/ssg/ssg.test.tsx @@ -3,7 +3,13 @@ import { Hono } from '../../hono' // eslint-disable-next-line @typescript-eslint/no-unused-vars import { jsx } from '../../jsx' import { poweredBy } from '../../middleware/powered-by' -import { SSG_DISABLED_RESPONSE, ssgParams, isSSGContext, disableSSG, onlySSG } from './middleware' +import { + X_HONO_DISABLE_SSG_HEADER_KEY, + ssgParams, + isSSGContext, + disableSSG, + onlySSG, +} from './middleware' import { fetchRoutesContent, saveContentToFile, toSSG, defaultExtensionMap } from './ssg' import type { BeforeRequestHook, @@ -594,7 +600,9 @@ describe('disableSSG/onlySSG middlewares', () => { const app = new Hono() app.get('/', (c) => c.html(

Hello

)) app.get('/api', disableSSG(), (c) => c.text('an-api')) - app.get('/disable-by-response', () => SSG_DISABLED_RESPONSE) + app.get('/disable-by-response', (c) => + c.text('', 404, { [X_HONO_DISABLE_SSG_HEADER_KEY]: 'true' }) + ) app.get('/static-page', onlySSG(), (c) => c.html(

Welcome to my site

)) const fsMock: FileSystemModule = { diff --git a/src/helper/ssg/ssg.ts b/src/helper/ssg/ssg.ts index 583a0ec6b..51be31fa2 100644 --- a/src/helper/ssg/ssg.ts +++ b/src/helper/ssg/ssg.ts @@ -4,7 +4,7 @@ import type { Env, Schema } from '../../types' import { createPool } from '../../utils/concurrent' import { getExtension } from '../../utils/mime' import type { AddedSSGDataRequest, SSGParams } from './middleware' -import { SSG_DISABLED_RESPONSE, SSG_CONTEXT } from './middleware' +import { X_HONO_DISABLE_SSG_HEADER_KEY, SSG_CONTEXT } from './middleware' import { joinPaths, dirname, filterStaticGenerateRoutes } from './utils' const DEFAULT_CONCURRENCY = 2 // default concurrency for ssg @@ -168,7 +168,7 @@ export const fetchRoutesContent = function* < [SSG_CONTEXT]: true, }) ) - if (response === SSG_DISABLED_RESPONSE) { + if (response.headers.get(X_HONO_DISABLE_SSG_HEADER_KEY)) { resolveReq(undefined) return } From da10598e34c1b22c68d75623c5259a2ada692eea Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Mon, 8 Apr 2024 21:57:47 +0900 Subject: [PATCH 2/3] fix(ssg): fix initialization error of SSG_DISABLED_RESPONSE, and deprecate it. --- src/helper/ssg/middleware.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/helper/ssg/middleware.ts b/src/helper/ssg/middleware.ts index 1925d3af2..305db8bd8 100644 --- a/src/helper/ssg/middleware.ts +++ b/src/helper/ssg/middleware.ts @@ -3,10 +3,22 @@ import type { Env, MiddlewareHandler } from '../../types' export const SSG_CONTEXT = 'HONO_SSG_CONTEXT' export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg' -export const SSG_DISABLED_RESPONSE = new Response('SSG is disabled', { - status: 404, - headers: { [X_HONO_DISABLE_SSG_HEADER_KEY]: 'true' }, -}) + +/** + * @deprecated + * Use `X_HONO_DISABLE_SSG_HEADER_KEY` instead. + * This constant will be removed in the next minor version. + */ +export const SSG_DISABLED_RESPONSE = (() => { + try { + return new Response('SSG is disabled', { + status: 404, + headers: { [X_HONO_DISABLE_SSG_HEADER_KEY]: 'true' }, + }) + } catch (e) { + return null + } +})() as Response interface SSGParam { [key: string]: string From ae5685f3a681ea32da31ac71dc5d0d52e3727bcc Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Mon, 8 Apr 2024 22:00:52 +0900 Subject: [PATCH 3/3] chore: denoify --- deno_dist/helper/ssg/index.ts | 8 +++++++- deno_dist/helper/ssg/middleware.ts | 21 +++++++++++++++++++-- deno_dist/helper/ssg/ssg.ts | 4 ++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/deno_dist/helper/ssg/index.ts b/deno_dist/helper/ssg/index.ts index 4d8f31d08..813a9d4d5 100644 --- a/deno_dist/helper/ssg/index.ts +++ b/deno_dist/helper/ssg/index.ts @@ -1,2 +1,8 @@ export * from './ssg.ts' -export { SSG_DISABLED_RESPONSE, ssgParams, isSSGContext, disableSSG, onlySSG } from './middleware.ts' +export { + X_HONO_DISABLE_SSG_HEADER_KEY, + ssgParams, + isSSGContext, + disableSSG, + onlySSG, +} from './middleware.ts' diff --git a/deno_dist/helper/ssg/middleware.ts b/deno_dist/helper/ssg/middleware.ts index 06c6fa549..4d81fc54b 100644 --- a/deno_dist/helper/ssg/middleware.ts +++ b/deno_dist/helper/ssg/middleware.ts @@ -2,7 +2,23 @@ import type { Context } from '../../context.ts' import type { Env, MiddlewareHandler } from '../../types.ts' export const SSG_CONTEXT = 'HONO_SSG_CONTEXT' -export const SSG_DISABLED_RESPONSE = new Response('SSG is disabled', { status: 404 }) +export const X_HONO_DISABLE_SSG_HEADER_KEY = 'x-hono-disable-ssg' + +/** + * @deprecated + * Use `X_HONO_DISABLE_SSG_HEADER_KEY` instead. + * This constant will be removed in the next minor version. + */ +export const SSG_DISABLED_RESPONSE = (() => { + try { + return new Response('SSG is disabled', { + status: 404, + headers: { [X_HONO_DISABLE_SSG_HEADER_KEY]: 'true' }, + }) + } catch (e) { + return null + } +})() as Response interface SSGParam { [key: string]: string @@ -43,7 +59,8 @@ export const isSSGContext = (c: Context): boolean => !!c.env?.[SSG_CONTEXT] export const disableSSG = (): MiddlewareHandler => async function disableSSG(c, next) { if (isSSGContext(c)) { - return SSG_DISABLED_RESPONSE + c.header(X_HONO_DISABLE_SSG_HEADER_KEY, 'true') + return c.notFound() } await next() } diff --git a/deno_dist/helper/ssg/ssg.ts b/deno_dist/helper/ssg/ssg.ts index 8188403f0..1366526d7 100644 --- a/deno_dist/helper/ssg/ssg.ts +++ b/deno_dist/helper/ssg/ssg.ts @@ -4,7 +4,7 @@ import type { Env, Schema } from '../../types.ts' import { createPool } from '../../utils/concurrent.ts' import { getExtension } from '../../utils/mime.ts' import type { AddedSSGDataRequest, SSGParams } from './middleware.ts' -import { SSG_DISABLED_RESPONSE, SSG_CONTEXT } from './middleware.ts' +import { X_HONO_DISABLE_SSG_HEADER_KEY, SSG_CONTEXT } from './middleware.ts' import { joinPaths, dirname, filterStaticGenerateRoutes } from './utils.ts' const DEFAULT_CONCURRENCY = 2 // default concurrency for ssg @@ -168,7 +168,7 @@ export const fetchRoutesContent = function* < [SSG_CONTEXT]: true, }) ) - if (response === SSG_DISABLED_RESPONSE) { + if (response.headers.get(X_HONO_DISABLE_SSG_HEADER_KEY)) { resolveReq(undefined) return }