Skip to content

Commit

Permalink
fix(ssg): use response header to mark as disabled routes for SSG (#2477)
Browse files Browse the repository at this point in the history
* fix(ssg): use response header to mark as disabled routes for SSG

* fix(ssg): fix initialization error of SSG_DISABLED_RESPONSE, and deprecate it.

* chore: denoify
  • Loading branch information
usualoma committed Apr 9, 2024
1 parent e428a05 commit 58175a1
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
8 changes: 7 additions & 1 deletion deno_dist/helper/ssg/index.ts
Original file line number Diff line number Diff line change
@@ -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'
21 changes: 19 additions & 2 deletions deno_dist/helper/ssg/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions deno_dist/helper/ssg/ssg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion src/helper/ssg/index.ts
Original file line number Diff line number Diff line change
@@ -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'
21 changes: 19 additions & 2 deletions src/helper/ssg/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ 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'

/**
* @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
Expand Down Expand Up @@ -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()
}
Expand Down
12 changes: 10 additions & 2 deletions src/helper/ssg/ssg.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -594,7 +600,9 @@ describe('disableSSG/onlySSG middlewares', () => {
const app = new Hono()
app.get('/', (c) => c.html(<h1>Hello</h1>))
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(<h1>Welcome to my site</h1>))

const fsMock: FileSystemModule = {
Expand Down
4 changes: 2 additions & 2 deletions src/helper/ssg/ssg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 58175a1

Please sign in to comment.